Chinaunix首页 | 论坛 | 博客
  • 博客访问: 255364
  • 博文数量: 52
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1538
  • 用 户 组: 普通用户
  • 注册时间: 2013-04-24 07:45
个人简介

生活就像海洋,只有意志坚强的人,才能到达彼岸。

文章存档

2013年(52)

分类: C/C++

2013-08-16 15:52:02

1>定义:

栈是一种特殊的线性表;栈仅能在线性表的一段进行操作;

栈顶(Top):允许操作的一端

栈底(Bottom):不允许操作的一端

2>性质:后进先出

栈的常用操作有:创建栈,销毁栈,清空栈,进栈,出栈,获取栈顶元素,获取栈的大小

3>栈的顺序存储实现

点击(此处)折叠或打开

  1. /*SeqList.h*/
  2. /************************************************************/
  3. #ifndef _SEQLIST_H_
  4. #define _SEQLIST_H_

  5. typedef void SeqList;
  6. typedef void SeqListNode;

  7. SeqList* SeqList_Create(int capacity);

  8. void SeqList_Destroy(SeqList* list);

  9. void SeqList_Clear(SeqList* list);

  10. int SeqList_Length(SeqList* list);

  11. int SeqList_Capacity(SeqList* list);

  12. int SeqList_Insert(SeqList* list, SeqListNode* node, int pos);

  13. SeqListNode* SeqList_Get(SeqList* list, int pos);

  14. SeqListNode* SeqList_Delete(SeqList* list, int pos);

  15. #endif

  16. /*SeqList.c*/
  17. /**************************************************************/

  18. #include <stdio.h>
  19. #include <malloc.h>
  20. #include "SeqList.h"

  21. typedef unsigned int TSeqListNode;

  22. typedef struct _tag_SeqList
  23. {
  24.     int capacity;
  25.     int length;
  26.     TSeqListNode* node;
  27. } TSeqList;

  28. SeqList* SeqList_Create(int capacity) // O(1)
  29. {
  30.     TSeqList* ret = NULL;
  31.     
  32.     if( capacity >= 0 )
  33.     {
  34.         ret = (TSeqList*)malloc(sizeof(TSeqList) + sizeof(TSeqListNode) * capacity);
  35.     }
  36.     
  37.     if( ret != NULL )
  38.     {
  39.         ret->capacity = capacity;
  40.         ret->length = 0;
  41.         ret->node = (TSeqListNode*)(ret + 1);
  42.     }
  43.     
  44.     return ret;
  45. }

  46. void SeqList_Destroy(SeqList* list) // O(1)
  47. {
  48.     free(list);
  49. }

  50. void SeqList_Clear(SeqList* list) // O(1)
  51. {
  52.     TSeqList* sList = (TSeqList*)list;
  53.     
  54.     if( sList != NULL )
  55.     {
  56.         sList->length = 0;
  57.     }
  58. }

  59. int SeqList_Length(SeqList* list) // O(1)
  60. {
  61.     TSeqList* sList = (TSeqList*)list;
  62.     int ret = -1;
  63.     
  64.     if( sList != NULL )
  65.     {
  66.         ret = sList->length;
  67.     }
  68.     
  69.     return ret;
  70. }

  71. int SeqList_Capacity(SeqList* list) // O(1)
  72. {
  73.     TSeqList* sList = (TSeqList*)list;
  74.     int ret = -1;
  75.     
  76.     if( sList != NULL )
  77.     {
  78.         ret = sList->capacity;
  79.     }
  80.     
  81.     return ret;
  82. }

  83. int SeqList_Insert(SeqList* list, SeqListNode* node, int pos) // O(n)
  84. {
  85.     TSeqList* sList = (TSeqList*)list;
  86.     int ret = (sList != NULL);
  87.     int i = 0;
  88.     
  89.     ret = ret && (sList->length + 1 <= sList->capacity);
  90.     ret = ret && (0 <= pos);
  91.     
  92.     if( ret )
  93.     {
  94.         if( pos >= sList->length )
  95.         {
  96.             pos = sList->length;
  97.         }
  98.         
  99.         for(i=sList->length; i>pos; i--)
  100.         {
  101.             sList->node[i] = sList->node[i-1];
  102.         }
  103.         
  104.         sList->node[i] = (TSeqListNode)node;
  105.         
  106.         sList->length++;
  107.     }
  108.     
  109.     return ret;
  110. }

  111. SeqListNode* SeqList_Get(SeqList* list, int pos) // O(1)
  112. {
  113.     TSeqList* sList = (TSeqList*)list;
  114.     SeqListNode* ret = NULL;
  115.     
  116.     if( (sList != NULL) && (0 <= pos) && (pos < sList->length) )
  117.     {
  118.         ret = (SeqListNode*)(sList->node[pos]);
  119.     }
  120.     
  121.     return ret;
  122. }

  123. SeqListNode* SeqList_Delete(SeqList* list, int pos) // O(n)
  124. {
  125.     TSeqList* sList = (TSeqList*)list;
  126.     SeqListNode* ret = SeqList_Get(list, pos);
  127.     int i = 0;
  128.     
  129.     if( ret != NULL )
  130.     {
  131.         for(i=pos+1; i<sList->length; i++)
  132.         {
  133.             sList->node[i-1] = sList->node[i];
  134.         }
  135.         
  136.         sList->length--;
  137.     }
  138.     
  139.     return ret;
  140. }

  141. /*SeqStack.h*/
  142. /**********************************************************/

  143. #ifndef _SEQSTACK_H_
  144. #define    _SEQSTACK_H_

  145. typedef void SeqStack;

  146. SeqStack* SeqStack_Create(int capacity);

  147. void SeqStack_Destroy(SeqStack* stack);

  148. void SeqStack_Clear(SeqStack* stack);

  149. int SeqStack_Push(SeqStack* stack, void* item);

  150. void *SeqStack_Pop(SeqStack* stack);

  151. void *SeqStack_Top(SeqStack* stack);

  152. int SeqStack_Length(SeqStack* stack);

  153. int SeqStack_Capacity(SeqStack* stack);

  154. #endif

  155. /*SeqStack.c*/
  156. **************************************************************************

  157. #include <stdio.h>
  158. #include "SeqStack.h"
  159. #include "SeqList.h"

  160. SeqStack* SeqStack_Create(int capacity)
  161. {
  162.     return SeqList_Create(capacity);
  163. }

  164. void SeqStack_Destroy(SeqStack* stack)
  165. {
  166.     SeqList_Destroy(stack);
  167. }

  168. void SeqStack_Clear(SeqStack* stack)
  169. {
  170.     SeqList_Clear(stack);
  171. }

  172. int SeqStack_Push(SeqStack* stack, void* item)
  173. {
  174.     return SeqList_Insert(stack,item,SeqList_Length(stack));    
  175. }

  176. void *SeqStack_Pop(SeqStack* stack)
  177. {
  178.     return SeqList_Delete(stack,SeqList_Length(stack)-1);
  179. }

  180. void *SeqStack_Top(SeqStack* stack)
  181. {
  182.     return SeqList_Get(stack,SeqList_Length(stack)-1);
  183. }

  184. int SeqStack_Length(SeqStack* stack)
  185. {
  186.     return SeqList_Length(stack);
  187. }

  188. int SeqStack_Capacity(SeqStack* stack)
  189. {
  190.     return SeqList_Capacity(stack);
  191. }
