c写了个通用链表带了例子。

本文深入探讨了C++编程的基础知识与实际应用,包括数据类型、运算符、流程控制、函数、类与对象等核心概念,通过实例展示了如何利用C++解决实际问题。

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

List.h
#ifndef LIST_H
#define LIST_H
#define LEN sizeof(struct ListNode_T)

struct staff
{
int iStaffID;
char acName[20];
char acPasswd[10];
};


typedef struct ListNode_T
{
	void *data;
	struct ListNode_T *next;
}ListNode;

ListNode* ListInit(void *data);
ListNode* ListLocate(ListNode *head);
void ListAdd(ListNode *head,void *data);
int ListGetCount(ListNode *head);
ListNode* ListGetNode(int iIndex, ListNode *head);
int ListFree(ListNode *head);
ListNode *ListDel(int iIndex,ListNode *head);
ListNode* ListDel2(int iIndex,ListNode *head);
#endif


List.c

#include<stdio.h>
#include<stdlib.h>
#include "List.h"

typedef void(*pPrintEvent)(void *data);

ListNode* ListInit(void *data)
{
	ListNode *pNode = (ListNode *)malloc(LEN);
	pNode->data = data;
	pNode->next = NULL;
	return pNode;
}

ListNode* ListLocate(ListNode *head)
{
	ListNode *pCurrent = head;
	if(pCurrent == NULL)
		return NULL;
	while(pCurrent->next != NULL)
	{
		pCurrent = pCurrent->next;
	}
	return pCurrent;
}

void ListAdd(ListNode *head,void *data)
{
	ListNode *p = NULL;
	ListNode *pNode = NULL;

	p = ListLocate(head);	//找到最后个非空的节点
	pNode = ListInit(data);	//初始化一个新的节点

	if(p == NULL)
	{
		printf("头节点未分配内存\n");
		exit(1);
	}

	if(p->data == NULL)
	{
		head->data = data;
		head->next = NULL;
	}
	else
	{
		p->next = pNode;
	}
}

void PrintStaff(void *data)
{
	struct staff *st = (struct staff*)data;
	printf("%d %s %s\n",st->iStaffID,st->acName,st->acPasswd);
}

void print(ListNode *head,pPrintEvent PrintEvent)
{
	ListNode *pCurrent = head;
	while(pCurrent != NULL)
	{
		
		PrintEvent(pCurrent->data);
		pCurrent = pCurrent->next;
	}
}

int ListGetCount(ListNode *head)
{
	ListNode *pCurrent = head;
	int iCount = 0;
	while(pCurrent != NULL)
	{
		iCount++;
		pCurrent = pCurrent->next;
	}
	return iCount;
}

/*节点存在返回该节点,否则返回NULL指针*/
ListNode* ListGetNode(int iIndex, ListNode *head)
{
	ListNode *pCurrent = head;
	int i = 0;
	
	while(i < iIndex && pCurrent != NULL)
	{
		pCurrent = pCurrent->next;
		i++;
	}

	if(pCurrent == NULL)
	{
		printf("该节点不存在\n");
		return NULL;
	}
	else
		return pCurrent;
}

int ListFree(ListNode *head)
{
	int iCount = 0;
	ListNode *pCurrent = head;
	while(head != NULL)
	{
		head = head->next;
		iCount++;
		free(pCurrent->data);
		free(pCurrent);
		pCurrent = head;
	}
	return iCount;
}

ListNode *ListDel(int iIndex,ListNode *head)
{
	ListNode *pPrior = NULL;
	ListNode *pNext = NULL;
	ListNode *pCurrent = head;
	int iCount =1;
	while(pCurrent->next != NULL && pCurrent != NULL)
	{
		if(iIndex == 0)
		{
			pPrior = head;
			head = head->next;
			free(pPrior->data);
			free(pPrior);
			return head;
		}
		pPrior = pCurrent;
		pCurrent = pCurrent->next;
		if(iCount == iIndex)
		{
			pNext = pCurrent->next;
			break;
		}
		iCount++;
	}

	if(pCurrent != NULL || pCurrent->next != NULL)
	{
		pPrior->next = pNext;
		free(pCurrent->data);
		free(pCurrent);
		return head;
	}
	else
	{
		printf("无能删除的节点\n");
		return head;
	}
}

ListNode* ListDel2(int iIndex,ListNode *head)
{
	ListNode *pPrior = NULL;
	ListNode *pNext = NULL;
	ListNode *pCurrent = head;
	int iCount =1;
	while(pCurrent->next != NULL && pCurrent != NULL)
	{
		if(iIndex == 0)
		{
			pPrior = head;
			head = head->next;
			free(pPrior->data);
			free(pPrior);
			return head;
		}
		pPrior = pCurrent;
		pCurrent = pCurrent->next;
		if(iCount == iIndex)
		{
			pNext = pCurrent->next;
			break;
		}
		iCount++;
	}

	if(pCurrent != NULL || pCurrent->next != NULL)
	{
		pPrior->next = pNext;
		pCurrent->next = NULL;
		return pCurrent;
	}
	else
	{
		printf("无能删除的节点\n");
		return NULL;
	}
}



int main()
{
	
	struct staff *st[10];
	int i = 0;
	int iCount = 0;

	ListNode *pNode = NULL;
	ListNode *head = ListInit(NULL);

	for(i=0;i<3;i++)
	{
		st[i] = (struct staff *)malloc(sizeof(struct staff));
		scanf("%d %s %s",&(st[i]->iStaffID),st[i]->acName,st[i]->acPasswd);
		ListAdd(head,st[i]);
	}
	print(head,PrintStaff);
	printf("\n");

	iCount = ListGetCount(head);
	printf("%d\n",iCount);

	for(i=0;i<iCount;i++)
	{
		 pNode = ListGetNode(i,head);
		 print(pNode,PrintStaff);
		 printf("\n");
	}

	pNode = ListDel2(1,head);
	print(pNode,PrintStaff);
	printf("______________________\n");
	print(head,PrintStaff);
	i = ListFree(head);
	printf("%d\n",i);
	return 0;
}


头结点没有独立出来为空,直接拿来用了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值