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

#include<stdio.h>
#include<stdlib.h>
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<i-1)
{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<i)
{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 mian()//主函数
{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);
}

语法错误已经全部改了,插入算法还有逻辑错误,现在没时间修改了
#include<stdio.h>
#include<stdlib.h>
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<i-1)
{
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<i)
{
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);
}追问

很感谢,如果可以的话能帮我把逻辑错误也该下吗?
等你有空时。谢谢了

温馨提示:答案为网友推荐,仅供参考
第1个回答  2011-05-06
#include <stdio.h>
#include <malloc.h>
#include <string.h>
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;
}
看不懂找我吧
另外,站长团上有产品团购,便宜有保证追问

谢谢

第2个回答  2011-05-05
你不是已经写出来了么?
第3个回答  2011-05-05
同学,你的问题很不清楚,说清楚了我们好回答!追问

不好意思,有些话没讲。
我编译时不通过,有一些很奇怪的问题,我“delete”函数明明已经声明了,编译时说我没声明,
我不知道自己错在哪里,编译器那个有些看不懂,麻烦各位了。

相似回答