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

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

#include
#include//malloc
#include//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 (iNext;
		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;
}

调试结果:

 

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/3002864.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-09-27
下一篇 2022-09-27

发表评论

登录后才能评论

评论列表(0条)

保存