首页 > 其他分享 >Doubly linked list【1月17日学习笔记】

Doubly linked list【1月17日学习笔记】

时间:2024-01-17 20:01:01浏览次数:21  
标签:node run 17 temp list next Doubly NULL 节点

点击查看代码
//Doubly linked list
#include<iostream>
using namespace std;
struct node {
	int data;
	node* next;
	node* prev;
};//定义双向链表结构体
node* A;

node* getnewnode(int x) {
	node* temp = new node;
	temp->data = x;
	temp->prev = NULL;
	temp->next = NULL;
	return temp;
}//创建节点函数
void insertathead(int x) {
	node* temp = getnewnode(x);
	if (A == NULL) {
		A = temp;
		return;//不要忘记return//或者用else
	}//头指针为空时
	A->prev = temp;//1节点头部指向新节点
	temp->next = A;//新节点尾巴指向1节点
	A = temp;//头指针指向新节点//头指针与新节点不是双向,新节点头部默认指向NULL
}

void insertattail(int x) {
	node* temp = getnewnode(x);
	if (A == NULL) {
		A = temp;
	}
	else {
		node* run = A;
		while (run->next != NULL) {
			run = run->next;
		}
		run->next = temp;
		temp->prev = run;//新节点头部指向末节点
	}
}

void print() {
	node* run = A;
	while (run != NULL) {
		cout << run->data<<" ";
		run = run->next;
	}
	cout << endl;
}

void reverseprint() {
	if (A == NULL)  return;//empty list,exit
	node* run = A;
	while (run->next != NULL) {
		run = run->next;
	}//going to last node
	while (run != NULL) {
		cout << run->data << " ";
		run = run->prev;
	}//traversing backward uing prev pointer,结束时run指向NULL(run指向NULL:打印模式)
	cout << endl;
}
int main() {
	A = NULL;
	insertathead(2); print(); reverseprint();
	insertathead(4); print(); reverseprint();
	insertathead(6); print(); reverseprint();
	insertattail(-2); print(); reverseprint();
	insertattail(-4); print(); reverseprint();
	insertattail(-6); print(); reverseprint();
}

标签:node,run,17,temp,list,next,Doubly,NULL,节点
From: https://www.cnblogs.com/whvivy/p/17971050

相关文章

  • Solution Set【2024.1.17】
    [ABC298Ex]SumofMinofLength在下文的推导中假设\(\operatorname{depth}_{L}\le\operatorname{depth}_R\),若不符合则交换\(L\)和\(R\)。首先我们可以发现,我们可以找到\(R\)的\(\left\lfloor\frac{\operatorname{dist}\left(L,R\right)}{2}\right\rfloor\)级祖先......
  • 2024年3月17日DAMA-CDGP数据治理专家认证考试开始报名
    DAMA认证为数据管理专业人士提供职业目标晋升规划,彰显了职业发展里程碑及发展阶梯定义,帮助数据管理从业人士获得企业数字化转型战略下的必备职业能力,促进开展工作实践应用及实际问题解决,形成企业所需的新数字经济下的核心职业竞争能力。DAMA是数据管理方面的认证,帮助数据从业者提升......
  • cyclone list to python tuple!
    背景python有list和tuplecyclone只有list(被称为array)pythonreturn多个值pythontuplecyclonelistpythontocyclonepythonlistto_cyclonelistto_pythontuple!单个元素的tuple末尾有,确实是tuplepythontupleto_cyclonelist证明cyclone的list(被称为array)其......
  • 全视通养老系统 -17年专注智慧养老守护产品-机构智慧养老解决方案
       随着人口老龄化的加剧,养老问题已经成为社会关注的焦点。传统的养老方式已经无法满足现代老年人的需求,而智慧养老作为一种新型的养老模式,正逐渐受到广泛的关注和应用。智慧养老是指利用先进的信息技术,为老年人提供更加智能化、便捷化的养老服务,从而提高老年人的生活质量。 ......
  • Print linked list using recursion【1月17日学习笔记】
    点击查看代码//Printlinkedlistusingrecursion#include<iostream>usingnamespacestd;structnode{ intdata; node*next;};voidprint(node*p){ if(p==NULL)return;//递归中止条件 cout<<p->data<<""; print(p->next)......
  • Reverse a linked list【1月17日学习笔记·】
    点击查看代码//Reverssealinkedlist#include<iostream>usingnamespacestd;structnode{ intdata; node*next;};node*A;voidreverse(){ node*next;//用于保存下一个·节点地址以便索引 node*current=A;//当前索引 node*prev=NULL;//保存上一个节点......
  • [CF1707E] Replace
    Replace题面翻译题目描述给定一个长为\(n\)的序列\(a_1,\ldots,a_n\),其中对于任意的\(i\)满足\(1\leqa_i\leqn\)。定义一个二元组函数如下:\[f((l,r))=(\min\{a_l,\ldots,a_r\},\max\{a_l,\ldots,a_r\})(l\leqr)\]你需要回答\(q\)次询问,每次给定\((l_i,r_i)\)......
  • Delete d node at nth position【1月17日学习笔记】
    点击查看代码//Deletednodeatnthposition#include<iostream>usingnamespacestd;structnode{ intdata; node*next;};node*A;voidinsert(intx){ node*temp=newnode; temp->data=x; temp->next=NULL; if(A==NULL){ A=temp;......
  • (Python)每日代码||2024.1.17||函数中给列表形参默认值时,该默认列表在函数中的改变会
    deff(x,li=[1]):print(id(li))li.append(x)print(li)f('a')#第一次调用函数print()f('b')#第二次调用函数print()f('a',[])#第三次调用函数print()f('b',[2,2])#第四次调用函数print()f('a')#第五次调用函数'''输出14......
  • Inserting a node at nth position【1月17日学习笔记】
    点击查看代码//Insertinganodeatnthposition#include<iostream>usingnamespacestd;structnode{intdata;node*next;};node*A;//全局头指针voidinsert(intx,intn){node*temp=newnode;//temp是局部变量,在stack区,每次调用更新temp-......