#include
#include
struct node
{
int data;
struct node *next;
};
typedef struct node NODE;
NODE *ApplyNODE(int x)
{
NODE *p;
p=(NODE *) malloc (sizeof(NODE));
p->data=x;
p->next=NULL;
return(p);
}
NODE *InitList()
{
NODE *head;
head=(NODE *) malloc (sizeof(NODE));
head->next=NULL;
return(head);
}
NODE *creat(int n)//创建链表
{
NODE *head,*p,*q;
int i,m,x;
printf("\nInput the length of the line:");
scanf("%d",&m);
head=q=InitList();
printf("\nInput %d datas:",m);
head=p=q=(NODE *)malloc(sizeof(NODE));//创建节点
for(i=0;i
{
if(q->next!=NULL)
{
scanf("%d",&x);
p=ApplyNODE(x);
q->next=p;//q->next保存p,q指向最后一个节点
q=p;
p=(NODE *)malloc(sizeof(NODE));//创建下一个节点
}
}
q->next=NULL;
return head;
}
NODE *search(NODE *head,int x)//查找数据
{
NODE *p=head;
p=head->next;
while((p!=NULL)&&(p->data!=x))
p=p->next;
return(p);
}
NODE *delete(NODE *head, int x)//删除节点
{
NODE *q;
q=head;
while(q->next->data!=x)
q=q->next;
q->next=q->next->next;
return(head);
}
void Display(NODE *head)
{
NODE *p;
printf("\nThe line are:");
p=head->next;
while(p!=head)
{
printf("%d",p->data);
p=p->next;
}
}
main()
{
int n,x;
NODE *head;
head=creat(n);
head=delete(head,x);
Display(head);
search(head,x);
}