栈
一、链栈
- 利用带头结点的单链表实现。
- 栈顶:入栈和出栈的操作都在栈顶,链栈栈顶在表头(根据时间复杂度确定的,都是O(1))。
1.链栈的设计
typedef struct LSNode
{
int data;
struct LSNode* next;//类似单链表的设计界结构;
}LSNode,*PLStack;//因为栈顶在表头,所以不需要top指针,表头的后面就是栈顶;
2.链栈的基本操作
(1)初始化
void InitStack(PLStack ps)
{
assert(ps != NULL);
if (ps == NULL)return;
//ps->data = 0;//带头结点的链栈数据域可以不用;
ps->next = NULL;
}
(2)入栈操作(头插)
- 如果从尾结点开始插入的话,需要遍历一般链表,时间复杂度为O(n),所以我们从头结点开始插入,时间复杂度为O(1)。此操作类似于单链表的头插法。
bool Push(PLStack ps, int val)
{
assert(ps != NULL);
if (ps == NULL)return false;
PLStack pnewnode = (PLStack)malloc(sizeof(LSNode));
if (pnewnode == NULL)return false;
pnewnode->data = val;
pnewnode->next =ps->next;
ps->next = pnewnode;
return true;
}
(3)获取栈中有效元素个数
- 顺序栈的有效元素个数就是尾指针下标的值,但链栈中由于所有操作都是从栈顶操作的,所以不存在尾指针,计数时需要单独申请指针遍历链栈。
int GetLength(PLStack ps)
{
assert(ps != NULL);
if (ps == NULL)return false;
int count = 0;
for (PLStack p = ps->next; p != NULL; p = p->next)
{
count++;
}
return count;
}
(4)判空
bool IsEmpty(PLStack ps)
{
return ps->next == NULL;
}
(5)获取栈顶元素的值,但不删除
- 如果获取函数返回值是int型,则有可能与栈中存的元素重合,所以用bool类型,获取的值就需要用另一个数据来接收。
bool Get_top(PLStack ps, int* rtval)
{
assert(ps != NULL&&rtval!=NULL);
if (ps == NULL||rtval==NULL)return false;
if (IsEmpty(ps))return false;//需要获取值,就需要链表不为空;
*rtval = ps->next->data;
return true;
}
(6)获取栈顶元素的值并删除
bool Del_top(PLStack ps, int* rtval)
{
assert(ps != NULL && rtval != NULL);
if (ps == NULL || rtval == NULL)return false;
if (IsEmpty(ps))return false;
*rtval = ps->next->data;
PLStack p = ps->next;
ps->next = p->next;
free(p);
p = NULL;
return true;
}
(7)销毁
bool Destroy(PLStack ps)
{
assert(ps != NULL);
if (ps == NULL)return false;
PLStack p = NULL;
while (!IsEmpty(ps))
{
p = ps->next;
ps->next = p->next;
free(p);
p = NULL;
}
return true;
}
(8)清空
bool Clear(PLStack ps)
{
Destroy(ps);
return true;
}
3.完整代码
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
typedef struct LSNode
{
int data;
struct LSNode* next;//类似单链表的设计界结构;
}LSNode,*PLStack;//因为栈顶在表头,所以不需要top指针;
//初始化
void InitStack(PLStack ps)
{
assert(ps != NULL);
if (ps == NULL)return;
//ps->data = 0;//带头结点的链栈数据域可以不用;
ps->next = NULL;
}
//入栈操作(头插)
bool Push(PLStack ps, int val)
{
assert(ps != NULL);
if (ps == NULL)return false;
PLStack pnewnode = (PLStack)malloc(sizeof(LSNode));
if (pnewnode == NULL)return false;
pnewnode->data = val;
pnewnode->next =ps->next;
ps->next = pnewnode;
return true;
}
//获取栈中有效元素个数
int GetLength(PLStack ps)
{
assert(ps != NULL);
if (ps == NULL)return false;
int count = 0;
for (PLStack p = ps->next; p != NULL; p = p->next)
{
count++;
}
return count;
}
//判空
bool IsEmpty(PLStack ps)
{
return ps->next == NULL;
}
//获取栈顶元素的值,但不删除
bool Get_top(PLStack ps, int* rtval)
{
assert(ps != NULL&&rtval!=NULL);
if (ps == NULL||rtval==NULL)return false;
if (IsEmpty(ps))return false;
*rtval = ps->next->data;
return true;
}
//获取栈顶元素的值并删除
bool Del_top(PLStack ps, int* rtval)
{
assert(ps != NULL && rtval != NULL);
if (ps == NULL || rtval == NULL)return false;
if (IsEmpty(ps))return false;
*rtval = ps->next->data;
PLStack p = ps->next;
ps->next = p->next;
free(p);
p = NULL;
return true;
}
//销毁
bool Destroy(PLStack ps)
{
assert(ps != NULL);
if (ps == NULL)return false;
PLStack p = NULL;
while (!IsEmpty(ps))
{
p = ps->next;
ps->next = p->next;
free(p);
p = NULL;
}
return true;
}
//清空
bool Clear(PLStack ps)
{
Destroy(ps);
return true;
}
int main()
{
LSNode head;
InitStack(&head);
for (int i = 0; i < 10; i++)
{
Push(&head, i * 2);
}
int k = GetLength(&head);
printf("count=%d\n ", k);
int rtval = 0;
while (!IsEmpty(&head))
{
Get_top(&head,&rtval);
printf("%d ", rtval);
Del_top(&head, &rtval);
}
printf("\n");
k = GetLength(&head);
printf("count=%d ", k);
}
运行结果: