C语言 循环单链表各种基本运算的算法
来源:互联网 发布:数据库漏洞防范 编辑:程序博客网 时间:2024/06/04 19:52
//实现循环单链表各种基本运算的算法
#include<stdio.h>
#include<stdlib.h>
typedef struct LNode
{
int data;
struct LNode *next;
}LinkList;
//新建一个只含头结点的空循环链表
void InitList(LinkList *&L)
{
L=(LinkList *)malloc(sizeof(LinkList));
L->data=0;
L->next=L;
}
//采用尾插法插入数据x
void ListInsertTail(LinkList *&L,int x)
{
LinkList *p=NULL,*q=NULL;
q=(LinkList *)malloc(sizeof(LinkList));
p=L;
if(L->data==0)//只有一个空结点时
{
L->data=x;
return ;
}
while(p->next!=L)
{
p=p->next;
}
q->data=x;
q->next=L;
p->next=q;
}
//计算链表的长度
int ListLength(LinkList *L)
{
int k=1;
LinkList *p=L;
while(p->next!=L)
{
k++;
p=p->next;
}
return k;
}
//将数据x插入到循环链表的第i个位置
void ListInsert(LinkList *&L,int x,int i)
{
int j=1;
LinkList *p=NULL,*q=NULL;
if(i<1||i>ListLength(L)+1)
{
printf("插入不成功!位置选择错误...\n");
return ;
}
q=(LinkList *)malloc(sizeof(LinkList));
q->data=x;
p=L;
if(i==1)
{
while(p->next!=L)
{
p=p->next;
}
p->next=q;
q->next=L;
L=q;
return ;
}
while(j!=i-1)
{
p=p->next;
j++;
}
q->next=p->next;
p->next=q;
}
//输出链表
void DispList(LinkList *L)
{
LinkList *p=L;
while(p->next!=L)
{
printf("%d ",p->data);
p=p->next;
}
printf("%d\n",p->data);
}
//销毁链表
void DestroyList(LinkList *&L)
{
LinkList *p=NULL;
while(L->next!=L)
{
p=L->next;
L->next=L->next->next;
free(p);
}
free(L);
}
void main(void)
{
int x,y;
LinkList *L=NULL;
//新建一个只有一个节点的空链表
InitList(L);
//采用尾插法插入数据
printf("请输入一串整数,以 0 结束:\n");
scanf("%d",&x);
while(x!=0)
{
ListInsertTail(L,x);
scanf("%d",&x);
}
//显示链表
DispList(L);
printf("链表的长度为:%d\n",ListLength(L));
//在链表的第y个位置插入数据x
printf("请输入要插入的位置 y 以及数据 x :\n");
scanf("%d%d",&y,&x);
ListInsert(L,x,y);
printf("在链表的第%d个位置插入数据%d后,链表为:",y,x);
DispList(L);
//销毁链表
DestroyList(L);
}
- C语言 循环单链表各种基本运算的算法
- C语言 双链表各种基本运算的算法
- 实现循环单链表各种基本运算的算法
- !实现循环双链表各种基本运算的算法
- 实现单链表各种基本运算的算法
- 单链表各种基本运算的算法
- 循环队列的各种基本运算
- C语言实现二叉排序树的基本运算算法
- c 语言的基本运算
- C语言的基本运算
- 顺序表各种基本运算的算法
- 顺序栈各种基本运算的算法
- 环形队列各种基本运算的算法
- 顺序串各种基本运算的算法
- 二叉树各种基本运算的算法
- 单链表各种基本的运算
- 单链表的各种基本运算
- 单链表的各种基本运算
- 文件系统制作(cramfs,jffs2)及busybox编译
- (VC)半透明的实现
- 再写服务器_打死不修改_第二版
- hdu1847
- ffmpeg+sdl教程——编写一个简单的播放器2(输出视频到屏幕)
- C语言 循环单链表各种基本运算的算法
- 计算机网络学习笔记------前言
- quartz的使用(三)
- OpenOffice中makefile文件描述
- Android基于ListView和CheckBox实现多选和全选记录的功能(转)
- hibernate duplicate import解决办法
- 计算机网络-----物理层
- vs2008安装部署工程制作教程
- struts2~按钮~提交~不同滴action~~