在双向链表中实现插入和删除,用c编程实现

2024-12-28 22:01:42
推荐回答(4个)
回答1:

语法错误已经全部改了,插入算法还有逻辑错误,现在没时间修改了
#include
#include
typedef struct DULNODE{
int data;
struct DULNODE *priou;
struct DULNODE *next;
}DULNODE,*Dulinklist;
Dulinklist creat(Dulinklist L)//创建
{
int node;
Dulinklist p;
L=(Dulinklist)malloc(sizeof(DULNODE));
L->next=NULL;
L->priou=NULL;
printf("\nplease input the node(end with 0)");
scanf("%d",&node);
while(node!=0)
{
p=(Dulinklist)malloc(sizeof(DULNODE));
p->data=node;
p->next=L->next;
L->next=p;
p->priou=L;
printf("\nplease input the node(end with 0):");
scanf("%d",&node);
}
return L;
}
Dulinklist insert(Dulinklist L,int i,int x)//插入函数
{
int j;
Dulinklist p,s;
p=L;j=0;
while(p!=NULL&&j {
p=p->next;
++j;
}
if(!(s=(Dulinklist)malloc(sizeof(DULNODE))))
printf("\nerror\n");
s->data=x;
p->next=s->next;
s->next=p->next;
s->next->priou=s->priou;
p->next=s;
return L;
}
Dulinklist Delete(Dulinklist L,int i)
{
int j,x;
Dulinklist p;
p=L,j=0;
while(p->next!=NULL&&j {
p=p->next;
++j;
}
x=p->data;
p->priou->next=p->next;
p->next->priou=p->priou;
printf("the delete value is %d",x);
free(p);
return L;
}
void display(Dulinklist L)//输出
{
Dulinklist p;
p=L->next;
while(p!=NULL)
{
printf("%d",p->data);
p=p->next;
printf("\n");
}
}
int main()//主函数
{
int i,x;
Dulinklist L;
L=creat(L);
display(L);
printf("\n please input the position you want to insert:");
scanf("%d",&i);
printf("\ninput the node you want to insert:");
scanf("%d",&x);
L=insert(L,i,x);
display(L);
printf("\nplease inout the node position you want to delete:");
scanf("%d",&i);
L=Delete(L,i);
display(L);
}

回答2:

#include
#include
#include
struct student
{
int num;
char sex;
student *next;
};
#define size sizeof(student)
void main()
{
student *del(student *head,int num);
student *head,*p1,*p2;
p1=( student *)malloc(size);
scanf("%d,%c",&p1->num,&p1->sex);
head=p1;
p2=p1;
for(int i=0;i<2;i++)
{
p1=(student *)malloc(size);
scanf("%d,%c",&p1->num,&p1->sex);
p2->next=p1;
p2=p1;
}
p1->next=0;
p1=del(head,3);
do
{
printf("%d,%c",p1->num,p1->sex);
p1=p1->next;
printf("\n");
}while(p1->next!=0);
printf("%d,%c",p1->num,p1->sex);
getchar();
getchar();
}

student *del(student *head,int num)
{
student *p1,*p2;
p1=head;
while(p1->num!=num&&p1->next!=0)
{
p2=p1;
p1=p1->next;
}
if(p1->num==num)
{
if(p1==head)
{
head=p1->next;
}
else
if(p1->next==0)
{
p2->next=0;
}
else
{
p2->next=p1->next;
}

}
return head;
}
看不懂找我吧
另外,站长团上有产品团购,便宜有保证

回答3:

你不是已经写出来了么?

回答4:

同学,你的问题很不清楚,说清楚了我们好回答!