c语言实现双链表的基本操作—增删改查
將q節(jié)點插入到p節(jié)點之后時,要讓q指針先和p->next節(jié)點相連接,最后在連接p和q,否者會造成重復(fù);
//插入數(shù)據(jù)包括(頭插,前插,尾插) void Insert(Node*s) {Node*p=s;int a,b;printf("請輸入你要插入的數(shù)據(jù):");scanf("%d",&a);Node*q=(Node*)malloc(sizeof(Node));q->data=a;q->next=NULL;q->prior=NULL;printf("請輸入年要插入哪個數(shù)據(jù)之后:");scanf("%d",&b);int flag=0;while(p){if(p->data==b&&p->next!=NULL){q->next=p->next;p->next->prior=q;p->next=q;q->prior=p;flag=1;break;}else if(p->data==b&&p->next==NULL){p->next=q;q->prior=p;flag=1;break; }p=p->next;}if(flag==1){printf("插入成功!\n");}else{printf("插入失敗,沒有該數(shù)據(jù)!");} } //頭插法 void Insert(Node*s) {int a;printf("請輸入你要插入的數(shù)據(jù):");scanf("%d",&a);Node*q=(Node*)malloc(sizeof(Node));q->data=a;q->next=NULL;q->prior=NULL;q->next=s->next;s->next->prior=q;s->next=q;q->prior=s; } //刪除操作 void Delete(Node*s) {int a;printf("請輸入你要刪除的數(shù)據(jù)!");scanf("%d",&a); Node*temp=s; int flag=0;while(temp){ if(temp->data==a&&temp->next!=NULL){printf("已執(zhí)行!");temp->next->prior=temp->prior;temp->prior->next=temp->next;free(temp);flag=1;break;}else if(temp->data==a&&temp->next==NULL)//如果刪除的是最后一個節(jié)點; {printf("shabi");temp->prior->next=NULL;free(temp);flag=1;break;}temp=temp->next;}if(flag==1){printf("刪除成功!");}else{printf("刪除失敗!"); } } //釋放全部節(jié)點 void Empty(Node*s) {Node*p=s;while(p){s=s->next;free(p);p=s;}printf("\n");printf("該內(nèi)存已經(jīng)釋放!"); }以下為原代碼:
//雙鏈表
#include<stdio.h>
#include<malloc.h>
typedef struct DNode{
int data;
struct DNode next;
struct DNode prior;
}Node;
//初始化
NodeInitList()
{
Nodehead=(Node*)malloc(sizeof(Node));
if(NULLhead)
{
printf(“內(nèi)存分配失敗!”);
}
printf(“內(nèi)存分配成功!\n”);
head->data=-1;
head->next=NULL;
head->prior=NULL;
return head;
}
//向鏈表中添加數(shù)據(jù)
void Add(Node s,int n)
{
Node p=s;
printf(“請輸入%d個數(shù)據(jù):”,n);
for(int i=1;i<=n;i++)
{
Nodeq=(Node)malloc(sizeof(Node));
scanf("%d",&q->data);
q->next=NULL;
p->next=q;
q->prior=p;
p=q;
}
}
void Display(Nodes)
{
Nodev=s->next;
while(v)
{
printf("%d->",v->data);
v=v->next;
}
}
//釋放節(jié)點
void Empty(Nodes)
{
Nodep=s;
while§
{
s=s->next;
free§;
p=s;
}
printf("\n");
printf(“該內(nèi)存已經(jīng)釋放!”);
}
//插入數(shù)據(jù)包括(尾插,前插,尾插)
void Insert(Nodes)
{
Nodep=s;
int a,b;
printf(“請輸入你要插入的數(shù)據(jù):”);
scanf("%d",&a);
Nodeq=(Node)malloc(sizeof(Node));
q->data=a;
q->next=NULL;
q->prior=NULL;
printf(“請輸入年要插入哪個數(shù)據(jù)之后:”);
scanf("%d",&b);
int flag=0;
while§
{
if(p->datab&&p->next!=NULL)
{
q->next=p->next;
p->next->prior=q;
p->next=q;
q->prior=p;
flag=1;
break;
}
else if(p->datab&&p->nextNULL)
{
p->next=q;
q->prior=p;
flag=1;
break;
}
p=p->next;
}
if(flag==1)
{
printf(“插入成功!\n”);
}
else
{
printf(“插入失敗,沒有該數(shù)據(jù)!”);
}
}
//頭插法
void Insert(Nodes)
{
int a;
printf(“請輸入你要插入的數(shù)據(jù):”);
scanf("%d",&a);
Nodeq=(Node*)malloc(sizeof(Node));
q->data=a;
q->next=NULL;
q->prior=NULL;
q->next=s->next;
s->next->prior=q;
s->next=q;
q->prior=s;
}
//刪除操作
/void Delete(Nodes)
{
int a;
printf(“請輸入你要刪除的數(shù)據(jù)!”);
scanf("%d",&a);
Node*temp=s;
int flag=0;
while(temp)
{
}*/
int main()
{
Node *head;ni
head=InitList();
int a;
printf(“請輸入你要增加多少數(shù)據(jù):”);
scanf("%d",&a);
Add(head,a);
// Insert(head);
//printf(“該數(shù)據(jù)情況如下:\n”);
// Display(head);
// printf("\n");
//Delete(head);
// printf("\n");
printf(“該數(shù)據(jù)情況如下:\n”);
Display(head);
Empty(head);
}
總結(jié)
以上是生活随笔為你收集整理的c语言实现双链表的基本操作—增删改查的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: jenkins自动化打包部署,jenki
- 下一篇: c语言实现循环单链表