数据结构之线性表的链式表示及其实现

本文分享了使用C语言实现的线性表链式表示及其基本操作,包括创建、排序、遍历等,并通过实际代码演示了如何进行元素的插入、删除等常见操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

记录一下自己学习过程中写的代码。以下是我看严蔚敏老师的数据结构教材后,结合教材所讲用C语言实现了关于线性表链式表示及其实现的基本操作,供以后复习所用。 
编译软件:VC++6.0 

测试用例结果截图如下:


源代码如下:


/********************************** 
线性表的链式表示和实现(完整代码,C实现)
Author:大地在我腳下 
Date:2016-7-25
Email:jsrcdjcyy@163.com 
**********************************/  

#include<stdio.h>  
#include<stdlib.h>  
  
  
typedef struct LNode  
{  
 int data;  
 struct LNode *next;  
}LNode, *LinkList;  

LinkList CreateList(int);      //初始化一个单链表  
void ClearList(LinkList);    //清除单链表 
void DestroyList(LinkList);//线性表L已存在情况下,执行销毁线性表L操作    
int LengthList(LinkList);   //求单链表长度     
bool EmptyList(LinkList);      //判断单链表是否为空表        
int GetList(LinkList,int);  //取单链表第某个位置上的元素    
bool InsertList(LinkList,int,int);//向单链表插入一个元素        
bool DeleteList(LinkList,int);//从单链表中删除一个元素
void TraverseList(LinkList);    //遍历单链表并输出  
void BubsortList(LinkList);    //对链表内数据进行冒泡排序

void main()
{int n,len,m,h,k,t;
 LinkList LHead=NULL;

//创建链表,排序并遍历输出
 printf("Please input the original number of the linear list:"); //请输入线性表元素的原始个数
 scanf("%d",&n);
 printf("\n");
 LHead=CreateList(n);
 BubsortList(LHead);
 printf("After sorting and traversing,now the datas in the list are:\n"); //经过排序后遍历输出链表数据为
 TraverseList(LHead);

//输出链表的原始长度
len=LengthList(LHead);
if(!EmptyList(LHead))
   printf("Now the current length of the list is:%d\n",len);
printf("\n");

//取单链表第某个位置上的元素 
printf("Please input the location of the element which should be taken out:"); //请输入要取出的元素的位置
scanf("%d",&m);
if(m>n) 
{ printf("Input error!please check your input!");//所要查找的元素位置不存在,请检查输入!
  printf("\n");
  printf("Please input the location again:"); //请输入要取出的元素的位置
  scanf("%d",&m);
}
printf("\n");
printf("Congratulations!input correct!\n");
printf("The element which should be taken out is:%d\n",GetList(LHead,m)); //要取出的元素值为
printf("\n");

//向链表中插入数据,插入后输出当前链表长度并遍历输出链表数据
printf("Please input the being inserted  number and its location:"); //请输入要插入的位置和要插入的数
scanf("%d%d",&h,&k);
printf("\n");
if(InsertList(LHead,h,k))
    printf("Insert succeed!\n");
else  printf("Insert failed!\n");
printf("\n");
printf("Now the datas in the list are:\n"); 
TraverseList(LHead);
len=LengthList(LHead);
if(!EmptyList(LHead))
   printf("Now the current length of the list is:%d\n",len);
printf("\n");

//从链表中删除数据,并重新遍历输出链表数据及其长度
printf("Please input the element's location which must be deleted:"); //请输入要删除的位置
scanf("%d",&t);
printf("\n");
if(DeleteList(LHead,t))
    printf("Delete succeed!\n");
else  printf("Delete failed!\n");
printf("\n");
printf("Now the datas in the list are:\n"); 
TraverseList(LHead);  
len=LengthList(LHead);
if(!EmptyList(LHead))
   printf("Now the current length of the list is:%d\n",len);

//判断单链表是否为空表 
if(!EmptyList(LHead)) printf("Now the list has numbers!\n");
else printf("Now the list is empty!\n");

//清空链表,遍历后输出(实际上并无数据输出) 
ClearList(LHead);
printf("\nAfter clearing,the list is:");
TraverseList(LHead);
printf("\n");

//判断单链表是否为空表 
if(!EmptyList(LHead)) printf("Now the list has numbers!");
else printf("Now the list is empty!");
printf("\n");
}

