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

关于链表 

链表(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;
}

循环链表

循环链表,注意将最后的尾指针指向头节点即可。 

#include<iostream>
using namespace std;
 
typedef struct node {     //定义单链表的节点 
	int data;
	struct node *next;
}*NodePointer, CirNode;
NodePointer head, rear;
//声明一个表头和尾指针 
class CircularList {
public :
	
	 NodePointer createList()   //初始化一个循环链表 
	{
		head = new CirNode;
		rear = head;
		rear->next = head;
		return head;
	}
	NodePointer createList(int n)  //创建一个循环链表 
	{
		NodePointer p;
		head = new CirNode;
		rear = head;
		for (int i = 1; i <= n; i++)
		{
			p = new CirNode;
			p->next = rear->next;
			rear->next = p;
			rear = p;
			cin >> p->data;
		}
		rear->next = head;
		return head;
	}
	int insertList(NodePointer head, int i, int x)
	{
		NodePointer p, q;
		int j = 0;
		p = head->next;
		while (p != head)
		{
			j++;
			p = p->next;
		}
		p = head;
		for (int j = 1; j < i; j++)
			p = p->next;
		if (i > j + 1 || i < 1)
		{
			printf("插入位置出错\n");
			return -1;
		}
		q = new CirNode;
		q->data = x;
		q->next = p->next;
		p->next = q;
		return 0;
	}
	int deleteList(NodePointer head, int i)   //删除位置i处的节点 
	{
		NodePointer p, q;
		p = head->next;
		int j = 0;
		while (p != head)
		{
			j++;
			p = p->next;
		}
		if (i<1 || i>j)
		{
			printf("删除位置出错\n");
			return -1;
		}
		p = head;
		for (j = 1; j < i; j++)
			p = p->next;
 
		q = p->next;
		p->next = p->next->next;
		free(q);
		return 0;
	}
	void printList(NodePointer head)    //输出循环链表的所有元素 
	{
		int cal = 0;
		NodePointer p;
		p = head->next;
		while (cal < 5)
		{
			printf(" %d ", p->data);
			p = p->next;
			if (p->next == head)
				p->next = head->next;
			cal++;
 
		}
	}
};
 
int main()
{
	
 
	CircularList* c = new CircularList;
	c->createList(2);
	c->printList(head);
	system("pause");
	return 0;
}

 

  • 1
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

吃瓜太狼

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值