單鏈表的相關基本操作(查找、刪除、插入、撤銷、輸出、逆置、排序)

單鏈表的相關基本操作(查找、刪除、插入、撤銷、輸出)

準備工作

#include<stdio.h>
#include<stdlib.h>
#define ERROR 0
#define OK 1
typedef int Status;
typedef int ElemType;
typedef struct node {
	ElemType element;
	struct node* link;
}Node;
typedef struct headerList {
	Node* head;
	int n;
}HeaderList;

帶表頭的單鏈表的初始化

Status Init(HeaderList* h) {                                      //初始化一個空的帶表頭的單鏈表
	h->head = (Node*)malloc(sizeof(Node));
	if (!h->head)return ERROR;
	h->head->link = NULL;
	h->n = 0;
	return OK;
}

插入運算,實現在下標i之後插入,似乎叫後插法?

Status Insert(HeaderList* h, int i, ElemType x) {                 //在ai之後插入新元素x
	Node* p = h->head;
	Node* q = (Node*)malloc(sizeof(Node));
	if (i<-1 || i>h->n - 1)
		return ERROR;
	for (int j = 0; j <=i; j++)
		p = p->link;                                             //定位到ai
	q->link = p->link;
	q->element = x;
	
	p->link = q;
	h->n++;                                                      //鏈表元素個數+1
	return OK;
}

刪除運算

Status Delete(HeaderList* h, int i) {                           //刪除下標爲i的元素
	Node* p = h->head;
	Node* pre = p;
	if (!h->n) return ERROR;
	if (i<0 || i>h->n - 1) return ERROR;
	for (int j = 0; j <= i; j++) {
		pre = p;
		p = p->link;
	}
	pre->link = p->link;
	h->n--;                                                     //鏈表元素個數減一
	free(p);                                                    //釋放p的動態空間
	return OK;
}

查找運算,將元素值賦給x

Status Search(HeaderList* h, int i, ElemType *x) {              //實現函數查找,將ai的值通過x返回
	Node* p = h->head;
	if (i<0 || i>h->n - 1) 
		return ERROR;
	for (int j = 0; j <= i; j++)
		p = p->link;
	*x = p->element;
	return OK;
}

輸出運算

void Output(HeaderList* h) {                                    //輸出函數
	Node* p = h->head;
	for (int i = 0; i < h->n; i++) {
		p = p->link;
		printf("%3d", p->element);
		if (i % 5 == 4 && i != 1) {
			printf("\n");
		}
	}
}

代碼撤銷運算,釋放內存空間

void Destroy(HeaderList* h) {
	Node* p, * q = h->head;
	while (q->link) {
		p = q->link;
		free(q);
		q = p;
	}
	printf("clear successfully!");
}

逆置運算,在函數內部定義了一個新鏈表,按照前插法,每次調用insert函數,最終輸出單鏈表r

Status Reverse(HeaderList* h) {                          //帶表頭的單鏈表逆置
	HeaderList r;
	Init(&r);
	Node* p;
	p = h->head;
	if (!h->n) return ERROR;
	while (p->link) {
		p = p->link;
		Insert(&r, -1, p->element);
	}
	printf("逆置後的單鏈表爲:\n");
	Output(&r);
	return OK;
}

單鏈表的降序算法

void Sort(HeaderList* h) {                       //返回一個降序的單鏈表
	Node* p, * q;
	Status temp;
	for (p = h->head->link; p->link != NULL; p = p->link) {
		for (q = p->link; q != NULL; q = q->link) {
			if (p->element < q->element) {
				temp = p->element;
				p->element = q->element;
				q->element = temp;
			}
		}
	}
	return;
}

測試函數的主函數

int main() {
	HeaderList q;
	Init(&q);
	for (int i = 0; i < 10; i++)
		Insert(&q, i - 1, i + 1);
	int j;
	Output(&q);
	Sort(&q);
	printf("降序排列:\n");
	Output(&q);
	Reverse(&q);
	Search(&q, 5, &j);
	printf("輸出刪除前下標爲5的元素:");
	printf("%d\n", j);
	Delete(&q, 5);
	Output(&q);
	Search(&q, 5, &j);
	printf("輸出刪除後下標爲5的元素:");
	printf("%d\n", j);
	Destroy(&q);                                   //清空函數
	return 0;
}

只需要將代碼逐塊複製進編譯器就能運行了呀!如果有什麼問題,可以評論區指出,新手上路,請多指教。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章