//逆序输入n个元素值,建立带头结点的单链线性表L并返回头指针
LinkList CreateList(int n)
{LinkList LHead,p;int i;
LHead=(LinkList)malloc(sizeof(LNode));
LHead->next=NULL;    //先建立一个带头结点的单链表
if(LHead==NULL)
{printf("Malloc failed!");
 exit(-1);
}
printf("Please input the linear list:");//请输入链表各结点值
for(i=n;i>0;--i)
   { p=(LinkList)malloc(sizeof(LNode));  //生成新结点
     if(p==NULL)
       {printf("Malloc failed!");
        exit(-1);
        }
     scanf("%d",&p->data);
     p->next=LHead->next;
     LHead->next=p;   
   }
printf("\n");
return LHead;
}  

//清除单链表 
void ClearList(LinkList L)
{LinkList p=L->next;
 L->next=NULL; //头指针指针域为空
 DestroyList(p); //销毁p所指的单链表
}

//线性表L已存在情况下,执行销毁线性表L操作  
void DestroyList(LinkList L)
{LinkList q;
 while(L)   //当L非空的时候,一直执行循环操作
   {q=L->next;
    free(L);
    L=q;
   }
}

//求单链表长度  
int LengthList(LinkList L)    
{LinkList q=L->next;
 int count=0;
 while(q)   //当L非空的时候,一直执行循环操作
   {
    count++;
	q=q->next;
   }
return  count;
}

//判断单链表是否为空表  
bool EmptyList(LinkList L)   
{if(L->next)
 return  false;
 else return  true;
}

//取单链表第某个位置上的元素 
int GetList(LinkList L,int pos)   
{LinkList  p=L->next;int j=1;
while(p&&j<pos)
   {p=p->next;
    j++;
   }
if(!p||j>pos) return 0;
return p->data;
}

//向单链表某处插入一个元素 
bool InsertList(LinkList L,int e,int pos) 
{LinkList  q,p=L->next;int i=1;
if(pos>LengthList(L)) 
     {printf("Input error!please check the input again!");//所要查找的元素位置不存在,请检查输入!
      return false;
     }
while(p&&i<pos-1)
   {p=p->next;
    i++;
   }
if(!p||i>pos)   return false;
q=(LinkList)malloc(sizeof(LNode));
if(q==NULL)
{printf("Malloc failed!");
 exit(-1);
}
q->data=e;
q->next=p->next;
p->next=q;
return true;
}

//从单链表中删除某位置元素
bool DeleteList(LinkList L,int pos)  
{LinkList  q,p=L->next;int i=1;
if(pos>LengthList(L)) 
     {printf("Input error!please check the input again!");//所要查找的元素位置不存在,请检查输入!
      return false;
     }
while(p&&i<pos-1)
   {p=p->next;
    i++;
   }
if(!p||i>pos)   return false;
q=p->next;
p->next=p->next->next;
free(q);
q=NULL;
return true;
}

//遍历单链表并输出  
void TraverseList(LinkList L)  
{LinkList p=L->next;
 while(p)
   {printf("%d",p->data);
    p=p->next;
	putchar(32);
   }
printf("\n");
}

//对链表内数据进行冒泡排序
void BubsortList(LinkList  L)    
{LinkList  p,q;int temp;
 for(p=L->next;p;p=p->next)
	 for(q=p->next;q;q=q->next)
		 if(p->data>q->data)
		 {temp=p->data;
		  p->data=q->data;
          q->data=temp;
		 }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值