数据结构-链表的基本操作实现

版权声明:欢迎分享(指明出处),若有错误还请指正!!! https://blog.csdn.net/zj19941201/article/details/70495816
/*主函数*/
#include<stdio.h>
#include<stdlib.h>
#define TRUE        1
#define FALSE       0
#define ERROR       0
#define OK          1
#define OVERFLOW    -2

typedef int status;
typedef int ElemType;

typedef struct LNode{
	ElemType    data;
	struct  LNode  *next;
}LNode,*LinkList;
#include"hsczl.h"
int main()
{
	LinkList head,head1,head2;
	ElemType m,e; 
	ElemType d;
	int n,i,x,o;
	InitList(head);
	InitList(head1);
	InitList(head2);
	printf("h\n");
	printf("输入结点数:");
	scanf("%d",&x);
	CreateList(head,x);
	printList(head);
	printf("h1\n");
	printf("输入结点数:");
	scanf("%d",&o);
	CreateList(head1,o);
	printList(head1);
	printf("输入插入的位置和元素:"); 
	scanf("%d",&i);
	scanf("%d",&m);
	InsertList(head,i,m);
	printList(head);
	printf("输入删除元素位置:");
	scanf("%d",&i);
	DeleteList(head,i);
	printList(head);
	printf("输入要查找的元素:");
	scanf("%d",&e);
	FindList(head,e);
	printf("输入要修改元素的位置:");
	scanf("%d",&n);
	printf("输入修改后的元素:");
	scanf("%d",&d);
	AlterList(head,n,d);
	printList(head);
	MergeList(head2,head,head1);
	printList(head2);
return 0;
}

/*头文件*/
status InitList(LinkList &L)   //链表结点的初始化       
{
	L=(LinkList)malloc(sizeof(LNode));
	L->next=NULL;
	return OK; 
} 

status CreateList(LinkList &L,int j)//头结点插入建表        
{
	LinkList p;
	ElemType e;
	for(int i=j;i>0;--i)
	{
		p=(LinkList)malloc(sizeof(LNode));
		scanf("%d",&e);
		p->data=e;
		p->next=L->next;L->next=p;	
	}
	return OK;
}

void printList(LinkList &L)//打印     
{
	LinkList cur1;
	cur1=L->next;
	while(cur1!=NULL)
	{
		printf("%d\n",cur1->data);
		cur1=cur1->next;
	}

}

status InsertList(LinkList &L,int i,ElemType t)//头 插入   
{
	int j=0,k=1;
	LinkList cur=L,cur2=L;
	while(cur->next!=NULL)
	{
		cur=cur->next;
		j++;
	}
	//free(cur);
	if(i<1||i>j)return ERROR;
	LinkList s;
	s=(LinkList)malloc (sizeof(LNode));
	s->data=t;
	while(k<i)
	{
		cur2=cur2->next;
		k++;
	}
	s->next=cur2->next;
	cur2->next=s;
	return OK;
}

status DeleteList(LinkList &L,int i)//删除     
{
	LinkList Lcur1=L;int j=0,flag=0;
	LinkList Lcur2=L;
	while(j<i)
	{	if(flag==0)
		{
			Lcur1=Lcur1->next;
			j++;flag=1;
		}
		else
		{
			Lcur1=Lcur1->next;
			Lcur2=Lcur2->next;
			j++;
		}	
	}
	Lcur2->next=Lcur1->next;
	free(Lcur1);
	return OK;
}
status FindList(LinkList &L,ElemType e) //查    
{
	LinkList scur;
	scur=L->next;
	int i=1;
	if(scur==NULL)return ERROR;
	while(scur!=NULL)
	{
		if(scur->data==e)
		{printf("%d",i);
		break;
		}
		else
		i++;
		scur=scur->next;	
	} 
	if(scur==NULL)
	printf("不存在!");
	return OK;
} 

status AlterList(LinkList &L,int n,ElemType y)// 改     
{
	LinkList Pcur;
	Pcur=L->next;int i=1;
	if(Pcur==NULL)return ERROR;
	while(i<n)
	{
		Pcur=Pcur->next;
		i++;
	}	
	Pcur->data=y;
	return OK;
}


status MergeList(LinkList &L,LinkList &L1,LinkList &L2) //两个有序链表的归并 
{
	LinkList Lcur;
	LinkList L1cur;
	LinkList L2cur;
	Lcur=L;
	L1cur=L1->next;
	L2cur=L2->next;
	
	while(L1cur!=NULL&&L2cur!=NULL)
	{
		if(L1cur->data>L2cur->data)
		{
			Lcur->next=L1cur;
			Lcur=L1cur;
			L1cur=L1cur->next;
		}
		else
		{
			Lcur->next=L2cur;
			Lcur=L2cur;
			L2cur=L2cur->next;
		}
	}
	Lcur->next=L1cur?L1cur:L2cur;
	return OK; 
} 

猜你喜欢

转载自blog.csdn.net/zj19941201/article/details/70495816