单链表栈
#include<iostream>
using namespace std;
struct Node
{
int value;
Node* next;
};
struct Lnode
{
Node* L_Front;
Node* L_Rear;
};
struct Node* Create_Node(int data)
{
Node* node;
node = new Node;
node->value = data;
node->next = NULL;
return node;
}
void Init_Lnode(Lnode* q)
{
q->L_Front = q->L_Rear = NULL;
}
void Push_Lnode(Lnode* q, int data)
{
if (q->L_Front == NULL)
{
q->L_Front = q->L_Rear = Create_Node(data);
return;
}
Node* node;//这个地方不晓得为什么要在单独创建个,哪位大佬可以解释一下
node = Create_Node(data);
node->next = q->L_Front;
q->L_Front = node;
}
int Pop_Lnode(Lnode* q, int& data)
{
Node* pnode;
if (q->L_Front == NULL)
{
return 0;
}
if (q->L_Front == q->L_Rear)
{
q->L_Rear = NULL;
}
pnode = q->L_Front;
data = q->L_Front->value;
q->L_Front = q->L_Front->next;
delete pnode;
}
int Pop_Lnode_Frontnode(Lnode* q)
{
return q->L_Front->value;
}
int Pop_Lnode_Rearnode(Lnode* q)
{
return q->L_Rear->value;
}
int Length_Lnode(Lnode* q)
{
int Length = 0;
Node* p;
p = q->L_Front;
while (p)
{
p = p->next;
Length++;
}
return Length;
}
int Is_Empty(Lnode* q)
{
if (q->L_Front == NULL)
return 1;
else
return 0;
}
int main()
{
Lnode* q;
q = new Lnode;
Init_Lnode(q);
int data;
for (int i = 0; i < 4; i++)
{
Push_Lnode(q, i);
}
cout << "Frontnode = " << Pop_Lnode_Frontnode(q) << endl;
cout << "Rearnode = " << Pop_Lnode_Rearnode(q) << endl;
cout << "Length = " << Length_Lnode(q) << endl;
while (!Is_Empty(q))
{
Pop_Lnode(q, data);
cout << " " << data << " ";
}
return 0;
}
写这些代码主要是记录一下自己的学习历程,有错误的地方请大佬指正。