链表以及链表的各种操作(增删改查)

关于链表 

链表(Linked list)是一种常见的基础数据结构,是一种线性表,但是并不会按线性的顺序存储数据,而是在每一个节点里存到下一个节点的指针(Pointer)。由于不必须按顺序存储,链表在插入的时候可以达到O(1)复杂度,比另一种线性表顺序表快得多,但是查找一个节点或者访问特定编号的节点则需要O(n)的时间,而顺序表相应的时间复杂度分别是O(logn)和O(1)

单链表

第一个部分保存或者显示关于节点的信息,第二个部分存储下一个节点的地址。单向链表只可向一个方向遍历

链表还用来构建许多其它数据结构,如堆栈,队列等等。

Singly-linked-list.svg

#include<iostream>
using namespace std;
typedef struct Node {//定义结构体Node
	int data;
	struct Node* next;
}* LinkedList,LNode;//别名

void CreateLinkedList(LinkedList &L, int &length) {/*创建链表 注意此处LinkedList &L为引用,
					引用在C++里面是确保传入的参数可以被修改。形参是无关的单引用可以使其修改*/
	L = (LinkedList)malloc(sizeof(LNode));//初始化,头节点创建
	L->data = 0;
	L->next = NULL;

	LinkedList tail = L;//尾节点

	cout << "The length of the LinkedList is" <<length << ":\n";
	
	while (length--) {//尾插迭代循环创建链表
		cout << " input number :\n ";

		//LinkedList temp = (LinkedList)malloc(sizeof(LNode));
		LinkedList temp = new LNode;
		cin >> temp->data;
		tail->next= temp;
		tail = temp;
		temp = NULL;
		L->data++;
	}
	tail->next = NULL;
}
bool getElem(int &e, int i, LinkedList L){//判断元素是否在相应围殴之
	while (L !=NULL && i>0){
		i--;
		L = L->next;
	}
	if (i == 0 && L !=NULL){
		e = L->data;
		return true;
	}
	else 
		return false;
}
bool insertElem(int e, int i, LinkedList L) {//插入数据
	if (i > L->data || i <= 0)
		return false;
	else {
		L->data++;
		while (i > 1) {
			i--;
			L = L->next;
		}
		LinkedList temp = (LinkedList)malloc(sizeof(LNode));
		temp->data = e;
		temp->next = L->next;
		L->next = temp;
		return true;
	}
}

bool DeleteElem(int i, LinkedList L) {//删除
	if (i > L->data || i < 1)
		return false;
	else {
		L->data--;
		while (i > 1) {
			i--;
			L = L->next;
		}
		LinkedList temp = (LinkedList)malloc(sizeof(LNode));
		temp = L ->next;
		L->next = temp->next;
		free(temp);
		return true;
	}
}
bool destoryLinkedList(LinkedList &L) {//销毁链表
	if (L->next != NULL)
		destoryLinkedList(L->next);
	free(L);
	return true;
}
bool clearLinkedList(LinkedList &L) {//清空链表
	destoryLinkedList(L->next);
	L->data = 0;
	L->next = NULL;
	return true;
}
void printLinkedList(LinkedList L) {//打印输出
	LinkedList head = L->next;
	cout << "\n\t";
	while (head != NULL) {
		cout << "[" << head->data << "]";
		if(head->next)cout<< "-->";
		head = head->next;
	}
	cout << "\n"<<endl;
}
int main() {
	int ListSize, i, Elem,choice;
	void(*p)(LinkedList &L, int &length);//定义函数指针
	p= CreateLinkedList;
	LinkedList L;
	cout << " \t\t\tit's a small progam about LinkedList " << endl;
	cout << "\n\t\t how long do you want create of the LinkedList?\n                             ";
	cin >> ListSize; CreateLinkedList(L, ListSize);
next:
	cout << "(1)打印链表           (2)插入元素\n"
		 << "(3)删除元素           (4)清空链表\n"
		 << "(5)销毁链表           (6)存在元素\n";
	cin >> choice;
	
	 switch (choice) {//switch选择判断 ,goto循环指向
	case 1:
		cout << "these are elements you input \n";
		printLinkedList(L); goto next;
	case 2:
		cout << "input num and location\n";
		cin >> i;
		cin >> Elem;
		insertElem(Elem, i, L); goto next;
	case 3:
		cout << "input location\n";
		cin >> i;
		DeleteElem(i, L);  goto next;
	case 4:
		clearLinkedList(L); goto next;
	case 5:
		destoryLinkedList(L);  
		cout << "you destory the list!    create it(1) OR withdraw it " << endl;
		cin >> choice;
		cin >> Elem;
		if (choice == 1) {
			(*p)(L, Elem);//函数指针的使用
			goto next;
		}
		else return 0;
	case 6:
		cin >> Elem;
		cin >> i;
		cout<<getElem(Elem, i, L);
	}
	system("pause");
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41722524/article/details/86653049