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


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

    (均已测试通过)

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

    栈——数组实现

    测试环境: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('
    ');   
    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 ...
    ");
    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...
    ");
    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...
    ");
    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...
    ");
    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...
    ");
    61         return ;
    62     }
    63     printf("printf the items of stack...
    ");
    64     for(i=ps->top;i>-1;i--){
    65         printf("%4d",ps->data[i]);
    66     }
    67     printf("
    
    ");
    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
    
    ",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(" "); 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...
    ");
    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...
    ");
    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...
    ");
    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...
    ");
    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("
    
    ");
    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
    
    ",len_stack(ps));
    85     printf("top of stack is :%c
    
    ",top_stack(ps));
    86     ps=pop(ps,data);
    87     printf("pop %c 
    ",*data);
    88     display(ps);
    89     ps=pop(ps,data);
    90     printf("pop %c 
    ",*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...
    ");
     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...
    ");
     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...
    ");
     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...
    ");
     75         return ;
     76     }
     77     while(p.front->next){
     78         printf("%4d",p.front->data);
     79         p.front=p.front->next;
     80     }
     81     printf("%4d
    
    ",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
    
    ",len_queue(lq));
     95     lq=delete_item(lq,data);
     96     printf("delete %d
    ",*data);
     97     display(lq);
     98     lq=delete_item(lq,data);
     99     printf("delete %d
    ",*data);
    100     display(lq);
    101 
    102 }

    运行结果:

     

     本文来自:~~~

  • 相关阅读:
    问题解决——MFC Ribbon 添加图标
    SQL Server 2008 R2——分组取前几名
    问题解决——VC 断点 无效 一个可能情况?
    C++/CLI——读书笔记《Visual C++/CLI从入门到精通》 第Ⅳ部分
    C++/CLI——读书笔记《Visual C++/CLI从入门到精通》 第Ⅱ部分
    随手小代码——最大子数组 联机算法
    随手小代码——最大子数组 分治法
    C++/CLI——读书笔记《Visual C++/CLI从入门到精通》 第Ⅰ部分
    问题解决——关闭子进程
    随手小代码——插入排序
  • 原文地址:https://www.cnblogs.com/labi/p/3586187.html
Copyright © 2020-2023  润新知