数据结构之单链表

一.代码

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

#define false 0
#define true 1

typedef struct node{
	int data;
	struct node* next;
}linkList;

linkList* init(linkList* List) {
	List = (linkList*)malloc(sizeof(linkList));
	if (List)
		List->next = NULL;
	else
		printf("fail to allocate menory");
	return List;
}

int length(linkList* List) {
	if (List) {
		int length = 0;
		linkList* temp = List;
		while (temp->next != NULL) {
			temp = temp->next;
			length++;
		}
		return length;
	}
	printf("the list doesn't exist\n");
	return -1;
}

int loacateElem(linkList* List, int* pos, int value) {
	if (List) {
		linkList* temp = List;
		*pos = 0;
		while (temp->next!=NULL) {
			(*pos)++;
			if (temp->next->data == value)
				return true;
			temp = temp->next;
		}
		printf("not found\n");
		return false;
	}
	printf("the list doesn't esist\n");
	return false;
}

int getElem(linkList* List, int pos, int* value) {
	if (pos<1) {
		printf("abnormal position\n");
		return false;
	}
	 if (List) {
		linkList* temp = List;
		int position = 0;
		while (temp->next != NULL) {
			position++;
			if (position == pos) {
				*value = temp->next->data;
				return true;
			}
			temp = temp->next;
		}
		printf("abnormal position\n");
		return false;
	}
	printf("the list doesn't exist\n");
	return false;
}

int listInsert(linkList* List, int pos, int value) {
	if (pos < 1) {
		printf("abnormal postion\n");
		return false; 
	}
	if (List) {
		linkList* temp = List;
		int position = 0;
		while (temp->next != NULL && position < pos - 1) {
			position++;
			temp = temp->next;
		}
		if (pos - 1 == position) {
			linkList* new_node = (linkList*)malloc(sizeof(linkList));
			new_node->data = value;
			new_node->next = temp->next;
			temp->next = new_node;
			return true;
		}
		printf("abnormal position\n");
		return false;
	}
	printf("the list doesn't exist\n");
	return false;
}

int listDelete(linkList* List, int pos, int* value) {
	if (pos < 1) {
		printf("abnormal position\n");
		return false;
	}
	if (List) {
		linkList* temp = List;
		int position = 0;
		while (temp->next != NULL && position < pos - 1) {
			position++;
			temp = temp->next;
		}
		if (temp->next != NULL && position == pos - 1) {
			*value = temp->next->data;
			linkList* deleted_node=temp->next;
			temp->next = temp->next->next;
                        freee(deleted_node);
			return true;
		}
		printf("abnormal position\n");
		return false;
	}
	printf("the list doesn't exist\n");
	return false;
}

int Empty(linkList* List) {
	if (List) {
		if (List->next == NULL)
			return true;
		return false;
	}
	printf("the list doesn't exist\n");
	return -1;
}

linkList* destroy(linkList* List) {
	if (List) {
		linkList* front = List;
		linkList* rear = List->next;
		while (rear->next != NULL) {
			free(front);
			front = rear;
			rear = rear->next;
		}
		free(rear);
		return NULL;
	}
	printf("the list doesn't exist\n");
	return NULL;
}

void print(linkList* List) {
	if (List) {
		printf("data:");
		linkList* temp = List;
		while (temp->next != NULL) {
			printf("%d ", temp->next->data);
			temp=temp->next;
		}
		printf("\n");
		return;
	}
	printf("the list doesn't exist\n");
}

int main() {
	linkList* List = NULL;
	List = init(List);
	if (List) {
		int size;
		int pos;
		int value;
		listInsert(List, 1, 1);
		listInsert(List, 2, 2);
		listInsert(List, 3, 3);
		listInsert(List, 4, 4);
		listInsert(List, 5, 5);
		listInsert(List, 6, 6);
		print(List);
		if ((size = length(List)) != -1)
			printf("size = %d\n", size);
		if (loacateElem(List, &pos, 3))
			printf("pos = %d\n", pos);
		if (getElem(List, 6, &value))
			printf("value = %d\n", value);
		if (listDelete(List, 7, &value))
			printf("deleted value = %d", value);
		print(List);
		linkList* temp = NULL;
		if (Empty(temp))
			printf("the list is empty\n");
		if (!(List = destroy(List)))
			printf("the list has been destroyed\n");
	}
	return 0;
}

二.运行结果

三.遇到的问题

2.1.删除节点时,首先是找到该节点前面的元素,然后通过指针将其指向该节点后面的元素。

2.1.1.当有n个节点,遇到越界数据n+1时,越界数据的前一个元素是n,但越界数据本身不存在(NULL),此时需要引用越界数据的下一个节点,就会对空指针进行操作。
2.1.2.因此,我们需要在找到节点的上一个元素时,判断这个元素的下一个位置是否为NULL。
posted @ 2023-03-12 20:54  彭乐祥  阅读(12)  评论(0编辑  收藏  举报