数据结构之链栈07

来源:互联网 发布:日本line 软件下载 编辑:程序博客网 时间:2024/06/05 20:43

/*
=========================================================
stack
限定仅在表尾进行插入或删除操作的线性表,栈顶和栈底
先进后出,FILO
=========================================================

规则:
数据域可以是各种类型,但必须属于同一个对象类型
线性关系,a1无前驱,an无后继

结构:
顺寻栈, 顺序存储,数组, 最大容量难估计
链栈,  链式存储


链栈的实现
以单向链表描述
---------------
*/
/*
* Copyright (C) 2013-2014 VantronTech, Inc. All Rights Reserved.
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program;
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct stack_link {
 char c;
 struct stack_link *next;
}STACK_LINK;

struct stack_listls {
 struct stack_link *top;//表示栈顶
 struct stack_link *botm; //表示栈底
};

/**
* @brief create_stack_link
*
* @return
*/
struct stack_link *create_stack_link(void)
{
 struct stack_link *head;
 
 head = (struct stack_link *)malloc(sizeof(struct stack_link));
 if (head == NULL)
  return NULL;

 head->c ='w';
 head->next = NULL;

 return head;
}

/**
* @brief pop_stack_list
*
* @param ls
* @param c
*
* @return
*/
int pop_stack_list(struct stack_listls *ls, char c)
{
 if (!ls)
  return -1;
 struct stack_link *pn;

 pn = (struct stack_link *)malloc(sizeof(struct stack_link));
 if (pn == NULL)
  return -1;
 
 pn->next = ls->top;
 pn->c = c;

 ls->top = pn;

 return 0;
}

/**
* @brief push_stack_list
*
* @param ls
* @param c
*
* @return
*/
int push_stack_list(struct stack_listls *ls, char *c)
{
 struct stack_link *pn;
 
 pn = ls->top;

 if (ls->top == ls->botm)
  return -1;
 
 else {
  *c = pn->c;
  ls->top = ls->top->next;
  free(pn);
  return 0;
 }
}

/**
* @brief print_stack_list
*
* @param ls
*/
void print_stack_list(struct stack_listls *ls)
{
 
 struct stack_link *pn = NULL;

 if (ls->top == ls->botm) {
  printf("This is empty\n");
 }
 else {
  for(pn = ls->top; pn != NULL && pn != ls->botm;) {
   printf("%2c", pn->c);
   pn = pn->next;
  }   
  printf("\n");
 }
}

/*==========================MAIN==================================*/
int main(int argc, char *argv[])
{
 struct stack_listls linkls;
 char c;

 struct stack_link *pn = NULL;

 pn = create_stack_link();
 if (pn == NULL)
  exit(0);

 linkls.top = pn;
 linkls.botm = pn; 
 
 printf("------Test Stack-------\n");
 
 printf("Input new char:");
 c = getchar();
 while(c != '\n') {
  pop_stack_list(&linkls, c);
  c = getchar();
 }
 printf("output stack:\n");
 print_stack_list(&linkls); 
 
 /* push stack */
 
 for (;linkls.top != linkls.botm;) {
  push_stack_list(&linkls, &c);
  printf("%c", c);
 }
 printf("\n");
 printf("Free memory...\n");
 free(linkls->botm);

 return 0;
}

原创粉丝点击