#include<stdio.h>
#include<stdlib.h>
struct Node{
int data;
struct Node *next;
};
typedef struct Node *node;
node create(node list,int x)
{
node newnode,t;
newnode=(struct Node*)malloc(sizeof(struct Node));
newnode->data=x;
newnode->next=NULL;
t=list;
while(t->next!=NULL)
{
t=t->next;
}
t->next=newnode;
return list;
}
node insert(node list,int x)
{
node prenode,newnode;
newnode=(struct Node*)malloc(sizeof(struct Node));
newnode->data=x;
newnode->next=NULL;
prenode=list;
while(prenode->next!=NULL)
{
if(prenode->next->data>x)
{
newnode->next=prenode->next;
prenode->next=newnode;
break;
}
prenode=prenode->next;
}
return list;
}
node deleteNode(node list,int x)
{
node p,temp;
p=list;
while(p->next!=NULL)
{
if(p->next->data==x)
{
temp=p->next;
p->next=p->next->next;
free(temp);
break;
}
p=p->next;
}
return list;
}
int main()
{
node list;
int n,i,x;
list=(struct Node*)malloc(sizeof(struct Node));
list->next=NULL;
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d",&x);
list=create(list,x);
}
list=insert(list,0);
list=deleteNode(list,3);
while(list)
{
list=list->next;
printf("%d,",list->data);
}
return 0;
}
(自用)带头节点单链表(C语言)
最新推荐文章于 2024-07-25 12:29:37 发布