链表

#include"stdio.h"
#include"stdlib.h"
#define ERROR 0
#define OK  1


typedef struct node
{
 int  data;
 struct node *next;
}node, *linklist;

void creatfifo(linklist &l,int n)
{
 linklist p,q;
 int i;
 l=(linklist)malloc(sizeof(node));
 l->next=NULL;
 p=l;
 for(i=1;i<=n;i++)
 {
  q=(linklist)malloc(sizeof(node));
  scanf("%d",&q->data);
  p->next=q;
  p=q;
 }
 p->next=NULL;
}

void print(linklist l)
{
 linklist p;
 p=l->next;
 while(p)
 {
  printf("%d ",p->data);
  p=p->next;
 }
 printf("\n");
}

int charu(linklist &l,int i,int e)
{
	int j;
	linklist p,s;
	p=l;
	j=0;
	while(p&&j<i-1){p=p->next;++j;}
	if(!(p->next)||j>i-1)return ERROR;
	s=(linklist)malloc(sizeof(node));
	s->data=e;s->next=p->next;
	p->next=s;
	return OK;
}

int shanchu(linklist &l,int i,int &e)
{
	int j;
	linklist p,q;
	p=l;j=0;
	while(p->next&&j<i-1){p=p->next;++j;}
	if(!(p->next)||j>i-1)return ERROR;
	q=p->next;p->next=q->next;
	e=q->data;free(q);
	return OK;
}


int hebing(linklist &l,linklist &k,linklist &h)
{
	linklist  l1,k1,h1;
	l1=l->next;k1=k->next;
	h=h1=l;
	while(l1&&k1){
		if(l1->data<=k1->data){h1->next=l1;h1=l1;l1=l1->next;
		}  
		else{h1->next=k1;h1=k1;k1=k1->next;}
	}
	h1->next=l1?k1:h1;
	free(k);
	return 0;
} 

int main()
{
	linklist l,k,h;
	int n,e,i,m;
	scanf("%d",&n);
	creatfifo(l,n);
	print(l);
	printf("请输入在第几个位置插入100\n");
	e=100;
	scanf("%d",&i);
	charu(l,i,e);
	print(l);
    scanf("%d",&i);
	shanchu(l,i,e);
	print(l);
	printf("请输入第二个链表的个数\n");
	scanf("%d",&m);
	creatfifo(k,m);
	print(k);
	hebing(l,k,h);
	print(l);
}

  

posted @ 2013-10-30 14:12  博园少主  阅读(178)  评论(0编辑  收藏  举报