#include<stdio.h>
#include<stdlib.h>
#define N 5
typedef struct node
{
int data;
struct node *next;
}sn;
sn *createlist(int a[])
{
sn *h;
sn *p,*q;
int i;
q=(sn *)malloc(sizeof(sn));
h=q;
for(i=0;i<N;i++)
{
p=(sn *)malloc(sizeof(sn));
p->data=a[i];
q->next=p;
q=p;
}
q->next=0;
return h;
}
void outlist(sn *h)
{
sn *p;
p=h->next;
while(p)
{
printf("%d",p->data);
p=p->next;
}
}
void destroylist(sn *h)
{
sn *p,*q;
p=h->next;
while(p)
{
q=p->next;
free(p);
p=q;
}
free(h);
}
void insert(sn *h,int a,int d)
{
sn *p,*q,*s;
s=(sn *)malloc(sizeof(sn));
s->data=d;
q=h;
p=h->next;
while(p)
{
if(p->data==a) break;
q=p;
p=p->next;
}
s->next=q->next;
q->next=s;
}
void delet(sn *h,int a)
{
sn *p,*q;
q=h;
p=h->next;
while(p)
{
if(p->data==a) break;
q=p;
p=p->next;
}
if(p)
{
q->next=p->next;
free(p);
}
}
main()
{
int a[N]={19,15,12,18,11};
sn *head;
head=createlist(a);
printf("原链表为:\n");
outlist(head);
insert(head,18,17);
printf("\n插入结点后的链表为:\n");
outlist(head);
delet(head,15);
printf("\n删除结点后的链表为:\n");
outlist(head);
printf("\n");
destroylist(head);
}
#include<stdlib.h>
#define N 5
typedef struct node
{
int data;
struct node *next;
}sn;
sn *createlist(int a[])
{
sn *h;
sn *p,*q;
int i;
q=(sn *)malloc(sizeof(sn));
h=q;
for(i=0;i<N;i++)
{
p=(sn *)malloc(sizeof(sn));
p->data=a[i];
q->next=p;
q=p;
}
q->next=0;
return h;
}
void outlist(sn *h)
{
sn *p;
p=h->next;
while(p)
{
printf("%d",p->data);
p=p->next;
}
}
void destroylist(sn *h)
{
sn *p,*q;
p=h->next;
while(p)
{
q=p->next;
free(p);
p=q;
}
free(h);
}
void insert(sn *h,int a,int d)
{
sn *p,*q,*s;
s=(sn *)malloc(sizeof(sn));
s->data=d;
q=h;
p=h->next;
while(p)
{
if(p->data==a) break;
q=p;
p=p->next;
}
s->next=q->next;
q->next=s;
}
void delet(sn *h,int a)
{
sn *p,*q;
q=h;
p=h->next;
while(p)
{
if(p->data==a) break;
q=p;
p=p->next;
}
if(p)
{
q->next=p->next;
free(p);
}
}
main()
{
int a[N]={19,15,12,18,11};
sn *head;
head=createlist(a);
printf("原链表为:\n");
outlist(head);
insert(head,18,17);
printf("\n插入结点后的链表为:\n");
outlist(head);
delet(head,15);
printf("\n删除结点后的链表为:\n");
outlist(head);
printf("\n");
destroylist(head);
}