#include<stdio.h>
#include<stdlib.h>
typedef struct node{
int data;
struct node *period;
struct node *next;
}ElemSN;
ElemSN *Createlink(int *Data,int n)
{
ElemSN *np,*head=NULL,*tail;
for(int i=0;i<n;i++){//创建新结点
np=(ElemSN *)malloc(sizeof(ElemSN));
np->data=*(Data+i);
np->next=np->period=NULL;
if(!head){//挪头指针
head=tail=np;
}
else{//挂链挪尾指针
tail->next=np;
np->period=tail;
tail=tail->next;
}
}
return head;
}
ElemSN *Delkeynode(ElemSN *h,int key)
{
ElemSN *p;
for(p=h;p&&p->data!=key;p=p->next);
if(h->next){
if(!p){
printf("Not Found!");
}
else{
if(p==h){//头删
h=h->next;
h->period=NULL;
}
else{
p->period->next=p->next;
if(p->next&&p->period){
p->next->period=p->period;
}
}
free(p);
}
}
else
{
printf("All of these node have been deleted!");
}
return h;
}
void Printlink(ElemSN *head)
{
ElemSN *p;
for(p=head;p;p=p->next)
printf("%5d",p->data);//正向输出
}
int main()
{
int n,i,key;
int *Data;
ElemSN *head=NULL;
printf("Please input the n:");
scanf("%d",&n);//输入数组长度
Data=(int *)malloc(n*sizeof(int));
printf("Please input the Data[i]:");
for(i=0;i<n;i++){//输入数组元素
scanf("%d",Data+i);
}
printf("Please input the key:");
scanf("%d",&key);
head=Createlink(Data,n);//创建双向链表
head=Delkeynode(head,key);
Printlink(head);//输出
free(Data);//释放内存空间
return 0;
}
有一个双向链表,其值不重复,删除关键字为key的结点
最新推荐文章于 2023-08-04 09:12:48 发布