Linux C 数据结构——栈

来源:互联网 发布:手机还原软件 编辑:程序博客网 时间:2024/05/19 16:19

还是先把这张图贴出来,以便对比和理解

  

  栈是限制在一段进行插入操作和删除操作的线性表(俗称堆栈),允许进行操作的一端称为“栈顶”,另一固定端称为“栈底”,当栈中没有元素称为“空栈”。特点:先进后出(FILO)。

栈顶即top,这里top有两种定义方式:

1、满栈(Full Stack),top指向最后一个使用的空间;

2、空栈(Empty Stack),top指向下一个可用的空间;

     栈也是线性表,所以也分顺序存储和链式存储:

一、顺序存储

       栈是顺序表的一种,具有顺序表同样的存储结构,由数组定义,配合用数组下表表示的栈顶指针top(相对指针)完成各种操作。

[cpp] view plain copy
  1. typedef int data_t;  //定义栈中数据元素的数据类型  
  2. typedef struct  
  3. {  
  4.     date_t *data;  //用指针指向栈的存储空间  
  5.     int maxlen;    //当前栈的最大元素个数  
  6.     int top;  //指向栈顶位置(数组下标)的变量  
  7. }seqstack_t;  //顺序栈类型定义  


如图,我们可以看到,栈的顺序存储与顺序表的区别,顺序表数组大小是固定的,这样限制了我们的后期修改,而栈的顺序存储将数据域单独开辟了一片空间才存放,大小为maxlen*sizeof(data_t), 下面是栈的基本运算:

1、创建栈

[cpp] view plain copy
  1. seqstack_t *CreateEmptyStack(int max_len)  
  2. {  
  3.     seqstack_t *stack;  
  4.     stack = (seqstack_t *)malloc(sizeof(seqstack_t));  
  5.     stack->data = (data_t *)malloc(sizeof(data_t)*max_len);  
  6.     stack->top = -1;  
  7.     stack->max_len = max_len;  
  8.   
  9.     return stack;  
  10. }  

2、摧毁一个栈

[cpp] view plain copy
  1. void DestroyStack(seqstack_t *stack)  
  2. {  
  3.     if(stack != NULL)  
  4.     {  
  5.         if(stack->data != NULL)  
  6.             free(stack->data);  
  7.   
  8.         free(stack);  
  9.     }  
  10. }  

3、清空一个栈

[cpp] view plain copy
  1. void ClearStack(seqstack_t *stack)  
  2. {  
  3.     if(stack != NULL)  
  4.         stack->top = -1;  
  5. }  

4、判断栈是否为空

[cpp] view plain copy
  1. int EmptyStack(seqstack_t *stack)  
  2. {  
  3.     if(stack == NULL)  
  4.         return -1;  
  5.   
  6.     return(stack->top == -1 ? 1 : 0);  
  7. }  

5、判断栈是否为满

[cpp] view plain copy
  1. int FullStack(seqstack_t *stack)  
  2. {  
  3.     if(stack == NULL)  
  4.         return -1;  
  5.   
  6.     return(stack->top == (stack->max_len - 1) ? 1 : 0);  
  7. }  

6、进栈

[cpp] view plain copy
  1. int PushStack(seqstack_t *stack ,data_t x)  
  2. {  
  3.     if(FullStack(stack))  
  4.         return -1;  
  5.     else  
  6.     {  
  7.         stack->top++;  
  8.         stack->data[stack->top] = x;  
  9.     }  
  10.   
  11.     return 0;  
  12. }  

7、出栈

[cpp] view plain copy
  1. int PopStack(seqstack_t *stack,data_t *x)  
  2. {  
  3.     if(EmptySqstack(stack))  
  4.         return -1;  
  5.     else  
  6.     {  
  7.         *x = stack->data[stack->top];  
  8.         stack->top--;  
  9.     }  
  10.   
  11.     return 0;  
  12. }  

8、取栈顶元素

[cpp] view plain copy
  1. int GetTop(seqstack_t *stack,data_t *x)  
  2. {  
  3.     if(EmptyStack(stack))  
  4.         return -1;  
  5.     else  
  6.         *x = stack->data[stack->top];  
  7.   
  8.     return 0;  
  9. }  

 

二、链式存储

        若是栈中元素的数目变化范围较大或不清楚栈元素的数目,就应该考虑使用链式存储结构。人们将用链式存储结构表示的栈称作"链栈"。链栈通常用一个无头结点的单链表表示。如图所示:

插入操作和删除操作均在链表头部进行,链表尾部就是栈底,栈顶指针就是头指针;

[cpp] view plain copy
  1. typedef int data_t;  
  2. typedef struct node_t  
  3. {  
  4.     data_t data;  //数据域  
  5.     struct node_t *next;  //链接指针域  
  6. }linkstack_t; //链栈类型定义  

栈(链式存储)基本运算如下:

1、创建空栈:

[cpp] view plain copy
  1. linkstack_t *CreateLinkstack()  
  2. {  
  3.     linkstack_t *top;  
  4.     top = (linkstack_t *)malloc(sizeof(linkstack_t));  
  5.     top->next = NULL;  
  6.       
  7.     return top;  
  8. }  

2、判断是否为空栈:

[cpp] view plain copy
  1. int EmptyStack(linkstack_t *top)  
  2. {  
  3.     return (top->next == NULL ? 1 : 0);  
  4. }  

3、入栈

[cpp] view plain copy
  1. void PushStack(linkstack_t *top,data_t x)  
  2. {  
  3.     linkstack_t *p;  
  4.     p = (linkstack_t *)malloc(sizeof(linkstack_t));  
  5.     p->data = x;  
  6.     p->next = top->next;  
  7.     top->next = p;  
  8.   
  9.     return;  
  10. }  

4、出栈

[cpp] view plain copy
  1. int PopStack(linkstack_t stack,data_t *x)  
  2. {  
  3.     if(stack->next == NULL || stack == NULL)  
  4.         return -1;  
  5.   
  6.     linkstack_t p;  
  7.     p = stack->next;  
  8.     stack->next = p->next;  
  9.     if(x != NULL)  
  10.         *x = p->data;  
  11.     free(p);  
  12.   
  13.     return 0;  
  14. }  
0 0
原创粉丝点击