数据结构 - 单链表

先讲讲原理,
单链表这个东西就是 一个结构体里面存着一个数据域再加上一个指针域
然后呢
一个数据指向另一个数据,另一个数据再指向下一个数据……
也就是数据和数据之间由指针决定链的走向,所以叫单链表(我胡诌的)

typedef struct LNode
{
	int data;		
	struct LNode *next; 
}LNode;

然后前面讲到,数据结构是存放数据的容器,可以这么认为
那么我们把数据放到单链表里,有什么意义呢,能做什么呢,这是我们应该考虑的

很显然,数据放进去,要读的出来
(人话:你把数据给了单链表之后,你也要能看到单链表里存的数据,不能你给了他数据你就不知道单链表里有什么惹)所以第一个功能,打印单链表里的数据

ok,但是在这之前,单链表先要存在对吧

所以第一步,让单链表存在,即定义(或者将初始化/创建)单链表
我们把它包成函数就是createlist

LNode *CreateList(int len)
{
	LNode *L = (LNode*)malloc(sizeof(LNode)); 
	LNode *temp = L;
	temp->next = NULL;
	for(int i=1;i<=len;i++) 
	{
		LNode *p = (LNode*)malloc(sizeof(LNode)); 
		scanf("%d",&p->data); 
		temp->next = p;   
		temp = p;		
	}
	temp->next = NULL;
	
	return (LNode*)L;
}

然后,定义完之后我们就可以打印表里的数据出来看惹,就是printlist

void PrintfList(LNode *L)
{
	LNode *temp = L;
	int count = 0;
	printf("List:\n");
	while(temp->next)
	{
		temp = temp->next;
		printf("%d ,",temp->data);
		count++;
	}
	printf("\n");
}

接着,数据打出来之后就可能出现需要修改数据的情况
那么就是 添加/删除/(高级一点结合两者的操作就是)替换
好der
在完成这些操作之前
你首先要想这些简单的操作怎么做到的,不管添加数据删除数据还是替换数据你都要先找到数据对伐,那么问题就来了

怎么找- -> 遍历找

找不到怎么办- -> 提示错误信息,比如数据以及遍历完了还是找不到你要找的数据,再比如你找的数据本来就是不会存在在这个单链表里面的(强人所难系列)

找到了返回什么才让添加删除和替换的操作变得最方便- -> 把数据的下标输出来给相应的操作,然后在此基础上进行数据更改(我真是个小机灵鬼)

好呗,因为这三种操作都要用到查找数据所以为了减少代码冗余,我们也把它变成一个函数search

int Serch(LNode *L, int elem)
{
	LNode *temp = L;
	int pos = 0;
	int i = 1;
	while(temp->next)
	{
		temp = temp->next;
		if(elem==temp->data)
		{
			pos = i;
			printf("The index of this number is %d\n",pos);
			return pos;	
		}
		i++;
	}
	printf("Serch error!\n");
	
	return ERROR;
}

----完了之后是不是就可以进行三种操作惹》insert》delete》replace》
----是!!
(然而我还是直接遍历的,有点傻,这东西我还真的是在写博客的时候才想到把这个函数单独—拎出来,氦,好蠢)

LNode *Insert(LNode *L, int pos, int elem)
{
	LNode *temp = L;
	int i = 0;
	while( (temp!=NULL)&&(i<pos-1) )
	{
		temp = temp->next;
		++i;
	}
	if( (temp==NULL)||(i>pos-1) )		
	{
		printf("ERROR!\n");
		return (LNode*)temp;
	}
	LNode *newl = (LNode*)malloc(sizeof(LNode));	
	newl->data = elem;	
	newl->next = temp->next; 
	temp->next = newl;	
	
	return (LNode*)L;
}
 
LNode *Delete(LNode *L, int pos, int *elem)
{
	LNode *temp = L;
	int i = 0;
	while( (temp!=NULL)&&(i<pos-1) )
	{
		temp = temp->next;
		++i;
	}
	if( (temp==NULL)||(i>pos-1) )
	{
		printf("ERROR!\n");
		return (LNode*)temp;
	}
	LNode *del = temp->next;
	*elem = del->data;		
	temp->next = del->next;	
	free(del);		
	del = NULL;			
	
	return (LNode*)L;		
}

LNode *Replace(LNode *L, int pos, int elem)
{
	LNode *temp = L;
	temp = temp->next;
	for(int i=1;i<pos;i++)
	{
		temp = temp->next;
	}
	temp->data = elem;  
	return (LNode*)L;   
}

(这个是我10月13日还是什么时候就打了的代码,当时写在博客的话)
先把今天上课打的单链表的系类操作放上来,附带有测试,

参考另一个博主代码打的
(这个测试代码真的有点意思,学习)
传送门:https://blog.csdn.net/hhhhhyyyyy8/article/details/81027728

#include <stdio.h>
#include <stdlib.h>
 
#define ERROR 1
#define OK 	  0
typedef struct LNode
{
	int data;		
	struct LNode *next; 
}LNode;

