好久没有写c++的链表了
刷题的时候要用所以浅写一下:
#include <iostream>
#include <string>
#include <stack>
using namespace std;
struct ListNode
{
int m_nKey;
ListNode *m_pNext;
};
int main()
{
ListNode *L = new ListNode;
L->m_pNext = nullptr;
ListNode *p = L;
int n;
int num;
cin >> n;
for (int i = 0; i < n; i++)
{
cin >> num;
ListNode *temp = new ListNode;
temp->m_nKey = num;
p->m_pNext = temp;
p = temp;
p->m_pNext = nullptr;
}
ListNode *q = L->m_pNext;
while (q != nullptr)
{
cout << q->m_nKey << " ";
q = q->m_pNext;
}
}
反正就常规操作。