数据结构实验-链表

#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
typedef int DataType;

typedef struct Node {
	DataType data;
	struct Node* next;
} Node, *List;

List InitList() {
	List head = (List)malloc(sizeof (Node));
	head->next = NULL;
	printf("链表建立成功\n");
	return head;
}

List create(int n) {
	
	if(!n) {
		printf("单链表为空,创建失败\n");
		return NULL;
	} else {
	  List head = InitList();
		List tmp = head;
		printf("请输入 %d 个整数\n", n);
		for(int i = 1; i <= n; i ++ ) {
			List p = (List)malloc(sizeof(Node));
			scanf("%d", &p->data);
			tmp->next = p;
			p->next = NULL;
			tmp = p;
			
		}
		return head; 
	}
	
	
}

void add_head(List head, DataType x) { 
	List temp = (List)malloc(sizeof (Node));
	temp->data = x;
	temp->next = head->next;
	head->next = temp;
}

bool insert(List head, int pos, DataType x) {
	List p = head;
	int count = pos;
	while(p != NULL && count) {
		p = p->next;
		-- count;
	}

	if(p == NULL) {
		printf("插入失败!!!\n");
		return false;
	} else {
		List temp = (List)malloc(sizeof (Node));
		temp->data = x;
		temp->next = p->next;
		p->next = temp;
		return true;
	}
}

bool Erase(List head, int pos, DataType& x) {
	List p = head;
	int count = pos;
	while(p != NULL && count) {
		p = p->next;
		-- count;
	}

	if(p == NULL) {
		printf("删除失败!!!\n");
		return false;
	} else {
		List temp = p->next;
		x = temp->data;
		p->next = temp->next;
		free(temp);
		return true;
	}
}

void Delete(List list) {
	List head = list;
	while(head != NULL) {
		List temp = head->next;
		free(head);
		head = temp;
	}
	list->next = NULL;
}

bool change(List head, int pos, DataType x) {
	List p = head;
	int count = pos;
	while(p != NULL && count) {
		p = p->next;
		-- count;
	}

	if(p == NULL) {
		printf("更改失败!!!\n");
		return false;
	} else {
		List temp = p->next;
		temp->data = x;
		return true;
	}
}

bool get_value(List head, int pos, DataType& x) {
	List p = head;
	int count = pos;
	while(p != NULL && count) {
		p = p->next;
		-- count;
	}

	if(p == NULL) {
		printf("查询失败!!!\n");
		return false;
	} else {
		List temp = p->next;
		x = temp->data;
		return true;
	}
}

bool IsEmpty(List head) {
	return (head->next == NULL);
}

void PrintList(List head) {
	printf("链表中的节点元素为: \n");
	while(head->next != NULL) {
		head = head->next;
		printf("%d ", head->data);
	}
	printf("\n"); 
}

List MergeList(List head1, List head2) {
	List ans; 
	ans = InitList();
	List head = ans;
	List p = head1->next, q = head2->next;
	while(p != NULL && q != NULL) {
		if(p->data < q->data) {
			List res = (List)malloc(sizeof (Node));
			res->data = p->data;
			res->next = NULL;
			head->next = res;
			head = res;
			p = p->next;	
		} else {
			List res = (List)malloc(sizeof (Node));
			res->data = q->data;
			res->next = NULL;
			head->next = res;
			head = res;
			q = q->next;
		}
	}
	List tmp = q;
	if(p != NULL) tmp = p;
	while(tmp != NULL) {
		List res = (List)malloc(sizeof (Node));
		res->data = tmp->data;
		res->next = NULL;
		head->next = res;
		head = res;
		tmp = tmp->next;	
	}
	return ans;
} 

void print() {
	printf("*****************************************\n");
	printf("********1----建立链表********************\n");
	printf("********2----在表头添加一个元素**********\n");
	printf("********3----在某个位置添加一个元素******\n");
	printf("********4----删除某个位置的一个元素******\n");
	printf("********5----删除整个链表****************\n");
	printf("********6----改变某个位置的值************\n");
	printf("********7----查询某个位置的值************\n");
	printf("********8----查询链表是否为空************\n");
	printf("********9----打印整个链表****************\n");
	printf("********10----归并有序表*****************\n");
	printf("*************退出:输入一个负数***********\n");
	printf("*****************************************\n");

}

int main() {
	int op, x, idx;
	List list;
	print();
	while(true) {
		scanf("%d", &op);
		if(op <= 0) {
			printf("操作结束,再见\n");
			break; 
		} else if(op == 1) {
			list = InitList();
		} else if(op == 2) {
			printf("请输入插入的元素: "); 
			scanf("%d", &x);
			add_head(list, x);
		} else if(op == 3) {
			printf("请输入插入的位置、元素: ");
			scanf("%d%d", &idx, &x);
			insert(list, idx, x);
		} else if(op == 4) {
			printf("请输入删除的位置: ");
			scanf("%d", &idx);
			if(Erase(list, idx, x))
				printf("删除的元素是: %d\n", x);
		} else if(op == 5) {
			Delete(list);
		} else if(op == 6) {
			printf("请输入需要改变的位置和改变之后的值: ");
			scanf("%d%d", &idx, &x);
			change(list, idx, x);
		} else if(op == 7) {
			printf("请输入查询的位置: ");
			scanf("%d", &idx);
			if(get_value(list, idx, x)) {
				printf("%d 位置的值为: %d\n", idx, x);
			}
		} else if(op == 8) {
			if(IsEmpty(list)) {
				printf("链表为空\n");
			} else {
				printf("链表不为空\n");
			}
		} else if(op == 9) {
			PrintList(list);
		} else if(op == 10) {
			printf("请输出第二个有序表的长度: ");
			int n; scanf("%d", &n);
			printf("请从小到大依次输出有序表中的元素: ");
			List head2 = create(n);
			printf("第一个有序表的元素为\n");
			PrintList(list); 
			printf("第二个有序表的元素为\n");
			PrintList(head2);
			List head = MergeList(list, head2);
			printf("合并之后有序表的元素为\n");
			PrintList(head);
		} 
	}

	return 0;
}



posted @ 2022-03-24 14:51  ccz9729  阅读(163)  评论(0编辑  收藏  举报