链表代码(完整)

#include<stdio.h>
#include<malloc.h>
#include<stdbool.h>
//定义链表
typedef struct ListNode{
	int val;
	struct ListNode* next;
}ListNode;

//链表初始化
ListNode* CreateList()
{
	ListNode* Head=(ListNode*)malloc(sizeof(ListNode));
	Head->next=NULL;
	return Head;
}
//创建新结点
ListNode* CreateListNode()
{
	ListNode* Node=(ListNode*)malloc(sizeof(ListNode));
	printf("please input data:\n");
	int data;
	scanf("%d",&data);
	Node->val=data;
	Node->next=NULL;
	return Node;
}
//头插法
void InsertNodeHead(ListNode* Head)
{
	int i,n;
	printf("InsertNodeHead:\n");
	printf("please input size:\n");
	scanf("%d",&n);
	for(i=0;i<n;i++)
	{
		ListNode* newNode=CreateListNode();
		newNode->next=Head->next;
		Head->next=newNode;
	}
}
//尾插法
void InsertNodeTail(ListNode* Head)
{	
	ListNode* Rear=(ListNode*)malloc(sizeof(ListNode));
	Rear=Head;
	int i,n;
	printf("InsertNodeTail:\n");
	printf("please input size\n");
	scanf("%d",&n);
	for(i=0;i<n;i++)
	{
		ListNode* newNode=CreateListNode();
		newNode->next=Rear->next;
		Rear->next=newNode;
		Rear=newNode;
	}
}
//打印输出链表
void PrintList(ListNode* Head)
{
	ListNode* p=Head->next;
	while(p)
	{
		printf("%d\t",p->val);
		p=p->next;
	}
	printf("\n");
}
//指定位置删除
void DeleteNodeLocate(ListNode* Head)
{
	int posData;
	printf("please input posData:\n");
	scanf("%d",&posData);
	ListNode* p=Head;
	ListNode* posNode=Head->next;
	while(posNode->val!=posData)
	{
		p=p->next;
		posNode=posNode->next;
		if(posNode==NULL)
		{
			printf("fail to DeleteNodeLocate!\n");
			return ;
		}
	}
	p->next=posNode->next;
	free(posNode);
}
//指定位置插入
void InsertNodeLocate(ListNode* Head)
{
	int pos;
	printf("please input pos:\n");
	scanf("%d",&pos);
	ListNode* p=Head;
	ListNode* posNode=Head->next;
	int i=0;
	for(i;i<pos-1;i++)
	{
		p=p->next;
		posNode=posNode->next;
	}
	ListNode* newNode=CreateListNode();
	p->next=newNode;
	newNode->next=posNode;
}
//判断是否为空
bool EmptyList(ListNode* Head)
{
	if(Head->next)
	{
		printf("not Empty!\n");
		return 0;
	}
	else
	{
		printf("Empty!\n");
		return 1;
	}
}
//清空链表
void ClearList(ListNode* Head)
{
	ListNode *p,*q;
	p=Head->next;
	while(p )
	{
		q=p->next;
		free(p);
		p=q;
	}
	Head->next=NULL;
	printf("Clear !\n");
}
//求链表长度
int Length(ListNode* Head)
{
	ListNode* p=Head;
	int count=0;
	while(p->next!=NULL)
	{
		count++;
		p=p->next;
	}
	printf("Length:%d\n",count);
	return count;
}

int main(int argc, const char *argv[])
{
	ListNode* Head=CreateList();
	EmptyList(Head);
	//InsertNodeTail(Head);
	InsertNodeHead(Head);
	PrintList(Head);
	//ClearList(Head);
	//EmptyList(Head);
	//DeleteNodeLocate(Head);
	//InsertNodeLocate(Head);
	PrintList(Head);
	Length(Head);
	return 0;
}

链表是一种常见的数据结构,它由一系列节点组成,每个节点包含数据和指向下一个节点的指针。在C语言中,实现链表通常需要定义节点结构体,并创建和操作这些节点的函数。以下是一个简单的单向链表完整示例代码,包括创建节点、插入节点到链表头部、遍历链表和释放链表内存的基本操作。 ```c #include <stdio.h> #include <stdlib.h> // 定义链表节点的结构体 struct Node { int data; struct Node* next; }; // 创建一个新节点 struct Node* createNode(int data) { struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); if (newNode == NULL) { printf("内存分配失败\n"); exit(0); } newNode->data = data; newNode->next = NULL; return newNode; } // 在链表头部插入一个新节点 void insertAtHead(struct Node** head, int data) { struct Node* newNode = createNode(data); newNode->next = *head; *head = newNode; } // 遍历链表并打印节点数据 void printList(struct Node* head) { struct Node* current = head; while (current != NULL) { printf("%d ", current->data); current = current->next; } printf("\n"); } // 释放链表占用的内存 void freeList(struct Node* head) { struct Node* current = head; struct Node* next; while (current != NULL) { next = current->next; free(current); current = next; } } int main() { struct Node* head = NULL; // 初始化链表为空 // 插入节点到链表头部 insertAtHead(&head, 1); insertAtHead(&head, 2); insertAtHead(&head, 3); // 打印链表 printf("链表元素:"); printList(head); // 释放链表占用的内存 freeList(head); return 0; } ``` 这段代码首先定义了一个链表节点的结构体`Node`,然后实现了创建节点、在链表头部插入节点、打印链表和释放链表内存的函数。`main`函数中创建了一个空链表,然后通过`insertAtHead`函数向链表头部插入了三个节点,并通过`printList`函数打印了链表中的所有节点数据。最后,使用`freeList`函数释放了链表占用的内存。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值