自学内容网 自学内容网

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!


原文地址:https://blog.csdn.net/liloqyqx/article/details/140620430

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!