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

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

(均已测试通过)

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

栈——数组实现

测试环境:VS 2010

 

 1     #include <stdio.h>  
 2     char stack[512];  
 3     int top=0;  
 4     void push(char c)  
 5     {  
 6         stack[top]=c;  
 7         top++;  
 8     }  
 9     char pop()  
10     {  
11         top--;  
12         return stack[top];  
13     }  
14     int is_empty()  
15     {  
16         return 0==top;  
17     }  
18     void main()  
19     {  
20         push('1');  
21         push('2');  
22         push('3');  
23         push('4');  
24         push('5');  
25         while(!is_empty())  
26             putchar(pop());  
27         putchar('\n');   
28     }  

 

运行结果:

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

 

栈——数组实现2

测试环境:VS 2010

 

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

 

运行结果:

 

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

 

栈——链表实现

测试环境:vs2010

 

 1 #include "stdafx.h"
 2 #include <malloc.h>
 3 typedef char DataType;
 4 struct _node{
 5     DataType data;
 6     struct _node *next;
 7 };
 8 
 9 typedef struct _node node,*pstack;
10 pstack init_stack()
11 {
12     pstack ps;
13     ps=(pstack)malloc(sizeof(node));
14     if(NULL==ps){
15         printf("Error,malloc is fail...\n");
16         return NULL;
17     }
18     ps->data=-1;
19     ps->next=NULL;
20     return ps;
21 }
22 pstack push(pstack ps,DataType data){
23     pstack ptop;
24     ptop=(pstack)malloc(sizeof(node));
25     if(NULL==ptop){
26         printf("Error,malloc is fail...\n");
27         return NULL;
28     }
29     ptop->data=data;
30     ptop->next=ps;
31     ps=ptop;
32     return ps;
33 }
34 
35 pstack pop(pstack ps,DataType *data){
36     if(ps->next==NULL){
37         printf("stack is empty...\n");
38         return NULL;
39     }
40     *data=ps->data;
41     ps=ps->next;
42     return ps;
43 }
44 DataType top_stack(pstack ps){
45     if(NULL==ps->next){
46         printf("stack is empty...\n");
47         return -1;
48     }
49     return ps->data;
50 }
51 
52 int len_stack(pstack ps){
53     int len=0;
54     pstack ptop=ps;
55     while(ptop->next){
56         len++;
57         ptop=ptop->next;
58     }
59     return len;
60 }
61 
62 void display(pstack ps){
63     pstack ptop;
64     ptop=ps;
65     while(ptop->next != NULL){
66         printf("%4c",ptop->data);
67         ptop=ptop->next;
68     }
69     printf("\n\n");
70 }
71 
72 
73 int _tmain(int argc, _TCHAR* argv[])
74 {
75     pstack ps;
76     DataType *data=(DataType *) malloc(sizeof(DataType));
77     ps=init_stack();
78     ps=push(ps,'a');
79     ps=push(ps,'b');
80     ps=push(ps,'c');
81     ps=push(ps,'d');
82     ps=push(ps,'e');
83     display(ps);
84     printf("len of stack is :%d\n\n",len_stack(ps));
85     printf("top of stack is :%c\n\n",top_stack(ps));
86     ps=pop(ps,data);
87     printf("pop %c \n",*data);
88     display(ps);
89     ps=pop(ps,data);
90     printf("pop %c \n",*data);
91     display(ps);
92 
93 }

运行结果:

 

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

堆 ——链表实现

测试环境:VS 2010

 

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

 

 

 

运行结果:

 

 本文来自:~~~

 

posted @ 2014-03-07 19:16  风华一指流砂,苍老一段年华  Views(256)  Comments(0Edit  收藏  举报