单链表

单链表

包括归并两个有序链表到一个有序链表

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

typedef int ElemType;

typedef struct LNode
{
	ElemType data;
	struct LNode *next;
}List,*LNodeList;


ElemType GetElem(LNodeList &L,int num)
{
	LNodeList p=L;
	int i=0;
	while(p!=NULL && i<num)
	{
		p=p->next;
		i++;
	}
	return p->data;
}

int GetIndex(LNodeList &L,ElemType x)
{
	LNodeList p;
	int i=0;
	p=L->next;	
	while(p!=NULL && p->data!=x)
	{
		p=p->next;
		i++;
	}
	if(p->data==x)
		return i+1;
	else
		printf("cannot find this value int the list! \n");
		exit(0);	
}



void InsertList(LNodeList &L,int index,ElemType data)
{
	LNodeList p=L;
	int i=0;
	while(p!=NULL && i<index-1)
	{
		p=p->next;
		i++;
	}
	if(p==NULL)
	{
		printf("p==NULL!!");
		exit(0);		
	}		
	if(!p || i>index-1)
	{
		printf("the index out of length of list!!");
		exit(0);		
	}
	LNodeList s=(LNodeList)malloc(sizeof(LNode));
	s->data=data;
	s->next=p->next;
	p->next=s;
}


void DeleteList(LNodeList &L,int index)
{
	LNodeList p=L;
	int i=0;
	while(p!=NULL && i<index-1)
	{
		p=p->next;
		i++;	
	}	
	if(!p || i>index-1)
	{
		printf("the index out of length of list!!");
		exit(0);
	}
	LNodeList s=p->next;
	p->next=s->next;
	free(s);
}


void MergeList(LNodeList &La,LNodeList &Lb,LNodeList &Lc)
{
	LNodeList pa,pb,pc;
	pa=La->next;
	pb=Lb->next;
	pc=Lc=Lb;
	while(pa && pb)
	{
		if(pa->data <= pb->data)
		{
			pc->next=pa;
			pc=pa;		
			pa=pa->next;
		}
		else
		{		
			pc->next=pb;
			pc=pb;			
			pb=pb->next;
		}
	}
	pc->next=pa?pa:pb;
	free(Lb);
	free(La);	
}


void CreatList(LNodeList &L,int num)
{
	LNodeList p;
	srand(time(NULL));
	L=(LNodeList)malloc(sizeof(LNode));
	L->next=NULL;
	for(int i=0;i<num;i++)
	{
		p=(LNodeList)malloc(sizeof(LNode));
		p->data=rand()%100;
		p->next=L->next;
		L->next=p;
	}
}


void CreatListArray(LNodeList &L,int array[],int num)
{
	LNodeList p;
	L=(LNodeList)malloc(sizeof(LNode));
	L->next=NULL;
	for(int i=0;i<num;i++)
	{
		p=(LNodeList)malloc(sizeof(LNode));
		p->data=array[i];
		p->next=L->next;
		L->next=p;
	}
}


void PrintList(LNodeList L)
{
	LNodeList p;
	p=L->next;
	printf("Show all list element:");
	while(p)
	{
	printf("%d ",p->data);
	p=p->next;
	}
	printf("\n");
}


int main(void)
{
	int a[5]={9,7,5,3,1};
	int b[5]={10,8,6,4,2};
	LNodeList L;
	CreatListArray(L,a,5);
	PrintList(L);
	LNodeList L2;
	CreatListArray(L2,b,5);
	PrintList(L2);
	LNodeList L3;	
	MergeList(L,L2,L3);
	PrintList(L3);	

	
	int i,j;
	
	printf("please input the index that you want to find:");
	scanf("%d",&i);
	printf("the %d index value is %d \n",i,GetElem(L3,i));	
	printf("please input the value that you want to find:");	
	scanf("%d",&i);
	printf("the %d index value is %d \n",i,GetIndex(L3,i));
	
	printf("please input the index and value that you want to insert:");	
	scanf("%d %d",&i,&j);
	InsertList(L3,i,j);
	PrintList(L3);
	printf("please input the index that you want to delete:");	
	scanf("%d",&i);	
	DeleteList(L3,i);
	PrintList(L3);	
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值