LNode *CreateList(int len)
{
	LNode *L = (LNode*)malloc(sizeof(LNode)); 
	LNode *temp = L;
	temp->next = NULL;
	for(int i=1;i<=len;i++) 
	{
		LNode *p = (LNode*)malloc(sizeof(LNode)); 
		scanf("%d",&p->data); 
		temp->next = p;   
		temp = p;		
	}
	temp->next = NULL;
	
	return (LNode*)L;
}
 
int Serch(LNode *L, int elem)
{
	LNode *temp = L;
	int pos = 0;
	int i = 1;
	while(temp->next)
	{
		temp = temp->next;
		if(elem==temp->data)
		{
			pos = i;
			printf("The index of this number is %d\n",pos);
			return pos;	
		}
		i++;
	}
	printf("Serch error!\n");
	
	return ERROR;
}

LNode *Replace(LNode *L, int pos, int elem)
{
	LNode *temp = L;
	temp = temp->next;
	for(int i=1;i<pos;i++)
	{
		temp = temp->next;
	}
	temp->data = elem;  
	return (LNode*)L;   
}
 
LNode *Insert(LNode *L, int pos, int elem)
{
	LNode *temp = L;
	int i = 0;
	while( (temp!=NULL)&&(i<pos-1) )
	{
		temp = temp->next;
		++i;
	}
	if( (temp==NULL)||(i>pos-1) )		
	{
		printf("ERROR!\n");
		return (LNode*)temp;
	}
	LNode *newl = (LNode*)malloc(sizeof(LNode));	
	newl->data = elem;	
	newl->next = temp->next; 
	temp->next = newl;	
	
	return (LNode*)L;
}
 
LNode *Delete(LNode *L, int pos, int *elem)
{
	LNode *temp = L;
	int i = 0;
	while( (temp!=NULL)&&(i<pos-1) )
	{
		temp = temp->next;
		++i;
	}
	if( (temp==NULL)||(i>pos-1) )
	{
		printf("ERROR!\n");
		return (LNode*)temp;
	}
	LNode *del = temp->next;
	*elem = del->data;		
	temp->next = del->next;	
	free(del);		
	del = NULL;			
	
	return (LNode*)L;		
}
 
void PrintfList(LNode *L)
{
	LNode *temp = L;
	int count = 0;
	printf("List:\n");
	while(temp->next)
	{
		temp = temp->next;
		printf("%d ,",temp->data);
		count++;
	}
	printf("\n");
}
 
int MenuSelect(void)
{
	int cmd;
	printf("1.Serch test\n");
	printf("2.Replace test\n");
	printf("3.Insert test\n");
	printf("4.Delete test\n");
	do
	{
		printf("Enter your choice: ");
		scanf("%d",&cmd);
	}while(cmd<0||cmd>4);
	return cmd;
}

void Test1(LNode *L)	
{
	int serchElem = 0;		
	printf("--start--\n");
	PrintfList(L);
	printf("Input the element you want to serch:");
	scanf("%d",&serchElem);
	Serch(L,serchElem);
	printf("--end--\n");
	printf("\n");
}
 
void Test2(LNode *L)	
{
	int replacePos = 0, replaceElem = 0; 
	printf("--start--\n");
	PrintfList(L);
	printf("Input the position and the element you want to replace(example:10,33):");
	scanf("%d,%d",&replacePos,&replaceElem);
	L = Replace(L,replacePos,replaceElem);
	printf("After replace,list is:\n");
	PrintfList(L);
	printf("--end--\n");
	printf("\n");
}
 
void Test3(LNode *L)	
{
	int insertPos = 0, insertElem = 0;
	
	printf("--start--\n");
	PrintfList(L);
	printf("Input the position and the element you want insert(example:10,33):");
	scanf("%d,%d",&insertPos,&insertElem);
	L = Insert(L,insertPos,insertElem);
	printf("After insert,list is:\n");
	PrintfList(L);
	printf("--end--\n");
	printf("\n");
}
 
void Test4(LNode *L)	
{
	int deletePos = 0;
	int elem = NULL;
	
	printf("--start--\n");
	PrintfList(L);
	printf("Input the position of the element you want to delete(example:10):");
	scanf("%d",&deletePos);
	L = Delete(L,deletePos,&elem);
	printf("After delete,list is:\n");
	PrintfList(L);
	printf("--end--\n");
	printf("\n");
}
 
int main(void)
{
	int len = 0;
	int cmd;
	LNode *L;		
	
	printf("Please input list length:");
	scanf("%d",&len);
	L = CreateList(len);
	PrintfList(L);
	while(1)
	{
		cmd = MenuSelect();
		switch(cmd)
		{
			case 1: Test1(L);		break;
			case 2: Test2(L);		break; 
			case 3: Test3(L);		break; 
			case 4: Test4(L);		break; 
		}
	}
	return 0;
}
 
//LNode *HeadCreateList(int len)
//{
//	LNode *L = (LNode*)malloc(sizeof(LNode)); 
//	LNode *temp = L;
//	temp->next = NULL;
//	for(int i=1;i<=len;i++) 
//	{
//		LNode *p = (LNode*)malloc(sizeof(LNode)); 
//		scanf("%d",&p->data); 
//		p->next = temp->next;  
//		temp->next = p;		 
//	}
//	return (LNode*)L;
//}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值