单向链表学习笔记 - C语言

链表头文件 linked_list.h

#ifndef _LINKED_LIST_H_
#define _LINKED_LIST_H_

struct Node;
typedef struct Node *PtrToNode;
typedef PtrToNode List;
typedef PtrToNode Position;
typedef int ElementType;


List initLinkedList(ElementType *X, int NumOfX);
List MakeEmpty(List L);
int IsEmpty(List L);
int IsLast(Position P, List L);
Position Find(ElementType X, List L);
void Delete(ElementType X, List L);
Position FindPrevious(ElementType X, List L);
void Insert(ElementType X, List L, Position P);
void DeleteList(List L);
Position Header(List L);
Position Advance(Position P);
ElementType Retrieve(Position P);
void PrintList(List L);

#endif

链表实现文件 linked_list.c

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

struct Node
{
	ElementType Element;
	Position Next;
};


List initLinkedList(ElementType *X, int NumOfX)
{
	int i;
	ElementType *Elements = X;
	Position Head = NULL;
	
	Position curCell = malloc(sizeof(struct Node));
	if(curCell == NULL)
			printf("Out of space!!!");
		
	Head = curCell;
		
	for(i=0; i<NumOfX; i++)
	{
		Position tmpCell = malloc(sizeof(struct Node));
		if(tmpCell == NULL)
			printf("Out of space!!!");
		printf("element:%d   line:%d, func:%s\r\n",Elements[i],__LINE__,__FUNCTION__);
		tmpCell -> Element = Elements[i];
		curCell -> Next = tmpCell;
		curCell = tmpCell;
	}
	return Head;
}

int IsEmpty(List L)
{
	return L -> Next == NULL;
}

int IsLast(Position P, List L)
{
	return P -> Next == NULL;
}

Position Find(ElementType X, List L)
{
	Position P;
	
	P = L -> Next;
	while(P != NULL && P -> Element != X)
	{
		P = P -> Next;
	}
	
	return P;
}

void Delete(ElementType X, List L)
{
	Position P, TmpCell;
	
	P = FindPrevious(X, L);
	
	if(!IsLast(P, L))
	{
		TmpCell = P -> Next;
		P -> Next = TmpCell -> Next;
		free(TmpCell);
	}
}

Position FindPrevious(ElementType X, List L)
{
	Position P;
	
	P = L;
	while(P -> Next != NULL && P -> Next -> Element != X)
		P = P -> Next;
	
	return P;
}

void Insert(ElementType X, List L, Position P)
{
	Position TmpCell;
	
	TmpCell = malloc(sizeof(struct Node));
	if(TmpCell == NULL)
		printf("Out of space!!!");
	
	TmpCell -> Element = X;
	TmpCell -> Next = P -> Next;
	P -> Next = TmpCell;
}

void PrintList(List L)
{
	int i = 1,j;
	List tmpList = L -> Next;
	
	while(tmpList -> Next != NULL)
	{
		tmpList = tmpList -> Next;
		i++;
	}
	j = i;
	tmpList = L -> Next;
	for(;i>0;i--)
	{
		printf("List[%d] = %d\r\n",j-i, tmpList -> Element);
		tmpList = tmpList -> Next;
	}	
}

链表调用主函数 main.c

#include <stdio.h>
#include "linked_list.h"

enum ListMethod
{
	INSERT,
	DELETE,
	PRINTLIST,
};

void printInfo(void);

int main()
{
	int num,n;
	ElementType X[] = {1,2,564,4,45,6};
	List listHead = initLinkedList(X, sizeof(X)/4);
	PrintList(listHead);

	while(1)
	{
		printInfo();
		scanf("%d",&num);
		switch (num)
		{
			case INSERT:
				printf("input a number:");
				scanf("%d",&num);
				Insert(num, listHead, listHead);
			break;
			case DELETE:
				printf("input a number:");
				scanf("%d",&num);
				Delete(num, listHead);
			break;
			case PRINTLIST:
				PrintList(listHead);
			break;
			default:
			printf("input error!!!\r\n");
			break;
		}
	}
	return 0;
}

void printInfo(void)
{
	printf("\
						ListMethod:\r\n\
						Insert -> 0\r\n\
						Delete -> 1\r\n\
						print list -> 2\r\n\
");
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值