#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *next;
};
void main()
{
int x;
struct node *pt=NULL, *head=NULL, *temp=NULL, *prio=NULL;
while(scanf("%d", &x))
{
pt=(struct node *)malloc(sizeof(struct node));
pt->data=x;
pt->next=NULL;
if(head==NULL)
temp=head=pt;
else
{
temp->next=pt;
temp=temp->next;
}
}
temp=head;
while(temp!=NULL)
{
printf("%d\n", temp->data);
temp=temp->next;
}
fflush(stdin);
prio=temp=head;
printf("请输入删除的数据\n");
scanf("%d", &x);
while(temp!=NULL)
{
if(x==temp->data)
{
if(temp==head)
prio=head=temp->next;
else
prio->next=temp->next;
free(temp);
temp=prio;
break;
}
prio=temp;
temp=temp->next;
}
temp=head;
while(temp!=NULL)//遍历数据
{
printf("%d\n", temp->data);
temp=temp->next;
}
while(head)
{
temp=head;
head=temp->next;
free(temp);
}
}
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *next;
};
void main()
{
int x;
struct node *pt=NULL, *head=NULL, *temp=NULL, *prio=NULL;
while(scanf("%d", &x))
{
pt=(struct node *)malloc(sizeof(struct node));
pt->data=x;
pt->next=NULL;
if(head==NULL)
temp=head=pt;
else
{
temp->next=pt;
temp=temp->next;
}
}
temp=head;
while(temp!=NULL)
{
printf("%d\n", temp->data);
temp=temp->next;
}
fflush(stdin);
prio=temp=head;
printf("请输入删除的数据\n");
scanf("%d", &x);
while(temp!=NULL)
{
if(x==temp->data)
{
if(temp==head)
prio=head=temp->next;
else
prio->next=temp->next;
free(temp);
temp=prio;
break;
}
prio=temp;
temp=temp->next;
}
temp=head;
while(temp!=NULL)//遍历数据
{
printf("%d\n", temp->data);
temp=temp->next;
}
while(head)
{
temp=head;
head=temp->next;
free(temp);
}
}
数据结构对单链表进行数据排序 http://bbs.csdn.net/topics/392201633