链栈的定义一个node的成员 linklist 指针的成员 代码实现 | data数据 next指针 top指针 count计数 typedef struct stacknode { int data; struct stacknode* next; }stacknode ,*linkstackptr; typedef struct linkstack { linkstackptr top; int count; }linkstack; |
链栈的插入声明 主要行为 代码 |
status stackinsert(linkstack* ps,int e) 创建节点,创建指向节点的指针 节点的data和next赋值 top重新赋值 count计数减少 linkstackptr p=(linkstackptr)malloc(sizeof(stacknode));
p->data=e;
p->next=ps->top; ps->top=p;
ps->count++; |
链栈的删除声明 主要行为 行为代码 | status stackdelete(linkstack*ps,int*pe) 原栈顶node的data传出 top换指向,原栈顶node释放 计数减一 *pe=ps->top->data;
linkstackptr p; p=ps->top; ps->top=ps->top->next; free (p);
ps->count--; |
代码汇总:
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#include <string.h>
//-----------------------------
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
typedef int status;
#define MAXSIZE 20
//-----------------------------
typedef struct stacknode
{
int data;
struct stacknode* next;
}stacknode ,*linkstackptr;
typedef struct linkstack
{
linkstackptr top;
int count;
}linkstack;
//linkstack arr[10];
status stackinsert(linkstack* ps,int e)
{
linkstackptr p=(linkstackptr)malloc(sizeof(stacknode));
p->data=e;
p->next=ps->top;
ps->top=p;
ps->count++;
}
status stackdelete(linkstack*ps,int*pe)
{
*pe=ps->top->data;
linkstackptr p;
p=ps->top;
ps->top=ps->top->next;
free (p);
ps->count--;
}
void test()
{
}
int main()
{
test();
system("pause");
return 0;
}