作者:小白石 | 来源:互联网 | 2023-06-06 23:49
提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
文章目录
- 前言
- 一、什么是链表
- 二、链表的分类
- 链表的库函数(模板函数)
- 总结
前言
在c语言数据结构中链表作为一种很基础但是又重要的结构。在学生管理系统中就用到了链表,而在c++中,链表也是一种重要的存储结构,不过在c++中有很多库函数,可以减少我们的麻烦,下面就给你们介绍一下;
提示:以下是本篇文章正文内容,下面案例可供参考
一、什么是链表
链表是动态存储分配的数据结构。链表的每个结点都是一个结构体变量,包含数据域(存放数据本身)和指针域(存放下一个结点)的地址;
结构体可以定义为以下方式:
struct Node{
int data;
Node*next;
};
二、链表的分类
顺序链表
由多个结点组成的一条线性的数据结构;
链表结构图
代码如下(示例):
#include
using namespace std;
struct Node
{int data;Node* next;
};
Node* creatNode(int n)
{Node* headnode = new Node;headnode->next = NULL;Node* p = headnode;while (n--){Node* q = new Node;cin >> q->data;q->next = p->next;p->next = q;p = q;}return headnode;
}
void printList(Node* headnode)
{Node* pmove=headnode->next;while (pmove){printf("%5d", pmove->data);pmove = pmove->next;}
}
int main(void)
{int n;Node *list;cin >> n;list = creatNode(n);printList(list);
}
2.逆序链表
其实说白了逆序链表就是先进入的结点向后走,后进的结点在前,然后遍历,就可以得到逆序链表;
代码如下(示例):
#include
#include
using namespace std;
struct Student
{int data;Student* next;
};
Student* Init_headnode()
{Student* headnode = new Student;headnode->next = NULL;return headnode;
}
void creatList(Student* headnode, int n)
{assert(headnode != NULL);Student* p = headnode;while (n--){Student* newnode = new Student;cin >> newnode->data;newnode->next = p->next;p->next = newnode;}
}
void printList(Student* headnode)
{assert(headnode != NULL);Student* pmove = headnode->next;while (pmove){printf("%5d", pmove->data);pmove = pmove->next;}
}
int main(void)
{Student* headnode= Init_headnode();int n;cin >> n;creatList(headnode, n);printList(headnode);
}
通过这两个例子,我们可以认识到链表,链表的操作还有很多,比如删除特定的值,或者特定位置插入,删除表头,清空链表,判空以及排序等一系列的操作方式,但是如果每个操作都定义一个函数,这样太过麻烦,那我们有没有什么模板来简化这些问题喃?这就是我们今天讲的STL(模板)之链表(list)
链表的库函数(模板函数)
代码演示:
#define _CRT_SECURE_NO_WARNINGS 1
#include
#include
using namespace std;
list<int>createList(int n)
{list<int>myList;int t;for (int i &#61; 0; i < n; i&#43;&#43;){cin >> t;myList.push_back(t);}return myList;
}
void prtList(list<int>myList)
{list<int>::iterator it;for (it &#61; myList.begin(); it !&#61; myList.end(); it&#43;&#43;){if (it !&#61; myList.begin())cout << " ";cout << *it; }cout << endl;
}
bool cmp(int a, int b)
{return a >&#61; b;
}
int main(void)
{int n;cin >> n;list<int>myList &#61; createList(n);prtList(myList);cout << myList.size() << endl;myList.push_front(3);myList.push_back(3);prtList(myList);myList.reverse();prtList(myList);myList.merge(myList,cmp);myList.sort(cmp);prtList(myList);
}
链表中的这些函数不用去背&#xff0c;一般在编译器上会有提示&#xff0c;不过要熟练掌握他的格式和用意。这些模板为我们解决一些问题减少了很多时间&#xff0c;方便&#xff0c;所以熟练掌握这些很重要;
STL之list的应用
#include
#include
using namespace std;
struct Node
{string name;int age;
};
list<Node>creatListQueue(int n)
{list<Node>myList;Node t;for (int i &#61; 0; i < n; i&#43;&#43;){cin >> t.name >> t.age;myList.push_back(t);}return myList;
}
void prtList(list<Node>myList)
{list<Node>::iterator it;for (it &#61; myList.begin(); it !&#61; myList.end(); it&#43;&#43;){if (it !&#61; myList.begin())cout << " ";cout << it->name << it->age;}cout << endl;
}
bool cmp(Node a, Node b)
{if (a.age !&#61; b.age)return a.age > b.age;return a.name < b.name;
}
int main(void)
{int n;cin >> n;list<Node>myList&#61;creatListQueue(n);prtList(myList);myList.sort(cmp);prtList(myList);myList.reverse();prtList(myList);Node t;cin >> t.name >> t.age;myList.insert(myList.begin(), t);prtList(myList);
}
总结
前面我们学习了STL之vector&#xff0c;仔细观察&#xff0c;这些模板之间都有很多相似之处&#xff0c;所以不需要去背这些代码&#xff0c;学会活学活用才最好&#xff0c;本节的list模板就讲到这里&#xff0c;其中有很多的模板函数在我们去实现代码的时候会有很大的帮助&#xff0c;尤其是一些常用的函数&#xff0c;上面我列举的函数都比较常用&#xff0c;需要熟练掌握;