#include
#include
struct node{
int data;
struct node *next;
};
struct node *creat_linklist()
{
struct node *head ,*tail,*p;int x;
head=tail=NULL;
printf("\n请输入一个整数: ");
scanf("%d",&x);
while(x!=0)
{
p=(struct node *)malloc(sizeof(struct node));
p->data=x;
p->next=NULL;
if(head==NULL)head=tail=p;
else
{
tail->next=p;
tail=p;
}
printf("请输入一个整数: ");
scanf("%d",&x);
}
return head;
}
//查找
int find(struct LNode **p,int i) {
int j=1;
struct LNode *q=*p;
while (j {
q=q->next;j++;
}
if (q!=NULL) /**//*找到了第i个结点*/
return(q->data);
else
{
printf("位置参数不正确!\n");
return NULL;
}
}
//插入
struct node *insert(struct node *head,int value)
{
struct node *newp,*p,*q;
newp=(struct node *)malloc(sizeof(struct node));
newp->data=value;
p=head;
if(head==NULL){head=newp; newp->next=NULL;}
else
{
while((p->next !=NULL) && (p->data{q=p; p=p->next;}
if(p->data>=value)
{ if(head==p){newp->next=head;
head=newp;}
else
{
q->next=newp;
newp->next=p;
}
}
else
{
p->next=newp;
newp->next=NULL;
}
}
return head;
}
//链表的删除
struct node *delete_link(struct node *head ,int value)
{
struct node *p,*q;
p=head;
if(head==NULL)
{
printf("这是一个空链表!\n");
return head;
}
while((p->next!=NULL)&&(p->data!=value))
{
q=p;p=p->next;
}
if(value==p->data)
{
if(head==p)head=p->next;
else q->next=p->next;
free(p);
}
else
printf("此链表无%d!\n",value);
return head;
}
/*输出链表*/
void PrintList(struct node *head)
{
struct node *p;
p=head;
do
{
printf("%d\n",p->data);
p=p->next;
}
while(p!=NULL);
}
/*链表的销毁*/
void destroy_link(struct node *L )
{
struct node *q=NULL;
struct node *p=L;
while(p)
{
q=p->next;
free(p);
p=q;
}
}
/*主函数*/
int main()
{
int value;
struct node *head;
head=creat_linklist();
printf("\n");
PrintList(head);
printf("请输入一个要添加的整数: ");
scanf("%d",&value);
head=insert(head,value);
PrintList(head);
printf("\n");
printf("请输入一个要删除的整数: ");
scanf("%d",&value);
head=delete_link(head,value);
PrintList(head);
printf("\n");
destroy_link(head);
system("pause");
return 0;
}
最基本的你写的也不对
o no 这中是最基本的东西了,你还要来问别人