C++链表

        C++中有两种结构:1 线性结构(如数组)2 链式结构(如链表

        数组大家一定不陌生,几乎所有比赛题目都有用到,所以就不讲了。

        如果不会,我后续会写一篇关于数组的文章。

        说回链表,链式结构和线性结构有什么区别呢?

        看下图:

        (线性结构)

       

(链式结构)

今天的主角链表是由一个个节点连接成的,一个节点包含一个数字data(1),用来存放当前节点的数值,还有一个指针(2),用来指向下一个节点。

要定义一个结构体来当作节点:

        struct Node{

                int data;

                Node *next;

        };

链表有一个指针指向链表的头部。

        Node *head = new Node;(刚开始,给头指针开辟一个新空间)

一切任务都完成了,告辞

接下来是实现一些功能了:

//在链表末尾追加
void add(int x){
	if(head != NULL){
		Node *a = new Node;
		a->data = x;
		a->next = NULL;
		Node *p = head;
		while(p->next != NULL){
			p = p->next;
		}
		p->next=a;
	}
	else{
		head = new Node;
		head->data = x;
		head->next = NULL;
	}
}
//在中间追加元素(在第n个元素的位置)
void insert(int n,int x){
	Node *d = new Node;
	d->data=x;
	d->next=NULL;
	//如果是头结点
	if(n==1){
		d->next = head;
		head = d;
	}
	else{
		Node *p = head;
		for(int i=1;i<=n-2;i++){
			p = p->next;
			if(p == NULL){
				break;
			}
		}
		if(p == NULL){
			cout<<"n有误,是尾节点增加"<<endl;
			add(x);
		}
		else{
			d->next = p->next;
			p->next = d;
		}
	}
}
//删除链表中第一个出现的data
void deldata(int data){
	Node *p = head,*pre = NULL;
	while(p != NULL){
		if(data == p->data){
			if(p == head){
				head = p->next;
			}
			else{
				pre->next = p->next;
			}
			delete p;
			break;
		}
		pre = p;
		p = p->next;
	}
}
//删除某个位置的元素
void delpos(int n){
	Node *p = head,*t;
	//如果要删除头结点
	if(n==1){
		if(head != NULL){
			head = head->next;
			delete p;
		}else{
			cout<<"链表空"<<endl;
		}
	}else{
		//移动到要删除位置之前的节点
		for(int i=1;i<=n-2;i++){
			p = p->next;
			if(p == NULL) break;
		}
		if(p == NULL || p->next == NULL){
			cout<<"n的值有误!"<<endl;
		}else{
			t = p->next;
			p->next = t->next;
			delete t;
		}
	}
}
//输出链表
void display(){
	Node *p = head;
	while(p != NULL){
		cout<<p->data<<" ";
		p = p->next;
	}
	cout<<endl;
}

整合后:

#include<iostream>
using namespace std;
struct Node{
	int data;
	Node *next;
};
Node *head = NULL;
//在链表末尾追加
void add(int x){
	if(head != NULL){
		Node *a = new Node;
		a->data = x;
		a->next = NULL;
		Node *p = head;
		while(p->next != NULL){
			p = p->next;
		}
		p->next=a;
	}
	else{
		head = new Node;
		head->data = x;
		head->next = NULL;
	}
}
//在中间追加元素(在第n个元素的位置)
void insert(int n,int x){
	Node *d = new Node;
	d->data=x;
	d->next=NULL;
	//如果是头结点
	if(n==1){
		d->next = head;
		head = d;
	}
	else{
		Node *p = head;
		for(int i=1;i<=n-2;i++){
			p = p->next;
			if(p == NULL){
				break;
			}
		}
		if(p == NULL){
			cout<<"n有误,是尾节点增加"<<endl;
			add(x);
		}
		else{
			d->next = p->next;
			p->next = d;
		}
	}
}
//删除链表中第一个出现的data
void deldata(int data){
	Node *p = head,*pre = NULL;
	while(p != NULL){
		if(data == p->data){
			if(p == head){
				head = p->next;
			}
			else{
				pre->next = p->next;
			}
			delete p;
			break;
		}
		pre = p;
		p = p->next;
	}
}
//删除某个位置的元素
void delpos(int n){
	Node *p = head,*t;
	//如果要删除头结点
	if(n==1){
		if(head != NULL){
			head = head->next;
			delete p;
		}else{
			cout<<"链表空"<<endl;
		}
	}else{
		//移动到要删除位置之前的节点
		for(int i=1;i<=n-2;i++){
			p = p->next;
			if(p == NULL) break;
		}
		if(p == NULL || p->next == NULL){
			cout<<"n的值有误!"<<endl;
		}else{
			t = p->next;
			p->next = t->next;
			delete t;
		}
	}
}
//输出链表
void display(){
	Node *p = head;
	while(p != NULL){
		cout<<p->data<<" ";
		p = p->next;
	}
	cout<<endl;
}
int main() {
  	int order,x,p;
  	cout<<"输入指令:"<<endl;
  	while(1){
		cout<<"1.追加 2.插入 3.删除值 4.删除位置 5.显示!"<<endl;
		cin>>order;
		if(order==1){
			cin>>x;
			add(x);
			display();
		}else if(order==2){
			cin>>p>>x;
			insert(p,x);
			display();
		}else if(order==3){
			cin>>x;
			deldata(x);
			display();
		}else if(order==4){
			cin>>x;
			delpos(x);
			display();
		}else if(order==5){
			display();
		}
	}
    return 0;
}

下课!

#include<bits/stdc++.h>
using namespace std;
void y(){
	for(int i = 0;i < 9999;i++){
		for(int j = 0;j < 30000;j++){
	 
		}
	}
}
void ren()
{
cout<<"  &&&&   "<<endl;
cout<<" &&&&&&  "<<endl;
cout<<"&&&&&&&& <==没关注的人"<<endl;
cout<<" &&&&&&  "<<endl;
cout<<"  &&&&   "<<endl;
cout<<" & && &  "<<endl;
cout<<"&  &&  & "<<endl;
cout<<"&  &&  & "<<endl;
cout<<"   &&    "<<endl;
cout<<" &    &  "<<endl;
cout<<"&      & "<<endl;
cout<<"&      & "<<endl;
 }
void ren2()
{
cout<<"  &&&&   ——————"<<endl;
cout<<" &&&&&&  |  我好   |"<<endl;
cout<<"&&&&&&&& |  厉害   |"<<endl;
cout<<" &&&&&&  ——————"<<endl;
cout<<"  &&&&   "<<endl;
cout<<" & && &  "<<endl;
cout<<"&  &&  & "<<endl;
cout<<"&  &&  & "<<endl;
cout<<"   &&    "<<endl;
cout<<" &    &  "<<endl;
cout<<"&      & "<<endl;
cout<<"&      & "<<endl;
}
void ren3()
{
cout<<"  &&&&                    ~"<<endl;
cout<<" &&&&&&                  ~ "<<endl;
cout<<"&&&&&&&&                ~  "<<endl;
cout<<" &&&&&&                ~   "<<endl;
cout<<"  &&&&                 ~   "<<endl;
cout<<" & && &               ~    "<<endl;
cout<<"&  &&  &              ~    "<<endl;
cout<<"&  &&  &               ~   "<<endl;
cout<<"   &&                  ~   "<<endl;
cout<<" &    &                 ~  "<<endl;
cout<<"&      &                 ~ "<<endl;
cout<<"&      &                  ~"<<endl;
 } 
void ren4()
{
cout<<"  &&&&   ——————     ~"<<endl;
cout<<" &&&&&&  |  那是   |     ~ "<<endl;
cout<<"&&&&&&&& |什么?   |    ~  "<<endl;
cout<<" &&&&&&  ——————  ~   "<<endl;
cout<<"  &&&&                 ~   "<<endl;
cout<<" & && &               ~    "<<endl;
cout<<"&  &&  &              ~    "<<endl;
cout<<"&  &&  &               ~   "<<endl;
cout<<"   &&                  ~   "<<endl;
cout<<" &    &                 ~  "<<endl;
cout<<"&      &                 ~ "<<endl;
cout<<"&      &                  ~"<<endl;
 } 
void ren5()
{
cout<<"  &&&&   —————— ~"<<endl;
cout<<" &&&&&&  |  那是   | ~ "<<endl;
cout<<"&&&&&&&& |什么?   |~  "<<endl;
cout<<" &&&&&&  ————  ~   "<<endl;
cout<<"  &&&&             ~   "<<endl;
cout<<" & && &           ~    "<<endl;
cout<<"&  &&  &          ~    "<<endl;
cout<<"&  &&  &           ~   "<<endl;
cout<<"   &&              ~   "<<endl;
cout<<" &    &             ~  "<<endl;
cout<<"&      &             ~ "<<endl;
cout<<"&      &              ~"<<endl;
 } 
void ren6()
{
cout<<"  &&&&   ———   ~"<<endl;
cout<<" &&&&&&  |  那是 ~ "<<endl;
cout<<"&&&&&&&& |什么?~  "<<endl;
cout<<" &&&&&&  ——  ~   "<<endl;
cout<<"  &&&&         ~   "<<endl;
cout<<" & && &       ~    "<<endl;
cout<<"&  &&  &      ~    "<<endl;
cout<<"&  &&  &       ~   "<<endl;
cout<<"   &&          ~   "<<endl;
cout<<" &    &         ~  "<<endl;
cout<<"&      &         ~ "<<endl;
cout<<"&      &          ~"<<endl;
 } 
 void ren7()
{
cout<<"  &&&&   —— ~"<<endl;
cout<<" &&&&&&  |   ~ "<<endl;
cout<<"&&&&&&&& |什~  "<<endl;
cout<<" &&&&&&    ~   "<<endl;
cout<<"  &&&&     ~   "<<endl;
cout<<" & && &   ~    "<<endl;
cout<<"&  &&  &  ~    "<<endl;
cout<<"&  &&  &   ~   "<<endl;
cout<<"   &&      ~   "<<endl;
cout<<" &    &     ~  "<<endl;
cout<<"&      &     ~ "<<endl;
cout<<"&      &      ~"<<endl;
 } 
void canju()
{
	cout<<"此处省略";
	for(int i = 0;i < 5;i++){
		cout<<"·";
		y();
		cout<<"·";
		y();
		cout<<"·";
		y();
		system("cls");
		cout<<"此处省略";
	}
 } 
void canju2()
{
	for(int i = 0;i<1000;i++){
		for(int j = 0;j < i;j++){
			cout<<" ";
		}
		cout<<"啊!";
	}
}
 
int main()
{
	ren();
	y();
	system("cls");
	ren2();
	y(); 
	system("cls");
	ren3();
	y();
	system("cls");
	ren4();
	y();
	system("cls");
	ren5();
	y(); 
	system("cls");
	ren6();
	y();
	system("cls");
	ren7();
	y();
	system("cls");
	canju();
	y();
	system("cls");
	canju2();
return 0;
}

OK!

相关推荐

  1. c实现

    2024-07-23 07:26:05       50 阅读
  2. c#

    2024-07-23 07:26:05       47 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-23 07:26:05       171 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-23 07:26:05       189 阅读
  3. 在Django里面运行非项目文件

    2024-07-23 07:26:05       157 阅读
  4. Python语言-面向对象

    2024-07-23 07:26:05       170 阅读

热门阅读

  1. “微软蓝屏”事件暴露的网络安全问题

    2024-07-23 07:26:05       38 阅读
  2. 【SpringBoot】第3章 系统配置之日志配置

    2024-07-23 07:26:05       30 阅读
  3. Hopfield 网络简介

    2024-07-23 07:26:05       34 阅读
  4. 为什么要学习网安技术?

    2024-07-23 07:26:05       28 阅读
  5. R语言读取txt和csv只有1列时

    2024-07-23 07:26:05       32 阅读
  6. 2024 7.15~7.21 周报

    2024-07-23 07:26:05       34 阅读
  7. 力扣 102题 二叉树的层次遍历 记录

    2024-07-23 07:26:05       28 阅读
  8. 【通俗理解】对数边缘似然:公式与应用

    2024-07-23 07:26:05       32 阅读
  9. mariadb安装centos再次踩坑

    2024-07-23 07:26:05       33 阅读
  10. PostgreSQL 8.4 ROW_NUMBER()函数

    2024-07-23 07:26:05       28 阅读
  11. 通过队列名寻找某队列-linux

    2024-07-23 07:26:05       26 阅读
  12. springboot业务逻辑写在controller层吗

    2024-07-23 07:26:05       28 阅读
  13. linux本地互传文件

    2024-07-23 07:26:05       27 阅读
  14. 异步TCP服务器;异步TCP客户端

    2024-07-23 07:26:05       29 阅读