数据结构(c语言版)链表的实现以及合并

本文详细介绍了如何使用C语言实现链表的数据结构,并探讨了如何高效地合并两个链表。通过实例代码和程序运行截图,深入理解链表操作的本质。
摘要由CSDN通过智能技术生成
/* Note:Your choice is C IDE */
#include "stdio.h"
#include"malloc.h"
#include"stdlib.h"//包含exit()函数的头文件,以及system("pause")
#define OK 1
#define error 0
#define OVERFLOW -1
typedef int Status;
typedef int ElemType;
/*-------------struct------------*/
typedef struct LNode{
	ElemType data;
	struct LNode *next;
}LNode ,*linklist;
/*-------------Init------------*/
Status InitList_L(linklist *L){
	(*L)=(linklist)malloc(sizeof(LNode));
	if(!*L)exit(OVERFLOW);
	(*L)->next=NULL;
	return OK;
}
/*-------------Insert------------*/
Status ListInsert_L(linklist *L,int i,ElemType e){
	linklist p,p1;
	int j=0;
	p1=*L;
	while(p1&&j<i-1){
		p1=p1->next;
		++j;
	}
	if(!p1||j>i-1)return error;
	p=(linklist)malloc(sizeof(LNode));
	if(!p)exit(OVERFLOW);
	p->data=e;
	p->next=p1->next;
	p1->next=p;
	return OK;
}
/*-------------delete------------*/
Status ListDelete_L(linklist *L,int i,ElemType *e){
	linklist p,q;
	int j=0;
	p=*L;
	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;
}
/*-------------merge------------*/
Status MergeList_L(linklist *L1,linklist *L2,linklist *L3)
{
	linklist p3,p1,p2;
	p1=(*L1)->next;
	p2=(*L2)->next;
	*L3=p3=*L1;	
	while(p1&&p2){
		if(p1->data<p2->data){
			p3->next=p1;
			p3=p1;
			p1=p1->next;
		}
		else{
			p3->next=p2;
			p3=p2;
			p2=p2->next;
		}
	}
	p3->next=p1?p1:p2;
	free(*L2);
	return OK;
}
int main()
{
	int i,e,n;
    linklist L1,L2,L3,p;
    if(InitList_L(&L1))printf("Init L1 is ok.\n");
    if(InitList_L(&L2))printf("Init L2 is ok.\n");
    printf("Please enter a value for n of L1:\n");
    scanf("%d",&n);
    printf("Please enter n values for L1:\n");
    for(i=1;i<=n;i++)
    {
  	   scanf("%d",&e);
  	   if(ListInsert_L(&L1,i,e)!=OK) break;
    }
    printf("The current elements of L1 are:\n");
    p=L1->next;
	printf ("L1->");
    for(i=1;i<=n;i++){
  	   printf("%d—>",p->data);
  	   p=p->next;
    }
    printf("NULL\n");
    printf("Please enter a value for n of L2:\n");
    scanf("%d",&n);
    printf("Please enter n values for L2:\n");
    for(i=1;i<=n;i++)
    {
  	   scanf("%d",&e);
  	   if(ListInsert_L(&L2,i,e)!=OK) break;
    }
    printf("The current elements of L2 are:\n");
    p=L2->next;
	printf ("L2->");
    while(p){
    	printf("%d—>",p->data);
  	    p=p->next;
    }
    printf("NULL\n");
    printf("下面进入合并操作:\n");
    MergeList_L(&L1,&L2,&L3);
    printf("The current elements of L3 are:\n");
    p=L3->next;
	printf ("L3->");
    while(p){
    	printf("%d—>",p->data);
  	    p=p->next;
    }
    printf("NULL\n");
    printf("下面进入删除操作:\n");
    printf("Please enter a value for i:\n");
    scanf("%d",&i);
    if(ListDelete_L(&L1,i,&e))
        printf("The deleted elem is :%d\n",e);
    else printf("删除失败\n");
	printf("The current elements of L3 are:\n");
    p=L3->next;
	printf ("L3->");
    while(p){
    	printf("%d—>",p->data);
  	    p=p->next;
    }
    printf("NULL\n");
    system("pause");
    return 0;
}


程序运行截图

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值