从链表开始——C语言实现链表

实现链表的创建、头插、查看、删除、查找、清空与反转:

头文件:linklist.h

#pragma once
#include<stdio.h>
#include<stdlib.h>
struct Node
{
	int data;
	struct Node* next;
};

struct Node* createlist();//创建链表
void printList(struct Node* headNode);//查看链表
void insertNodeByhead(struct Node* headNode, int data);//头插
void deleteNodeByAppoin(struct Node* headNode, int posdata);//指定位置删除
struct Node* locateNode(struct Node* headNode, int data);//查找
int initList(struct Node* list);//初始化链表
void recursive_reverse(struct Node* head);

linkList.cpp

#include"linklist.h"
struct Node* createlist()
{
	struct Node* headNode = (struct Node*)malloc(sizeof(struct Node));
	headNode->data = 0;
	headNode->next = NULL;
	return headNode;
}

struct Node* createNode(int data)
{
	struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
	newNode->data = data;
	newNode->next = NULL;
	return newNode;
}

void printList(struct Node* headNode)
{
	struct Node* pMove = headNode->next;
	if (pMove == NULL)
	{
		printf("链表为空!");
	}
	else
	{
		while (pMove)
		{
			printf("%d ", pMove->data);
			pMove = pMove->next;
		}
	}
	printf("\n");
}

void insertNodeByhead(struct Node* headNode, int data)
{
	struct Node* newNode = createNode(data);
	newNode->next = headNode->next;
	headNode->next = newNode;
}

void deleteNodeByAppoin(struct Node* headNode, int posdata)
{
	struct Node* posNode = headNode->next;
	struct Node* posNodeFront = headNode;
	if (posNode == NULL)
	{
		printf("无法删除链表为空!\n");
	}
	else
	{
		while (posNode->data != posdata)
		{
			posNodeFront = posNode;
			posNode = posNode->next;
			if (posNode == NULL)
			{
				printf("未找到指定元素!\n");
				return;
			}
		}
		posNodeFront->next = posNode->next;
		printf("删除成功!\n");
		free(posNode);
	}
}

struct Node* locateNode(struct Node* headNode, int data)
{
	struct Node *p = headNode->next;
	while (p->next!=NULL&&p->data!=data)
	{
		p = p->next;
	}
	return p;
}

int initList(struct Node* list)
{
	struct Node* p;
	struct Node* q;
	p = list->next;
	while (p)
	{
		q = p->next;
		free(p);
		p = q;
	}
	list->next = NULL;
	return 1;
}

void recursive_reverse(struct Node* head)
{
	if (head->next == NULL) return;

	struct Node* pre = NULL;
	struct Node* cur = head->next;
	struct Node* next;

	while (cur) {
		next = cur->next;
		cur->next = pre;
		pre = cur;
		cur = next;
	}

	head->next = pre;
}

链表儿.cpp

#include<stdio.h>
#include<stdlib.h>
#include"linklist.h"
void menu()
{
	printf("***********[1]创建链表*********\n");
	printf("***********[2]头插元素*********\n");
	printf("***********[3]查看链表*********\n");
	printf("***********[4]删除元素*********\n");
	printf("***********[5]查找元素**** ****\n");
	printf("***********[6]清空链表*********\n");
	printf("***********[7]反转链表*********\n");
}
int main()
{
	struct Node* list= createlist();
	menu();
	printf("\n");
	while (1)
	{
		int choose=0;
		int item = 0;
		int del = 0;
		int loc = 0;
		scanf_s("%d", &choose);
		switch (choose)
		{
		case 1:
		{
			struct Node* list= createlist();
			printf("链表创建成功!\n");
			break;
		}

		case 2:
		{
			printf("请输入数据>>以-1结尾\n");
			while (1)
			{
				scanf_s("%d", &item);
				if (item == -1)
				{
					printf("插入完成!\n");
					break;
				}
				insertNodeByhead(list, item);
			}
			break;
		}
		case 3:
		{
			printList(list);
			break;
		}
		case 4:
		{
			
			printf("请输入要删除的元素>>\n");
			scanf_s("%d", &del);
			deleteNodeByAppoin(list, del);
			break;
		}
		case 5:
		{
			printf("请输入要查找的元素>>\n");
			scanf_s("%d", &loc);
			if (locateNode(list, loc))
				printf("所查找元素的地址为:%p\n", locateNode(list, loc));
			else
				printf("您查找的数值不存在。\n");
			break;
		}
		case 6:
		{
			initList(list);
			printf("清空成功!\n");
		}
		case 7:
		{
			printf("反转成功!\n");
			recursive_reverse(list);
			printList(list);
			break;
		}
		}
	}
	system("pause");
	return 0;
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值