通过C++进行单链表的创建、打印以及利用栈实现逆序打印
ListNode定义
struct ListNode{
int val;
ListNode *next;
ListNode() : val(0),next(NULL){
}
ListNode(int x) : val(x), next(NULL){
}
ListNode(int x, ListNode *next) : val(x), next(next){
}
};
单链表的创建和打印
#include <iostream>
using namespace std;
//定义结构体
struct ListNode{
int val;
ListNode* next;
};
class operateList
{
public:
/*创建单链表*/
void createList(ListNode *head)
{
int i;
ListNode* phead=head; //不破坏头指针
for(i=1;i<10;i++){
ListNode* node=new ListNode;
node->val=i;
node->next=NULL;
phead->next=node;
phead=node;
}
cout<<"创建成功\n";
}
/*打印链表*/
void printList(ListNode* head)
{
ListNode* phead=head;
while(phead->next!=NULL)
{
cout<<phead->val<<" ";
phead=phead->next;
}
cout<<phead->val;
}
};
int main()
{
ListNode* head=new ListNode;
operateList ll;
head->val=0;
head->next=NULL;
ll.createList(head);
ll.printList(head);
return 0;
}
逆序打印单链表的方式
#include <iostream>
#include <vector>
#include <stack>
using namespace std;
//定义结构体
struct ListNode{
int val;
ListNode* next;
};
class operateList
{
public:
/*创建单链表*/
void createList(ListNode *head)
{
int i;
ListNode* phead=head; //不破坏头指针
for(i=1;i<10;i++){
ListNode* node=new ListNode;
node->val=i;
node->next=NULL;
phead->next=node;
phead=node;
}
cout<<"链表创建成功!\n";
}
/*打印链表*/
void printList(ListNode* head)
{
ListNode* phead=head;
while(phead->next!=NULL)
{
cout<<phead->val<<" ";
phead=phead->next;
}
cout<<phead->val<<"\n";
}
/*利用栈先进后出的思想*/
vector<int> printListInverseByStack(ListNode* head){
vector<int> result;
stack<int> arr;
int i;
ListNode* phead=head;
while(phead->next!=NULL)
{
arr.push(phead->val);
phead=phead->next;
}
arr.push(phead->val); //最后一个元素
while(!arr.empty())
{
result.push_back(arr.top());
arr.pop();
}
return result;
}
void printVector(vector<int> result)
{
int i;
for(i=0;i<result.size();i++)
cout<<result[i]<<" ";
cout<<"\n";
}
};
int main()
{
ListNode* head=new ListNode;
vector<int> result;
operateList ll;
head->val=0;
head->next=NULL;
ll.createList(head);
ll.printList(head);
//利用栈逆序
result=ll.printListInverseByStack(head);
cout<<"利用栈逆序的结果为:\n";
ll.printVector(result);
return 0;
}
示例
#include <iostream>
#include <assert.h>
#include <vector>
#include <algorithm>
using namespace std;
struct ListNode
{
int val; //当前节点的值
ListNode *next; //指向下一个节点的指针
ListNode() : val(0), next(nullptr) {
} //初始化当前结点值为默认值0,指针为空
ListNode(int x) : val(x), next(nullptr) {
} //初始化当前结点值为x,指针为空
ListNode(int x, ListNode *next) : val(x), next(next) {
} //初始化当前结点值为x,下一个绩点为next
};
class Solution
{
public:
//创建长度为len的单向链表
void createList(ListNode *head,int len){
for(int i=1;i<len;i++) //len-1个节点,加上head节点共len个
{
ListNode *node=new ListNode; //每次都需要实例化一个ListNode
node->val=i*i; //为节点赋值
node->next=nullptr;
head->next=node; //head指向下一个节点(即当前节点)
head=node; //将当前节点设为head
}
cout<<"Create a new ListNode with len of "<<len<<" successfully."<<endl;
}
//打印链表(顺序)
void printList(ListNode *head){
if(head==nullptr)
cout<<"empty list."<<endl;
else
while(head!=nullptr)
{
cout<<head->val<<" ";
head=head->next;
}
cout<<endl;
}
//打印链表(逆序)
void reversePrintList(ListNode *head){
//因为ListNode只能根据next单向索引,无法逆向回溯,所以只能将节点数值存在vector中反向输出。
//目前只想到了这种方法。
if(head==nullptr)
{
cout<<"empty list."<<endl;
exit(1);
}
else
{
vector<int> node;
while(head!=nullptr)
{
node.push_back(head->val);
head=head->next;
}
while(!node.empty())
{
//先输出node中的最后一个元素,再删除最后一个元素。而不是先对node做reverse再正向输出。
cout<<node.back()<<" ";
node.pop_back();
}
cout<<endl;
}
}
//在链表尾节点添加一个新节点
void pushBack(ListNode *head,int val){
ListNode *node=new ListNode(val,nullptr); //要添加的新节点
if(head==nullptr)
head=node;
else
{
while(head->next!=nullptr) //while循环结束后head就是尾结点了
head=head->next;
head->next=node;
}
}
//更改链表尾节点数值
void changeBackValue(ListNode *head,int val){
assert(head!=nullptr);
while(head->next!=nullptr) //while循环结束后head就是尾结点了
head=head->next;
head->val=val;
}
//删除链表尾节点
void popBack(ListNode *head){
assert(head!=nullptr);
while(head->next->next!=nullptr) //while循环结束后head是倒数第二个节点,其next指向尾节点
head=head->next;
head->next=nullptr; //删除尾节点
//注意不要直接delete尾结点,因为尾结点的next是nullptr,直接delete nullptr会输出很多乱码。
}
//删除链表中节点值等于指定值的节点(不包括头节点)
void deleteNode(ListNode *head, int val) {
assert(head != nullptr);
ListNode *node = head; //copy一份链表
while (head->next != nullptr)
{
if (head->next->val == val)
node->next=head->next->next;
head=head->next;
node=node->next;
}
}
//清空列表
void clearList(ListNode *head){
head->next=nullptr; //清楚头结点之后的所有节点
//清空列表的功能一直不知道怎么实现,头结点不知道怎么删除。
}
};
int main()
{
Solution solution;
ListNode *listnode=new ListNode(5,nullptr); //初始化链表的head节点
solution.printList(listnode); // 5
solution.createList(listnode,5);
solution.printList(listnode); // 5 1 4 9 16
solution.pushBack(listnode,30);
solution.printList(listnode); // 5 1 4 9 16 30
solution.reversePrintList(listnode); // 30 16 9 4 1 5
solution.changeBackValue(listnode,88);
solution.printList(listnode); // 5 1 4 9 16 88
solution.popBack(listnode);
solution.printList(listnode); // 5 1 4 9 16
solution.pushBack(listnode,101);
solution.printList(listnode); // 5 1 4 9 16 101
solution.deleteNode(listnode,9);
solution.printList(listnode); // 5 1 4 16 101
solution.clearList(listnode);
solution.printList(listnode); // 5
cout<<"END"<<endl;
return 0;
}
程序输出: