各种基本算法总结——堆栈

 

转载地址:http://blog.youkuaiyun.com/sunboy_2050/article/details/5645812

 

 

各种基本算法实现小结(二)—— 堆 栈

(均已测试通过)

==============================================================

栈——数组实现

测试环境:Win - TC

[cpp:showcolumns] view plain copy print ?
·········10········20········30········40········50········60········70········80········90········100·······110·······120·······130·······140·······150
  1. #include <stdio.h> 
  2. char stack[512]; 
  3. int top=0; 
  4. void push(char c) 
  5.     stack[top]=c; 
  6.     top++; 
  7. char pop() 
  8.     top--; 
  9.     return stack[top]; 
  10. int is_empty() 
  11.     return 0==top; 
  12. void main() 
  13.     push('1'); 
  14.     push('2'); 
  15.     push('3'); 
  16.     push('4'); 
  17.     push('5'); 
  18.     while(!is_empty()) 
  19.         putchar(pop()); 
  20.     putchar('/n'); 
  21.     getch(); 

运行结果:

====================================================

栈——数组实现2

测试环境:Win - TC

[cpp:showcolumns] view plain copy print ?
·········10········20········30········40········50········60········70········80········90········100·······110·······120·······130·······140·······150
  1. #include <stdio.h> 
  2. #include <malloc.h> 
  3. /* typedef int DataType; */ 
  4. #define DataType int 
  5. #define MAX 1024 
  6. typedef struct 
  7.     DataType data[MAX]; 
  8.     int top; 
  9. }stack, *pstack; 
  10. pstack *init_stack() 
  11.     pstack ps; 
  12.     ps=(pstack)malloc(sizeof(stack)); 
  13.     if(!ps) 
  14.     { 
  15.         printf("Error. fail malloc.../n"); 
  16.         return NULL; 
  17.     } 
  18.     ps->top=-1; 
  19.     return ps; 
  20. int empty_stack(pstack ps) 
  21.     if(-1 == ps->top) 
  22.         return 1; 
  23.     else 
  24.         return 0; 
  25. int push(pstack ps, DataType data) 
  26.     if(ps->top == MAX-1) 
  27.     { 
  28.         printf("Stack is full.../n"); 
  29.         return 0; 
  30.     } 
  31.     ps->top++; 
  32.     ps->data[ps->top]=data; 
  33.     return 1; 
  34. int pop(pstack ps, DataType *data) 
  35.     if(empty_stack(ps)) 
  36.     { 
  37.         printf("Stack is empty.../n"); 
  38.         return 0; 
  39.     } 
  40.     *data=ps->data[ps->top]; 
  41.     ps->top--; 
  42.     return 1; 
  43. DataType top_stack(pstack ps) 
  44.     if(empty_stack(ps)) 
  45.     { 
  46.         printf("Stack is empty.../n"); 
  47.         return 0; 
  48.     } 
  49.     return ps->data[ps->top]; 
  50. void display(pstack ps) 
  51.     int i; 
  52.     if(empty_stack(ps)) 
  53.     { 
  54.         printf("Stack is empty.../n"); 
  55.         return
  56.     } 
  57.     printf("printf the items of stack.../n"); 
  58.     for(i=ps->top;i>-1;i--) 
  59.         printf("%4d", ps->data[i]); 
  60.     printf("/n/n"); 
  61. void main() 
  62.     int i, num, data, *pdata; 
  63.     pstack ps; 
  64.     ps=init_stack(); 
  65.     printf("Enter stack num:"); 
  66.     scanf("%d", &num); 
  67.     for(i=0;i<num;i++) 
  68.     { 
  69.         scanf("%d", &data); 
  70.         push(ps, data); 
  71.     } 
  72.     display(ps); 
  73.     printf("Top is %d/n/n", top_stack(ps)); 
  74.     for(i=0;i<num;i++) 
  75.     { 
  76.         pop(ps, pdata); 
  77.         printf("%3d", *pdata); 
  78.     } 
  79.     printf("/n/n"); 
  80.     display(ps); 
  81.     getch(); 

运行结果:

====================================================

栈——链表实现

测试环境:Win - TC

[cpp:showcolumns] view plain copy print ?
·········10········20········30········40········50········60········70········80········90········100·······110·······120·······130·······140·······150
  1. #include <stdio.h> 
  2. #include <malloc.h> 
  3. typedef char DataType; 
  4. struct _node 
  5.     DataType data; 
  6.     struct _node *next; 
  7. }; 
  8. typedef struct _node node, *pstack; 
  9. pstack init_stack() 
  10.     pstack ps; 
  11.     ps=(pstack)malloc(sizeof(node)); 
  12.     if(NULL == ps) 
  13.     { 
  14.         printf("Error. malloc is fail.../n"); 
  15.         return NULL; 
  16.     } 
  17.     ps->data=-1;  /* base of stack: data=-1 and next=NULL */ 
  18.     ps->next=NULL; 
  19.     return ps; 
  20. pstack push(pstack ps, DataType data) 
  21.     pstack ptop; 
  22.     ptop=(pstack)malloc(sizeof(node)); 
  23.     if(NULL == ptop) 
  24.     { 
  25.         printf("Error. malloc is fail.../n"); 
  26.         return NULL; 
  27.     } 
  28.     ptop->data=data; 
  29.     ptop->next=ps;   /* insert new item */ 
  30.     ps=ptop;         /* move top */ 
  31.     return ps; 
  32. pstack pop(pstack ps, DataType *data) 
  33.     if(ps->next == NULL) 
  34.     { 
  35.         printf("stack is empty.../n"); 
  36.         return NULL;             
  37.     } 
  38.     *data=ps->data; 
  39.     ps=ps->next; 
  40.     return ps; 
  41. DataType top_stack(pstack ps) 
  42.     if(ps->next == NULL)  /* if empty */ 
  43.     { 
  44.         printf("stack is empty.../n"); 
  45.         return -1; 
  46.     } 
  47.     return ps->data; 
  48. int len_stack(pstack ps) 
  49.     int len=0; 
  50.     pstack ptop=ps; 
  51.     while(ptop->next) 
  52.     { 
  53.         len++; 
  54.         ptop=ptop->next; 
  55.     } 
  56.     return len; 
  57. void display(pstack ps) 
  58.     pstack ptop; 
  59.     ptop=ps; 
  60.     while(ptop->next != NULL) 
  61.     { 
  62.         printf("%4c", ptop->data); 
  63.         ptop=ptop->next; 
  64.     } 
  65.     printf("/n/n"); 
  66. void main() 
  67.     pstack ps; 
  68.     DataType *data=(DataType *)malloc(sizeof(DataType)); 
  69.     ps=init_stack(); 
  70.     ps=push(ps, 'a'); 
  71.     ps=push(ps, 'b'); 
  72.     ps=push(ps, 'c'); 
  73.     ps=push(ps, 'd'); 
  74.     ps=push(ps, 'e'); 
  75.     display(ps); 
  76.     printf("len of stack is: %d/n/n", len_stack(ps)); 
  77.     printf("top of stack is: %c/n/n", top_stack(ps)); 
  78.     ps=pop(ps, data); 
  79.     printf("pop %c/n",*data); 
  80.     display(ps); 
  81.     ps=pop(ps, data); 
  82.     printf("pop %c/n",*data); 
  83.     display(ps); 
  84.     getch(); 

运行结果:

========================================================

堆 ——链表实现

测试环境:Win - TC

[cpp:showcolumns] view plain copy print ?
·········10········20········30········40········50········60········70········80········90········100·······110·······120·······130·······140·······150
  1. #include <stdio.h> 
  2. #include <malloc.h> 
  3. #include <stdlib.h> 
  4. struct _node 
  5.     int data; 
  6.     struct _node *next; 
  7. }; 
  8. typedef struct _node node, *pnode; 
  9. struct _linkqueue 
  10.     pnode front; 
  11.     pnode rear;     
  12. }; 
  13. typedef struct _linkqueue linkqueue, *plinkqueue; 
  14. linkqueue init_queue() 
  15.     linkqueue lq; 
  16.     lq.front=lq.rear=(pnode)malloc(sizeof(node)); 
  17.     if(NULL == lq.front) 
  18.     { 
  19.         printf("Error. malloc is fail.../n"); 
  20.         exit(1); 
  21.     } 
  22.     lq.rear->data=lq.front->data=-1; 
  23.     lq.rear->next=lq.front->next=NULL; 
  24.     return lq; 
  25. int empty_queue(linkqueue lq) 
  26.     if(lq.front == lq.rear) 
  27.         return 1; 
  28.      else 
  29.         return 0; 
  30. linkqueue insert_item(linkqueue lq, int data) 
  31.     pnode pq; 
  32.     pq=(pnode)malloc(sizeof(node)); 
  33.     if(pq == NULL) 
  34.     { 
  35.         printf("Error. malloc is fail.../n"); 
  36.         exit(1); 
  37.     } 
  38.     pq->data=data; 
  39.     pq->next=lq.rear->next; 
  40.     lq.rear->next=pq; 
  41.     lq.rear=lq.rear->next; 
  42.     return lq; 
  43. linkqueue delete_item(linkqueue lq, int *data) 
  44.     if(empty_queue(lq)) 
  45.     { 
  46.         printf("queue is empty.../n"); 
  47.         exit(1); 
  48.     } 
  49.     *data=lq.front->data; 
  50.     lq.front=lq.front->next; 
  51.     return lq; 
  52. int len_queue(linkqueue lq) 
  53.     int len=0; 
  54.     while(lq.front) 
  55.     { 
  56.         len++; 
  57.         lq.front=lq.front->next; 
  58.     } 
  59.     return len; 
  60. void display(linkqueue lq) 
  61.     linkqueue p; 
  62.     p=lq; 
  63.     if(empty_queue(lq)) 
  64.     { 
  65.         printf("queue is empty.../n"); 
  66.         return
  67.     } 
  68.     while(p.front->next) 
  69.     { 
  70.         printf("%4d", p.front->data); 
  71.         p.front=p.front->next; 
  72.     } 
  73.     printf("%4d/n/n", p.front->data); 
  74. void main() 
  75.     int *data = (int *)malloc(sizeof(int)); 
  76.     linkqueue lq; 
  77.     lq=init_queue(); 
  78.     lq=insert_item(lq, 1); 
  79.     lq=insert_item(lq, 2); 
  80.     lq=insert_item(lq, 3); 
  81.     lq=insert_item(lq, 4); 
  82.     lq=insert_item(lq, 5); 
  83.     display(lq); 
  84.     printf("len of queue is: %d/n/n", len_queue(lq)); 
  85.     lq=delete_item(lq, data); 
  86.     printf("delete %d/n", *data); 
  87.     display(lq); 
  88.     lq=delete_item(lq, data); 
  89.     printf("delete %d/n", *data); 
  90.     display(lq); 
  91.     getch(); 

运行结果:


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值