c语言实现双链表的基本操作—增删改查
//初始化
Node*InitList()
{Node*head=(Node*)malloc(sizeof(Node));if(NULL==head){printf("内存分配失败!");}printf("内存分配成功!\n");head->data=-1;head->next=NULL;head->prior=NULL;return head;
}
//向链表中添加数据
void Add(Node *s,int n)
{Node *p=s;printf("请输入%d个数据:",n);for(int i=1;i<=n;i++){Node*q=(Node*)malloc(sizeof(Node));scanf("%d",&q->data);q->next=NULL;p->next=q;q->prior=p;p=q;}
}
将q节点插入到p节点之后时,要让q指针先和p->next节点相连接,最后在连接p和q,否者会造成重复;
//插入数据包括(头插,前插,尾插)
void Insert(Node*s)
{Node*p=s;int a,b;printf("请输入你要插入的数据:");scanf("%d",&a);Node*q=(Node*)malloc(sizeof(Node));q->data=a;q->next=NULL;q->prior=NULL;printf("请输入年要插入哪个数据之后:");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("插入失败,没有该数据!");}
}
//头插法
void Insert(Node*s)
{int a;printf("请输入你要插入的数据:");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("请输入你要删除的数据!");scanf("%d",&a); Node*temp=s; int flag=0;while(temp){ if(temp->data==a&&temp->next!=NULL){printf("已执行!");temp->next->prior=temp->prior;temp->prior->next=temp->next;free(temp);flag=1;break;}else if(temp->data==a&&temp->next==NULL)//如果删除的是最后一个节点; {printf("shabi");temp->prior->next=NULL;free(temp);flag=1;break;}temp=temp->next;}if(flag==1){printf("删除成功!");}else{printf("删除失败!"); }
}
//释放全部节点
void Empty(Node*s)
{Node*p=s;while(p){s=s->next;free(p);p=s;}printf("\n");printf("该内存已经释放!");
}
以下为原代码:
//双链表
#include
#include
typedef struct DNode{
int data;
struct DNode next;
struct DNode prior;
}Node;
//初始化
NodeInitList()
{
Nodehead=(Node*)malloc(sizeof(Node));
if(NULLhead)
{
printf(“内存分配失败!”);
}
printf(“内存分配成功!\n”);
head->data=-1;
head->next=NULL;
head->prior=NULL;
return head;
}
//向链表中添加数据
void Add(Node s,int n)
{
Node p=s;
printf(“请输入%d个数据:”,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;
}
}
//释放节点
void Empty(Nodes)
{
Nodep=s;
while§
{
s=s->next;
free§;
p=s;
}
printf("\n");
printf(“该内存已经释放!”);
}
//插入数据包括(尾插,前插,尾插)
void Insert(Nodes)
{
Nodep=s;
int a,b;
printf(“请输入你要插入的数据:”);
scanf("%d",&a);
Nodeq=(Node)malloc(sizeof(Node));
q->data=a;
q->next=NULL;
q->prior=NULL;
printf(“请输入年要插入哪个数据之后:”);
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(“插入失败,没有该数据!”);
}
}
//头插法
void Insert(Nodes)
{
int a;
printf(“请输入你要插入的数据:”);
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(“请输入你要删除的数据!”);
scanf("%d",&a);
Node*temp=s;
int flag=0;
while(temp)
{
if(temp->data==a&&temp->next!=NULL){printf("已执行!");temp->next->prior=temp->prior;temp->prior->next=temp->next;free(temp);flag=1;break;}else if(temp->data==a&&temp->next==NULL)//如果删除的是最后一个节点; {printf("shabi");temp->prior->next=NULL;free(temp);flag=1;break;}temp=temp->next;
}
if(flag==1)
{printf("删除成功!");
}
else
{printf("删除失败!");
}
}*/
int main()
{
Node *head;ni
head=InitList();
int a;
printf(“请输入你要增加多少数据:”);
scanf("%d",&a);
Add(head,a);
// Insert(head);
//printf(“该数据情况如下:\n”);
// Display(head);
// printf("\n");
//Delete(head);
// printf("\n");
printf(“该数据情况如下:\n”);
Display(head);
Empty(head);
}
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
