链栈的简单操作实现

入栈,先申请节点空间,然后将当前节点指向栈顶,然后栈顶指向本节点即可。如图



实现如下

void PushStack(Stack stack, int x)
{
  PNode node;
  node = (PNode)malloc(sizeof(struct Node));
  if (!node) {
    printf("无内存可分配\n");
  } else {
    node->next = stack->top;
    stack->top = node;
    node->data = x;
  }
}

出栈, 先判断栈是否为空,然后用一个新的节点存p放此节点地址,指向下一个节点,然后释放p。如果直接进行释放会导致节点断掉

void PopStack(Stack stack)
{
  PNode p;
  if (IsEmptyStack(stack)) {
    printf("空栈\n");
  } else {
    p = stack->top;
    stack->top = stack->top->next;
    free(p);
  }
}


这个输出栈我要说一下,不要用栈顶定位!!!,如果这样写每次就会移动栈顶指针,直至栈底。下次输出时就会认为空栈,导致无输出。

void print(Stack stack)
{
  Stack p = stack;
  while (p->top) {
    printf("%d ", p->top->data);
    p->top = p->top->next;
  }
  putchar('\n');
}


这个写法是知道栈顶指向的节点的地址,直接在从此节点知道下一个节点

void print(Stack stack)
{
  PNode p = stack->top;
  while (p) {
    printf("%d ", p->data);
    p = p->next;
  }
  putchar('\n');
}


链表的一大特性就是动态,不是固定一个空间。所以这里直接读取元素,当非整型停止读取即可

while (scanf("%d", &num)) {
    PushStack(stack, num);
  }


完整实现如下

#include<stdio.h>
#include<stdlib.h>

typedef struct Node * PNode;
struct Node { //节点
  int data;
  PNode next;
};

struct linkstack { //指向栈顶
  PNode top;
};
typedef struct linkstack * Stack;

Stack CreateStack(void)
{
  Stack stk;
  stk = (Stack)malloc(sizeof(struct linkstack));
  if (!stk) {
    printf("无内存\n");
  } else {
    stk->top = NULL;
  }
  printf("创建栈成功\n");
  return stk;
}

int IsEmptyStack(Stack stack)
{
  return (stack->top==NULL);
}

void PushStack(Stack stack, int x)
{
  PNode node;
  node = (PNode)malloc(sizeof(struct Node));
  if (!node) {
    printf("无内存可分配\n");
  } else {
    node->next = stack->top;
    stack->top = node;
    node->data = x;
  }
}

void PopStack(Stack stack)
{
  PNode p;
  if (IsEmptyStack(stack)) {
    printf("空栈\n");
  } else {
    p = stack->top;
    stack->top = stack->top->next;
    free(p);
  }
}

int GetTopStack(Stack stack)
{
  if (IsEmptyStack(stack)) {
    printf("空栈\n");
  } else {
    return stack->top->data;
  }
}

void print(Stack stack)
{
  PNode p = stack->top;
  while (p) {
    printf("%d ", p->data);
    p = p->next;
  }
  putchar('\n');
}

int main(void)
{
  Stack stack;
  int n, i, num;

  stack = CreateStack();
  printf("输入栈元素:");

  while (scanf("%d", &num)) {
    PushStack(stack, num);
  }

  getchar();
  printf("1.入栈     2.出栈\n");
  printf("3.判断栈是否为空     4.输出栈元素     5.栈首元素     6.退出\n");
  while (scanf("%d", &n) != EOF && n != 6) {
    switch (n) {
      case 1:printf("输入元素值:");scanf("%d", &n);PushStack(stack, n);break;
      case 2:PopStack(stack);break;
      case 3:printf("%s\n", IsEmptyStack(stack)==1?"空栈":"非空栈");break;
      case 4:print(stack);break;
      case 5:printf("栈首元素为:%d\n\n", GetTopStack(stack));break;
      default:printf("请输入合法操作!\n\n"); break;
    }
    printf("1.入栈     2.出栈\n");
    printf("3.判断栈是否为空     4.输出栈元素     5.退出\n");
  }

  return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值