日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

c语言实现循环单链表

發(fā)布時(shí)間:2024/10/5 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 c语言实现循环单链表 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
//初始化 Node*InitList() {Node*head=(Node*)malloc(sizeof(Node));head->next=NULL;head->data=-1;return head; }

增加數(shù)據(jù)

void Add(Node*s) {Node*p=s;int n;printf("你要輸入多少數(shù)據(jù):");scanf("%d",&n);printf("請(qǐng)輸入%d個(gè)數(shù)據(jù):",n);for(int i=1;i<=n;i++){Node*q=(Node*)malloc(sizeof(Node));scanf("%d",&q->data);p->next=q;p=q;}p->next=s; }

刪除操作

void Delete(Node*s) {int a;printf("請(qǐng)輸入你要?jiǎng)h除的數(shù)據(jù):");scanf("%d",&a);Node*p=s->next;int flag=0;Node*q=s;while(p!=s){if(p->data==a){q->next=p->next;free(p);flag=1;break;}p=p->next;q=q->next; } }

插入操作

void Insert(Node*s) {int a;int flag=0;Node*q=(Node*)malloc(sizeof(Node));printf("請(qǐng)輸入你要插入的數(shù)據(jù):");scanf("%d",&q->data);printf("請(qǐng)輸入你要插入哪個(gè)數(shù)據(jù)之后:");scanf("%d",&a);Node*p=s->next;while(p!=s){if(p->data==a){q->next=p->next;p->next=q;flag=1;break;}p=p->next;}if(flag==1)printf("插入成功!");elseprintf("插入失敗!"); } //頭插法 /* void Insert(Node*s) {Node*q=(Node*)malloc(sizeof(Node));printf("請(qǐng)輸入你要插入的數(shù)據(jù):");scanf("%d",&q->data);q->next=s->next;s->next=q; } */

顯示操作

void Display(Node*s) {Node*v=s->next;while(v!=s){printf("%d->",v->data);v=v->next;} }

清空操作

void Empty(Node*s) {Node*p=s->next;Node*q=p->next;while(p!=s){free(p);p=q;q=q->next;}free(s); }

原代碼如下
#include<stdio.h>
#include<malloc.h>
typedef struct Node{
int data;
struct Nodenext;
}Node;
//初始化
NodeInitList()
{
Nodehead=(Node)malloc(sizeof(Node));
head->next=NULL;
head->data=-1;
return head;
}
void Add(Nodes)
{
Nodep=s;
int n;
printf(“你要輸入多少數(shù)據(jù):”);
scanf("%d",&n);
printf(“請(qǐng)輸入%d個(gè)數(shù)據(jù):”,n);
for(int i=1;i<=n;i++)
{
Nodeq=(Node)malloc(sizeof(Node));
scanf("%d",&q->data);
p->next=q;
p=q;
}
p->next=s;
}
void Display(Nodes)
{
Nodev=s->next;
while(v!=s)
{
printf("%d->",v->data);
v=v->next;
}
}
void Empty(Nodes)
{
Nodep=s->next;
Nodeq=p->next;
while(p!=s)
{
free§;
p=q;
q=q->next;
}
free(s);
}
/void Delete(Nodes)
{
int a;
printf(“請(qǐng)輸入你要?jiǎng)h除的數(shù)據(jù):”);
scanf("%d",&a);
Nodep=s->next;
int flag=0;
Nodeq=s;
while(p!=s)
{
if(p->data==a)
{
q->next=p->next;
free§;
flag=1;
break;
}
p=p->next;
q=q->next;
}
}/
void Insert(Nodes)
{
int a;
int flag=0;
Nodeq=(Node*)malloc(sizeof(Node));
printf(“請(qǐng)輸入你要插入的數(shù)據(jù):”);
scanf("%d",&q->data);
printf(“請(qǐng)輸入你要插入哪個(gè)數(shù)據(jù)之后:”);
scanf("%d",&a);
Nodep=s->next;
while(p!=s)
{
if(p->dataa)
{
q->next=p->next;
p->next=q;
flag=1;
break;
}
p=p->next;
}
if(flag1)
printf(“插入成功!”);
else
printf(“插入失敗!”);
}
//頭插法
/
void Insert(Nodes)
{
Nodeq=(Node*)malloc(sizeof(Node));
printf(“請(qǐng)輸入你要插入的數(shù)據(jù):”);
scanf("%d",&q->data);
q->next=s->next;
s->next=q;
} /
int main()
{
Nodehead=InitList();
Add(head);
// Delete(head);
Insert(head);
printf(“該數(shù)據(jù)情況如下”);
printf("\n");
Display(head);
Empty(head);
printf(“該鏈表已清空!”);
return 0;
}

總結(jié)

以上是生活随笔為你收集整理的c语言实现循环单链表的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。