• 链栈——课上练


      1 #include <stdio.h>
      2 #include <stdlib.h>
      3 
      4 /*此处是链栈数据结构定义*/
      5 typedef int DataType;
      6 struct node 
      7 {
      8   DataType  info;
      9   struct node  *link;
     10 };
     11 
     12 typedef struct node *PNode;
     13 
     14 struct LinkStack {
     15    PNode  top;
     16 };
     17 typedef struct LinkStack *PLinkStack;
     18 
     19 
     20 //第一关
     21 PLinkStack createNullStack_link( )
     22 {//此处填写代码,创建一个空的链栈 
     23  PLinkStack head;
     24  /*PNode nul;
     25  nul=(PNode)malloc(sizeof(struct node));
     26  nul->link=NULL;*/
     27  head=(PLinkStack)malloc(sizeof(struct LinkStack));
     28  head->top=(PNode)malloc(sizeof(struct node));
     29  if(head!=NULL){
     30      head->top=NULL;
     31  }
     32  return head;
     33 }
     34 
     35 //第二关
     36 int isNullStack_link(PLinkStack L)
     37 {
     38   //判断栈是否为空,若为空,返回值为1,否则返回值为0,若栈不存在,则返回-1
     39 if(L==NULL){
     40     return -1;
     41 }
     42 if(L->top==NULL){
     43     return 1;
     44 
     45 }
     46 return 0;
     47 }
     48 
     49 
     50 
     51 
     52 //第三关
     53 int push_link(PLinkStack L ,DataType x)
     54 {//在栈中插入数据元素x,若插入不成功,返回0;插入成功返回值为1
     55     if(L==NULL){
     56         return 0;
     57     }
     58     PNode p = (PNode)malloc(sizeof(struct node));
     59     if(p==NULL){
     60         return;
     61     }
     62 
     63     p->info=x;
     64     p->link=L->top;
     65     L->top=p;
     66     return 1;
     67     
     68 }
     69  
     70 
     71 
     72 //第四关
     73 DataType pop_link(PLinkStack L)
     74 {//弹栈并返回删除元素,若栈为空,则返回-1
     75     if(L==NULL){
     76         return;
     77     }
     78     if(L->top==NULL){
     79         return -1;
     80     }
     81     PNode p=L->top;
     82     DataType ans=p->info;
     83    // ans=L->top->info;
     84     L->top=L->top->link;
     85     free(p);
     86     return ans;
     87 }
     88 
     89 //第五关
     90 DataType top_link(PLinkStack L)
     91 {// 取栈顶元素返回,若栈为空,则返回-1
     92      if(L==NULL){
     93         return;
     94     }
     95     if(L->top==NULL){
     96         return -1;
     97     }
     98     return L->top->info;
     99 }
    100 
    101 
    102 //销毁栈,释放栈所占存储空间
    103 int destroystack_link(PLinkStack L)
    104 {
    105     //返回值为销毁的栈中现有数据元素的个数,若待销毁的线性表不存在,则返回0
    106     int cnt =0 ;
    107     if(L==NULL) return 0;
    108     struct node *p = L->top ,*q ;
    109     free(L);
    110     while(p->link!=NULL)
    111     {
    112        q = p->link;
    113        cnt++;
    114        free(p);
    115        p = q ;
    116     }
    117     return cnt ;
    118 }
    119 
    120 //第六关
    121 
    122 int  balance_symbol(char *s)
    123 {//在此处填写代码完成符号配对判断,若配对,返回值为1,否则返回值为0
    124   PNode stack= createNullStack_link();
    125     if(stack==NULL){
    126         return 0;
    127     }
    128     int cnt=0;
    129     while(s[cnt]!=''){
    130         if(s[cnt]=='('||s[cnt]=='['||s[cnt]=='{'){
    131             push_link(stack,s[cnt]);
    132         }
    133         if(s[cnt]==')'||s[cnt]==']'||s[cnt]=='}'){
    134             if(abs(s[cnt]-top_link(stack))==1||abs(s[cnt]-top_link(stack))==2){
    135 
    136             if(!isNullStack_link(stack)){
    137 
    138                 pop_link(stack);
    139             }
    140             }
    141         }
    142         cnt++;
    143     }
    144     if(isNullStack_link(stack)){
    145         return 1;
    146     }
    147     return 0;
    148 }
    View Code
  • 相关阅读:
    (转)C# DllImport的用法
    (转)C#网络编程(异步传输字符串) Part.3
    (转)C#网络编程(订立协议和发送文件) Part.4
    C# tostring()汇总
    (转)C#网络编程(基本概念和操作) Part.1
    (转)关于数据库存储过程分页DatagridView BindingNavigator 控件的详细实现
    C# sql server 数据库备份和还原
    (转)C#网络编程(接收文件) Part.5
    2010年5月学习计划
    APUE学习笔记 Chapter 2 . Unix Standardization and Implementations
  • 原文地址:https://www.cnblogs.com/jeseesmith/p/13825886.html
Copyright © 2020-2023  润新知