基於數組或鏈表的堆棧實現

本文使用C語言,給出了堆棧的兩種實現:基於數組和基於鏈表的實現方式。

堆棧是一種常用的數據結構,具有“後進先出(Last In First Out)”的特性,常用來進行函數調用時候的參數傳遞,解決遞歸函數書的非遞歸實現,表達式中的括號匹配等問題。堆棧的常用操作如下:

  • createStack(st):建立一個空棧
  • push(st, x):將元素x壓入棧st當中,使之成爲棧頂元素
  • pop(st,x):當棧非空時,將棧頂元素彈出,並賦值給x
  • top(st):當棧非空時,返回棧頂元素的值
  • isEmpty(st):判斷棧st是否爲空

 

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

#define EleType int
#define DEFAULT_SIZE 100

/*---------array-based stack--------------*/
#if defined(ARRAY_BASED_STACK)
typedef 
struct Stack_t {
    
int top;
    
int capacity;

    EleType 
*container;
}
Stack;

int createStack(Stack *st, int c)
{
    
if(c<0)
        c 
= DEFAULT_SIZE;
    st
->container = (EleType*)malloc(c*sizeof(EleType));
    
if(NULL == st->container) return 0;
    st
->capacity = c;
    st
->top = -1;
    
return 1;
}

int destoryStack(Stack *st)
{
    
if(st){
        st
->top = -1;
        free(st
->container);
        st
->container = NULL;
    }

    
return 1;
}

int isEmpty(Stack *st)
{
    
if(-1 == st->top)
        
return 1;
    
else
        
return 0;
}

EleType top(Stack 
*st)
{
    
return st->container[st->top];
}

int push(Stack *st, EleType e)
{
    
if((st->top + 1== st->capacity) {
        EleType 
*tmp = st->container;
        st
->container = (EleType*)malloc(st->capacity * 2 *sizeof(EleType));
        
if(!(st->container)) return 0;
        st
->capacity <<= 1;
        
int i;
        
for(i=0; i<st->top; i++)
            st
->container[i] = tmp[i];
        free(tmp);
    }

    st
->top ++;
    st
->container[st->top] = e;

    
return 1;
}

int pop(Stack *st, EleType *eptr)
{
    
if(-1 == st->top){
        
return 0;
    }

    
else {
        st
->top --;
        
*eptr = st->container[st->top+1];
        
if(st->top < (st->capacity>>1)) {
            EleType 
*tmp = (EleType*)malloc((st->capacity/2*sizeof(EleType));
            
if(tmp) {
                
int i;
                
for(i=0; i<st->top; i++)
                    tmp[i] 
= st->container[i];
                free(st
->container);
                st
->container = tmp;
            }

        }

        
return 1;
    }

}

#endif
/*------------link-based stack-------------*/
typedef 
struct Element_t{
    
void *data;
    
struct Element_t *next;
}
Element;

int createStack(Element **st);
int destoryStack(Element **st);
int push(Element **st, void *data);
int pop(Element **st, void **data);
int isEmpty(Element **st);
void * top(Element **st);

int createStack(Element **st)
{
    
*st = NULL;
    
return 1;
}

int destoryStack(Element **st)
{
    Element 
*next = NULL;
    
while(*st){
        next 
= (*st)->next;
        free(
*st);
        
*st = next;
    }

    
return 1;
}

int push(Element **st, void *data)
{
    Element 
*tmp = (Element*)malloc(sizeof(*tmp));
    
if(!tmp)
        
return 0;
    
else {
        tmp
->data = data;
        tmp
->next = *st;
        (
*st)->next = tmp;
        
return 1;
    }

}

int pop(Element **st, void **data)
{
    
if(!(*st))
        
return 0;
    
else {
        Element 
*= *st;
        
*data = t->data;
        
*st = t->next;
        free(t);
        
return 1;
    }

}

int isEmpty(Element **st)
{
    
if(!(*st))
        
return 0;
    
else
        
return 1;
}

void* top(Element **st)
{
    
if(!(*st))
        
return (*st)->data;
    
else
        
return NULL;
}
 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章