数据结构与算法(C语言描述版)单链表代码实现

#include<stdio.h>
#include<stdlib.h>//malloc
#include<stdbool.h>//bool类型
/*函数原型说明*/
typedef struct Node {
	int element;//值域
	struct Node* Next;//指针域
}Node;
Node* createHeaderNode();//创建头节点

bool isEmpty(Node* header);

void insertFirst(struct Node* header, int x);//头插法
void insertLast(struct Node* header, int x);//尾插法

Node* find(struct Node* header, int x);//按值查找
Node* findth(struct Node* header, int position);//按位查找

void PrintfList(struct Node* header);//链表打印

void delete(struct Node* header, int x);//链表删除

int size(struct Node* header);//链表大小
int main() {
	struct Node* header;
	struct Node* node;
	header = createHeaderNode();
	//node = createHeaderNode();

	printf("List is Empty?:%d\n", isEmpty(header));
	printf("==================\n");

	insertFirst(header, 1);
	insertFirst(header, 2);
	insertFirst(header, 3);

	PrintfList(header);
	printf("==================\n");

	insertLast(header, 3);
	insertLast(header, 4);


	PrintfList(header);
	printf("==================\n");

	node = find(header, 4);
	if (node == NULL) {
		printf("not find...\n");
	}
	else
	{
		printf("find the element of the node:%d\n", node->element);
	}
	printf("==================\n");

	node = findth(header, 2);
	if (node == NULL) {
		printf("not find...\n");
	}
	else
	{
		printf("find the element of the node:%d\n", node->element);
	}
	printf("==================\n");

	delete(header, 3);
	PrintfList(header);
	printf("==================\n");

	printf("the size of the list is %d", size(header));
}

Node* createHeaderNode() {
	Node* p;
	p = (Node*)malloc(sizeof(struct Node));
	p->Next = NULL;
	p->element = 0;
	return p;
}
bool isEmpty(Node* header) {
	return header->Next = NULL;
}
void insertFirst(struct Node* header, int x) {
	Node* tmp;
	tmp=(Node*)malloc(sizeof(struct Node));
	if (tmp == NULL) {
		printf("内存空间不足\n");
		return;
	}
	tmp->Next = header->Next;
	header->Next = tmp;
	tmp->element = x;
}
void insertLast(struct Node* header, int x) {
	Node* tmp;
	Node* p;
	tmp = (Node*)malloc(sizeof(struct Node));
	if (tmp == NULL) {
		printf("内存空间不足\n");
		return;
	}
	p = header;
	while (p->Next) {
		p = p->Next;
	}
	tmp->Next = p->Next;
	p->Next = tmp;
	tmp->element = x;
}
void PrintfList(struct Node* header) {
	Node* p;
	p = header;
	while (p->Next) {
	    p = p->Next;
		printf("%d--->", p->element);
	}
	printf("NULL\n");
}
Node* find(struct Node* header, int x) {
	Node* p;
	p = header->Next;
	while (p != NULL && p->element != x) {
		p = p->Next;
	}
	return p;
}
Node* findth(struct Node* header, int position) {
	Node* p;
	int i = 1;
	p = header->Next;
	while (i<position) 
	{
		//if (i == position) {
			//return p;
		//}
		p = p->Next;
		i++;
	}
	return p;
}
void delete(struct Node* header, int x) {
	struct Node* privious;
	struct Node* p;
	p = header->Next;
	privious = header;
	
	while (p != NULL) {
		if (p->element ==x) {
			privious->Next = p->Next;
			free(p);
			break;
		}
		else {
			privious = p;
			p = p->Next;
		}
	}
	printf("删除后的节点为:\n");
	return;
}
int size(struct Node* header) {
	int count = 0;
	struct Node* p;
	p = header->Next;
	while (p != NULL) {
		count++;
		p = p->Next;
	}
	return count;
}

调试结果:

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值