链表的操作实例

#include "stdio.h"
#include "conio.h"
#include "string.h"
#include "malloc.h"
#include "stdlib.h"
struct node
{int data;
struct node *next;
};
struct node*head=NULL;
struct node *insert(struct node *head,int value)
{
struct node *n,*p,*q;
n=(struct node *)malloc(sizeof(struct node));
printf("\n qing shu ru yao cha ru de shu zhi:");
scanf("%d",&value);

n->data=value;
p=head;
if(head==NULL)
{head=n;
n->next=NULL;}
else
{ while((p->next!=NULL)&&(p->data{q=p;
p=p->next;}
if(p->data>=value)
{if(head==p)
{n->next=head;
head=n;}
else
{q->next=n;
n->next=p;}}
else
{p->next=n;
n->next=NULL;}
return head; } }
struct node *create()
{struct node *head,*tail,*p;
int x;
head=tail=NULL;
printf("\nqing shu ru yi ge zheng shu:");
getch();
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("qing shu ru yi ge zheng shu:");
getch();
scanf("%d",&x);
}
return head;}
void show(struct node*head)
{
printf("\n");
getch();
printf("shu ju xin xi ru xia:\n");
getch();
while(head)
{
printf("shu ju: %d",head->data);
getch();
head=head->next;
}
}
void main()
{struct node *q;
struct node *head=NULL;
int value;

head=create();
q=insert(head,value);
show(head);

}

相关文档
最新文档