4>栈的链表实现方式(代码复用的思想

点击(此处)折叠或打开

  1. /**********************LinkList.h******************************/

  2. #ifndef _LINKLIST_H_
  3. #define _LINKLIST_H_

  4. typedef void LinkList;
  5. typedef struct _tag_LinkListNode LinkListNode;
  6. struct _tag_LinkListNode
  7. {
  8.     LinkListNode* next;
  9. };

  10. LinkList* LinkList_Create();

  11. void LinkList_Destroy(LinkList* list);

  12. void LinkList_Clear(LinkList* list);

  13. int LinkList_Length(LinkList* list);

  14. int LinkList_Insert(LinkList* list, LinkListNode* node, int pos);

  15. LinkListNode* LinkList_Get(LinkList* list, int pos);

  16. LinkListNode* LinkList_Delete(LinkList* list, int pos);

  17. #endif

  18. /*********************LinkList.c***************************/
  19. #include <stdio.h>
  20. #include <malloc.h>
  21. #include "LinkList.h"

  22. typedef struct _tag_LinkList
  23. {
  24.     LinkListNode header;
  25.     int length;
  26. } TLinkList;

  27. LinkList* LinkList_Create() // O(1)
  28. {
  29.     TLinkList* ret = (TLinkList*)malloc(sizeof(TLinkList));
  30.     
  31.     if( ret != NULL )
  32.     {
  33.         ret->length = 0;
  34.         ret->header.next = NULL;
  35.     }
  36.     
  37.     return ret;
  38. }

  39. void LinkList_Destroy(LinkList* list) // O(1)
  40. {
  41.     free(list);
  42. }

  43. void LinkList_Clear(LinkList* list) // O(1)
  44. {
  45.     TLinkList* sList = (TLinkList*)list;
  46.     
  47.     if( sList != NULL )
  48.     {
  49.         sList->length = 0;
  50.         sList->header.next = NULL;
  51.     }
  52. }

  53. int LinkList_Length(LinkList* list) // O(1)
  54. {
  55.     TLinkList* sList = (TLinkList*)list;
  56.     int ret = -1;
  57.     
  58.     if( sList != NULL )
  59.     {
  60.         ret = sList->length;
  61.     }
  62.     
  63.     return ret;
  64. }

  65. int LinkList_Insert(LinkList* list, LinkListNode* node, int pos) // O(n)
  66. {
  67.     TLinkList* sList = (TLinkList*)list;
  68.     int ret = (sList != NULL) && (pos >= 0) && (node != NULL);
  69.     int i = 0;
  70.     
  71.     if( ret )
  72.     {
  73.         LinkListNode* current = (LinkListNode*)sList;
  74.         
  75.         for(i=0; (i<pos) && (current->next != NULL); i++)
  76.         {
  77.             current = current->next;
  78.         }
  79.         
  80.         node->next = current->next;
  81.         current->next = node;
  82.         
  83.         sList->length++;
  84.     }
  85.     
  86.     return ret;
  87. }

  88. LinkListNode* LinkList_Get(LinkList* list, int pos) // O(n)
  89. {
  90.     TLinkList* sList = (TLinkList*)list;
  91.     LinkListNode* ret = NULL;
  92.     int i = 0;
  93.     
  94.     if( (sList != NULL) && (0 <= pos) && (pos < sList->length) )
  95.     {
  96.         LinkListNode* current = (LinkListNode*)sList;
  97.         
  98.         for(i=0; i<pos; i++)
  99.         {
  100.             current = current->next;
  101.         }
  102.         
  103.         ret = current->next;
  104.     }
  105.     
  106.     return ret;
  107. }

  108. LinkListNode* LinkList_Delete(LinkList* list, int pos) // O(n)
  109. {
  110.     TLinkList* sList = (TLinkList*)list;
  111.     LinkListNode* ret = NULL;
  112.     int i = 0;
  113.     
  114.     if( (sList != NULL) && (0 <= pos) && (pos < sList->length) )
  115.     {
  116.         LinkListNode* current = (LinkListNode*)sList;
  117.         
  118.         for(i=0; i<pos; i++)
  119.         {
  120.             current = current->next;
  121.         }
  122.         
  123.         ret = current->next;
  124.         current->next = ret->next;
  125.         
  126.         sList->length--;
  127.     }
  128.     
  129.     return ret;
  130. }


  131. /*LinkStack.h*/
  132. /**************************************************************/
  133. #ifndef _LINKSTACK_H_
  134. #define    _LINKSTACK_H_

  135. typedef void LinkStack;

  136. LinkStack* LinkStack_Create();

  137. void LinkStack_Destroy(LinkStack* stack);

  138. void LinkStack_Clear(LinkStack* stack);

  139. int LinkStack_Push(LinkStack* stack, void* item);

  140. void *LinkStack_Pop(LinkStack* stack);

  141. void *LinkStack_Top(LinkStack* stack);

  142. int LinkStack_Size(LinkStack* stack);

  143. #endif

  144. /*LinkStack.c*/
  145. /*************************************************************/
  146. #include <malloc.h>
  147. #include "LinkList.h"
  148. #include "LinkStack.h"

  149. typedef struct _tag_LinkStackNode
  150. {
  151.     LinkListNode header;
  152.     void* item;
  153. }TLinkStackNode;

  154. LinkStack* LinkStack_Create()
  155. {
  156.     return LinkList_Create();    
  157. }

  158. void LinkStack_Destroy(LinkStack* stack)
  159. {
  160.     
  161. }

  162. void LinkStack_Clear(LinkStack* stack)
  163. {
  164.     
  165. }

  166. int LinkStack_Push(LinkStack* stack, void* item)
  167. {
  168.     TLinkStackNode* node = (TLinkStackNode*)malloc(sizeof(TLinkStackNode));
  169.     int ret = (node != NULL) && (item != NULL);
  170.     
  171.     if( ret )
  172.     {
  173.         node->item = item;
  174.         
  175.         ret = LinkList_Insert(stack,(LinkListNode*)node,0);    
  176.     }
  177.     
  178.     if( !ret )
  179.     {
  180.         free(node);
  181.         
  182.     }
  183.     
  184.     return ret;
  185. }

  186. void *LinkStack_Pop(LinkStack* stack)
  187. {
  188.     TLinkStackNode* node = (TLinkStackNode*)LinkList_Delete(stack, 0);
  189.     void* ret = NULL;
  190.     
  191.     if( node != NULL)
  192.     {
  193.         ret = node->item;
  194.         
  195.         free(node);    
  196.     }
  197.     
  198.     return ret;
  199. }

  200. void *LinkStack_Top(LinkStack* stack)
  201. {
  202.     TLinkStackNode* node = (TLinkStackNode*)LinkList_Get(stack, 0);
  203.     void* ret = NULL;
  204.     
  205.     if( node != NULL)
  206.     {
  207.         ret = node->item;
  208.         
  209.     }
  210.     
  211.     return ret;    
  212.     
  213. }

  214. int LinkStack_Size(LinkStack* stack)
  215. {
  216.     return LinkList_Length(stack);
  217. }

阅读(1382) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~