线性表之单链表(链式存储结构)--C实现

本文介绍了一个使用C语言实现的链表数据结构,包括创建、获取元素、插入、删除及清理链表等基本操作。链表的优势在于动态调整大小,但查找效率较低。

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

/*
 * LinkedList
 * linc
 * 2013.2.26
 */
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define OK 1  
#define ERROR -1  
#define TURE 1  
#define FALSE 0 

struct Node
{
	int data;
	struct Node *next;
};
typedef struct Node *Head;
typedef struct Node *LinkedList;

//create the linked list,head insert
void createList(LinkedList *list,int size,Head *head)
{
	/*
	*head = (Head)malloc(sizeof(struct Node));
	printf("head test0");
	(*head)->data = size;
	printf("head test1");
	//(*head)->next = (*list);
	printf("head test2");
	*/
	LinkedList tmpList;
	srand(time(0));
	*list = (LinkedList)malloc(sizeof(struct Node));
	(*list)->next = NULL;
	for(int i = 0; i < size; i++)
	{
		tmpList = (LinkedList)malloc(sizeof(struct Node));
		tmpList->data = rand()%100 + 1;
		tmpList->next = (*list)->next;
		(*list)->next = tmpList;
		printf("list->data %d is %d\n",i,tmpList->data);
	}
	//head node
	tmpList = (LinkedList)malloc(sizeof(struct Node));
	tmpList->data = size;
	tmpList->next = (*list)->next;
	(*list)->next =tmpList;

	/*
	while((*list) != NULL)
	{
		printf("data is %d\n",(*list)->data);
		(*list)->next;
	}
	*/

}
//get element of list
int getElement(LinkedList list,int index,int *element)
{
	printf("getElement\n");
	LinkedList tmpList = list->next;
	printf("getElement---test\n");
	int size = tmpList->data;//the list size
	printf("the size is %d\n",size);
	if(index > size)
	{
		return ERROR;
	}
	
	int count = 0;
	while(tmpList && count < index+1)//+1 for head node
	{
		printf("data is %d\n",tmpList->data);
		tmpList = tmpList->next;
		++count;
	}
	if(!tmpList)
		return ERROR;
	*element = tmpList->data;
	return OK;
}

//clear list
int clearList(LinkedList *list)
{
	//use 2 points free the list
	LinkedList tmpList1,tmpList2;
	tmpList1 = (*list)->next;
	while(tmpList1)
	{
		printf("free the data is %d\n",tmpList1->data);
		tmpList2 = tmpList1->next;
		free(tmpList1);
		tmpList1 = tmpList2;
	}
	(*list)->next = NULL;
	return OK;
}

//insert
//after index
int insert(LinkedList *list,int index,int element)
{
	printf("insert index is %d,element is %d\n",index,element);
	LinkedList tmpList = (*list)->next;
	int size = tmpList->data;
	if(index > size)
	{
		printf("ERROR:the index > size.\n");
		return ERROR;
	}
	int count = 0;
	while(tmpList && count < index+1)
	{
		tmpList = tmpList->next;
		++count;
	}
	if(!tmpList)
		return ERROR;
	LinkedList node = (LinkedList)malloc(sizeof(struct Node));
	node->data = element;
	node->next = tmpList->next;
	tmpList->next = node;
	return OK;
}
//delete
int delete(LinkedList *list,int index)
{
	printf("delete,index is %d\n",index);
	LinkedList tmpList = (*list)->next;
	int size = tmpList->data;
	if(index > size)
	{
		printf("ERROR:the index > size.\n");
		return ERROR;
	}
	int count = 0;
	while(tmpList && count < index+1)
	{
		tmpList = tmpList->next;
		++count;
	}
	if(!tmpList)
		return ERROR;
	LinkedList node = tmpList->next;
	tmpList->next = node->next;
	free(node);
	return OK;
}

int main()
{
	LinkedList *list;
	Head *head;
	createList(list,10,head);
	int i = 1;
	getElement(*list,5,&i);
	insert(list,5,20);
	printf("getElement is %d\n",i);
	getElement(*list,5,&i);
	printf("getElement is %d\n",i);
	delete(list,5);
	clearList(list);
	return 0;
}
链式存储结构的优点在于它在找到目标元素后,删除或者插入都十分方便,只需将指针挪动一次就好;缺点就是不能像顺序存储结构那样迅速的找到目标元素,只能笨笨的一个一个元素的遍历下去。
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值