基于Linux的单链表的增删改查代码

/*************************************************************************
  > File Name: SingleLinkList.c
  > Author: Wenfei6316
  > Mail: Wenfei6316@163.com 
  > Created Time: 2018年06月16日 星期六 16时20分24秒
 ************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

#define LEN 25

typedef int data_t;
typedef struct Node
{
	data_t data;
	struct Node *next;
}Node, *Linklist;

Linklist CreateEmptyLinklist(void);
Linklist CreateListHead(Linklist head, int len);
Linklist CreateListTail(Linklist head, int len);
void PrintList(Linklist linklist);
void ListInsert(Linklist linklist, int posi, int value);
void ListDelete(Linklist linklist, int posi, int *value);
Linklist AdjMax(Linklist linklist);
void ListReverse(Linklist linklist);
void Clearlist(Linklist linklist);

int main(int argc, const char *argv[])
{
	int posi = 5;
	data_t data = 113;
	Linklist head1, head2, singlelist, adjmax;
	head1 = CreateEmptyLinklist();
	head2 = CreateEmptyLinklist();

	singlelist = CreateListHead(head1, LEN);
	PrintList(singlelist);
	printf("\n");

	head2 = CreateListTail(head2, LEN);
	PrintList(head2);
	printf("\n");

	ListInsert(singlelist, posi, data);
	PrintList(singlelist);
	printf("\n");

	ListDelete(singlelist, posi, &data);
	PrintList(singlelist);
	printf("\n");

	adjmax = AdjMax(singlelist);
	printf("Adjmax data is %d, next data is %d\n",
			adjmax->data, adjmax->next->data);

	ListReverse(singlelist);
	PrintList(singlelist);

	Clearlist(singlelist);
	Clearlist(head2);

	return 0;
}

//创建一个空表,即头节点
Linklist CreateEmptyLinklist(void)
{
	Linklist head;
	head = (Linklist)malloc(sizeof(Node));
	if (head == NULL)
	{
		perror("Create head failed");
		exit(EXIT_FAILURE);
	}

	head->data = -1;
	head->next = NULL;

	return head;
}

//头插法创建链表
Linklist CreateListHead(Linklist head, int len)
{
	int i;
	Linklist p;
	srand(time(NULL));

	for (i=0; i<len; i++)
	{
		p = (Linklist)malloc(sizeof(Node));
		if (p == NULL)
		{
			perror("Create linklist failed");
			return NULL;
		}
		p->data = rand() % 100;
		p->next = head->next;
		head->next = p;
	}

	return head;
}

//尾插法创建链表
Linklist CreateListTail(Linklist head, int len)
{
	int i;
	Linklist p, q;
	q = head;
	srand(time(NULL));
	for (i=0; i<len; i++)
	{
		p = (Linklist)malloc(sizeof(Node));
		if (p == NULL)
		{
			perror("Create linklist failed");
			return NULL;
		}
		p->data = rand() % 100;
		q->next = p;
		q = p;
	}
	q->next = NULL;

	return head;
}

//遍历链表
void PrintList(Linklist linklist)
{
	int count = 0;
	Linklist p;
	if (linklist == NULL)
		printf("Error linklist!\n");

	p = linklist->next;
	if (p == NULL)
		printf("Empty data!\n");

	while (p != NULL)
	{
		printf("%3d  ", p->data);
		if (!(++count % 5))
			printf("\n");
		p = p->next;
	}
	if (count % 5)
		printf("\n");
}

//插入新节点
void ListInsert(Linklist linklist, int posi, int value)
{
	int i;
	Linklist p, q;
	if (posi <= 0)
	{
		printf("Insert position can not be zero or less than zero!\n");
		return;
	}

	p = linklist;
	for (i=0; i<posi-1 && p != NULL; i++)
		p = p->next;
	if (p == NULL)
	{
		printf("Insert position error!\n");
		return;
	}

	q = (Linklist)malloc(sizeof(Node));
	if (q == NULL)
	{
		printf("Create linklist error!\n");
		return;
	}
	q->data = value;
	q->next = p->next;
	p->next = q;

	return;
}

//删除第 posi 位置节点
void ListDelete(Linklist linklist, int posi, int *value)
{
	int i;
	Linklist p, q;
	if (posi <= 0)
	{
		printf("Delete position can not be zero or less than zero!\n");
		return;
	}

	p = linklist;
	for (i=0; i<posi-1 && p != NULL; i++)
		p = p->next;
	if (p == NULL || p->next == NULL)
	{
		printf("Delete position error!\n");
		return;
	}

	q = p->next;
	*value = q->data;
	p->next = q->next;
	free(q);
}

//寻找最大元素对
Linklist AdjMax(Linklist linklist)
{
	int max = 0, ret;
	Linklist p, q;
	if (linklist->next==NULL || linklist->next->next==NULL)
	{
		printf("Linklist error!\n");
		exit(EXIT_FAILURE);
	}

	p = linklist->next;
	q = p->next;
	max = p->data + q->data;

	while (q->next != NULL)
	{
		if (max < q->data+q->next->data) 
		{
			max = q->data + q->next->data;
			p = q;
		}
		q = q->next;
	}

	return p;
}

//单链表反序
void ListReverse(Linklist linklist)
{
	Linklist p, q;
	if (linklist->next == NULL)
	{
		printf("Linklist empty!\n");
		return;
	}

	p = linklist->next;
	linklist->next = NULL;
	while (p != NULL)
	{
		q = p;
		p = p->next;    //必须先,否则 p->next 直接被设置为 NULL 了
		q->next = linklist->next;
		linklist->next = q;
	}
}

//清除链表,但不清楚链表头
void Clearlist(Linklist linklist)
{
	Linklist p;
	p = linklist->next;
	while (p != NULL)
	{
		linklist->next = p->next;
		free(p);
		p = linklist->next;
	}
	printf("Free linklist OK!\n");
}


单链表是一种常用的数据结构,它由若干个节点组成,每个节点包含一个数据元素和一个指向下一个节点的指针。在Linux C语言中,可以通过结构体来实现单链表,结构体包含一个数据成员和一个指向下一个节点的指针成员。 下面是一个简单的单链表结构体定义: ```c typedef struct node { int data; struct node *next; } Node; ``` 其中,data表示数据元素,next表示指向下一个节点的指针。 增加节点操作: ```c void add_node(Node **head, int data) { Node *new_node = (Node*)malloc(sizeof(Node)); new_node->data = data; new_node->next = NULL; if (*head == NULL) { *head = new_node; } else { Node *cur = *head; while (cur->next != NULL) { cur = cur->next; } cur->next = new_node; } } ``` 删除节点操作: ```c void delete_node(Node **head, int data) { Node *cur = *head; Node *prev = NULL; while (cur != NULL) { if (cur->data == data) { if (prev == NULL) { *head = cur->next; } else { prev->next = cur->next; } free(cur); return; } prev = cur; cur = cur->next; } } ``` 修改节点操作: ```c void modify_node(Node *head, int old_data, int new_data) { Node *cur = head; while (cur != NULL) { if (cur->data == old_data) { cur->data = new_data; return; } cur = cur->next; } } ``` 查询节点操作: ```c Node* find_node(Node *head, int data) { Node *cur = head; while (cur != NULL) { if (cur->data == data) { return cur; } cur = cur->next; } return NULL; } ``` 以上就是在Linux C语言中实现单链表增删改查操作的示例代码
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值