数据结构笔记2

1.栈

stack.h

  1. //stack.h   
  2.     
  3. #ifndef _STACK_H   
  4. #define _STACK_H   
  5.     
  6. #include "stack.h"   
  7. #include "data.h"   
  8.     
  9. #define ElemType TREE*   
  10.     
  11. #define STACK_INIT_SIZE 10   
  12. #define STACK_INCREME 10   
  13.     
  14. typedef struct  
  15. {   
  16.     ElemType *base;   
  17.     ElemType *top;   
  18.     int size;   
  19. }STACK;   
  20.     
  21. STACK *InitStack();   
  22. void DestroyStack(STACK *s);   
  23. int Push(STACK *s,ElemType *e);   
  24. int Pop(STACK *s,ElemType *e);   
  25. int IsEmpty(STACK *s);   
  26.     
  27. #endif 

stack..c

  1. //stack.c   
  2.     
  3. #include<stdio.h>   
  4. #include<stdlib.h>   
  5. #include "stack.h"   
  6.     
  7. STACK * InitStack() //初始化一个栈   
  8. {   
  9.     STACK *s=(STACK*)malloc(sizeof(STACK));//初始化一个栈   
  10.     if(s==NULL)   
  11.         exit(0);   
  12.     s->base=(ElemType*)malloc(STACK_INIT_SIZE*sizeof(ElemType));//为栈分配一个初始大小   
  13.     if(s->base==NULL) //如果栈底指针指向空   
  14.         exit(0);   
  15.     s->top=s->base; //空栈,使栈底与栈顶指针相同   
  16.     s->size=STACK_INIT_SIZE;   
  17.     return s;   
  18. }   
  19.     
  20. void DestroyStack(STACK *s) //销毁一个栈   
  21. {   
  22.     free(s->base);   
  23.     free(s);   
  24. }   
  25.     
  26. int Push(STACK *s,ElemType *e) //压栈   
  27. {   
  28.     if(s==NULL||e==NULL) //判断传入的参数是否合法   
  29.         return 0;   
  30.     if(s->top-s->base>=s->size)//如果满栈   
  31.     {   
  32.         s->base=(ElemType*)realloc(s->base,   
  33.             (s->size+STACK_INCREME)*sizeof(ElemType));//重新分配栈的大小   
  34.         if(s->base==NULL)//如果分配失败,返回零   
  35.             return 0;   
  36.         s->top=s->base+s->size;//重置栈顶指针   
  37.         s->size=s->size+STACK_INCREME;//重置栈大小   
  38.     }   
  39.     /*  
  40.     //写法一  
  41.     *s->top=*e;//将数据存到栈顶  
  42.     s->top++; //栈顶上移  
  43.     */  
  44.     //写法二   
  45.     *s->top++=*e;   
  46.     return 1;   
  47. }   
  48.     
  49. int Pop(STACK *s,ElemType *e) //出栈   
  50. {   
  51.     if(s==NULL||e==NULL)//判断传入的参数是否合法   
  52.         return 0;   
  53.     if(s->base==s->top) return 0; //如果是空栈,返回   
  54.     *e= *--s->top; //将栈顶元素存到*e   
  55.     return 1;   
  56. }   
  57.     
  58. int IsEmpty(STACK *s) //判断栈是否为空   
  59. {   
  60.     return s->top==s->base ? 1:0;   
  61. }

data.h

  1. //data.h   
  2.     
  3. #ifndef _DATA_H   
  4. #define _STACK_H   
  5.     
  6. typedef int ElemType; //定义一个ElemType类型   
  7.     
  8. #endif 

main.c

  1. //main.c   
  2. //将十进制数转换成八进制数   
  3. #include <stdio.h>   
  4. #include "stack.h"   
  5.     
  6. void main()   
  7. {   
  8.     int num=1348,temp;   
  9.     STACK *s= InitStack();   
  10.     while(num)   
  11.     {   
  12.         temp=num % 8;   
  13.         Push(s,&temp);   
  14.         num/=8;   
  15.     }   
  16.     printf("result is:");   
  17.     while(!IsEmpty(s))   
  18.     {   
  19.         Pop(s,&temp);   
  20.         printf("%d",temp);   
  21.     }   
  22.     printf("\n");   
  23.     DestroyStack(s);   
  24. }

2. 栈_迷宫求解

stack.h

  1. //stack.h   
  2.     
  3. #ifndef _STACK_H   
  4. #define _STACK_H   
  5.     
  6. #include "data.h"   
  7.     
  8. #define STACK_INIT_SIZE 10   
  9. #define STACK_INCREME 10   
  10.     
  11. typedef struct  
  12. {   
  13.     ElemType *base;   
  14.     ElemType *top;   
  15.     int size;   
  16. }STACK;   
  17.     
  18. STACK *InitStack();   
  19. void DestroyStack(STACK *s);   
  20. int Push(STACK *s,ElemType *e);   
  21. int Pop(STACK *s,ElemType *e);   
  22. int IsEmpty(STACK *s);   
  23.     
  24. #endif  

stack.c

  1. //stack.c   
  2.     
  3. #include<stdio.h>   
  4. #include<stdlib.h>   
  5. #include "stack.h"   
  6.     
  7. STACK * InitStack() //初始化一个栈   
  8. {   
  9.     STACK *s=(STACK*)malloc(sizeof(STACK));//初始化一个栈   
  10.     if(s==NULL)   
  11.         exit(0);   
  12.     s->base=(ElemType*)malloc(STACK_INIT_SIZE*sizeof(ElemType));//为栈分配一个初始大小   
  13.     if(s->base==NULL) //如果栈底指针指向空   
  14.         exit(0);   
  15.     s->top=s->base; //空栈,使栈底与栈顶指针相同   
  16.     s->size=STACK_INIT_SIZE;   
  17.     return s;   
  18. }   
  19.     
  20. void DestroyStack(STACK *s) //销毁一个栈   
  21. {   
  22.     free(s->base);   
  23.     free(s);   
  24. }   
  25.     
  26. int Push(STACK *s,ElemType *e) //压栈   
  27. {   
  28.     if(s==NULL||e==NULL) //判断传入的参数是否合法   
  29.         return 0;   
  30.     if(s->top-s->base>=s->size)//如果满栈   
  31.     {   
  32.         s->base=(ElemType*)realloc(s->base,   
  33.             (s->size+STACK_INCREME)*sizeof(ElemType));//重新分配栈的大小   
  34.         if(s->base==NULL)//如果分配失败,返回零   
  35.             return 0;   
  36.         s->top=s->base+s->size;//重置栈顶指针   
  37.         s->size=s->size+STACK_INCREME;//重置栈大小   
  38.     }   
  39.     /*  
  40.     //写法一  
  41.     *s->top=*e;//将数据存到栈顶  
  42.     s->top++; //栈顶上移  
  43.     */  
  44.     //写法二   
  45.     *s->top++=*e;   
  46.     return 1;   
  47. }   
  48.     
  49. int Pop(STACK *s,ElemType *e) //出栈   
  50. {   
  51.     if(s==NULL||e==NULL)//判断传入的参数是否合法   
  52.         return 0;   
  53.     if(s->base==s->top) return 0; //如果是空栈,返回   
  54.     *e= *--s->top; //将栈顶元素存到*e   
  55.     return 1;   
  56. }   
  57.     
  58. int IsEmpty(STACK *s) //判断栈是否为空   
  59. {   
  60.     return s->top==s->base ? 1:0;   
  61. }  

data.h

  1. //data.h   
  2.     
  3. #ifndef _DATA_H   
  4. #define _DATA_H   
  5.     
  6. #include "stack.h"   
  7. #include<stdio.h>   
  8.     
  9. //typedef int ElemType;   
  10. typedef struct //位置   
  11. {   
  12.     int x;   
  13.     int y;   
  14. }POS;    
  15.     
  16. typedef struct //要存放在栈中的元素的类型   
  17. {   
  18.     int sno; //编号   
  19.     POS seat; //位置   
  20.     int di; //朝向   
  21. }ElemType;   
  22.     
  23. #endif  

main.c

  1. //main.c   
  2.     
  3. #include "stack.h"   
  4. #include "data.h"   
  5. #include <stdio.h>   
  6. #include <conio.h>   
  7. #include <stdlib.h>   
  8.     
  9. int item[10][10]={   
  10.     {1,0,1,1,1,1,1,1,1,1},   
  11.     {1,0,0,1,0,0,0,1,0,1},   
  12.     {1,0,0,1,0,0,0,1,0,1},   
  13.     {1,0,0,0,0,1,1,0,0,1},   
  14.     {1,0,1,1,1,0,0,0,0,1},   
  15.     {1,0,0,0,1,0,0,0,0,1},   
  16.     {1,0,1,0,0,0,1,0,0,1},   
  17.     {1,0,1,1,1,0,1,1,0,1},   
  18.     {1,1,0,0,0,0,0,0,0,1},   
  19.     {1,1,1,1,1,1,1,1,0,1}   
  20. };   
  21. static const POS inPos={1,0},outPos={8,9};   
  22. //入口(开始位置)与出口(结束位置)   
  23. int IsPass(POS curP) //是否可通,为0可通,为1不可通   
  24. {   
  25.     return item[curP.y][curP.x]==0 ? 1:0;   
  26. }   
  27.     
  28. POS NextPos(POS curP,int di)//指向下一个位置,di表示方向   
  29. {   
  30.     POS p=curP;   
  31.     switch(di)   
  32.     {   
  33.     case 0: //向右边   
  34.         p.x++;   
  35.         break;   
  36.     case 1: //向下边   
  37.         p.y++;   
  38.         break;   
  39.     case 2: //向右边   
  40.         p.x--;   
  41.         break;   
  42.     case 3: //指向上一个   
  43.         p.y--;   
  44.         break;   
  45.     }   
  46.     return p;   
  47. }   
  48.     
  49. void PrintItem(POS curP) //印迷宫   
  50. {   
  51.     int i,j;   
  52.     system("cls"); //清空屏幕   
  53.     for(i=0;i<10;i++)   
  54.     {   
  55.         for(j=0;j<10;j++)   
  56.         {   
  57.             if(i==curP.y && j==curP.x) //如果是开始位置   
  58.             {   
  59.                 printf("");   
  60.                 continue;   
  61.             }   
  62.             if(item[i][j]==1) //如果不可通   
  63.                 printf("■");   
  64.             else            //可通   
  65.                 printf(" ");   
  66.         }   
  67.         printf("\n");   
  68.     }   
  69. }   
  70.     
  71. void main()   
  72. {   
  73.     ElemType e; //用于存放于栈中的元素结点   
  74.     int step=1; //位置计数,第几步   
  75.     POS curPos=inPos; //当前位置   
  76.     STACK *s=InitStack();//初始化栈   
  77.     PrintItem(inPos); //打印入口点,开始位置   
  78.     getch(); //等待读入字符,暂停作用   
  79.     do  
  80.     {   
  81.         if(IsPass(curPos)) //如果是当前位置可通   
  82.         {   
  83.             e.sno=step; //当前计数赋给结点的编号   
  84.             e.di=0; //朝向,默认向左   
  85.             e.seat=curPos;  //当前位置   
  86.             Push(s,&e); //把当前位置压入栈顶   
  87.             item[curPos.y][curPos.x]=2;//修改item中的值,保存足迹   
  88.             if(curPos.y==outPos.y && curPos.x==outPos.x) //如果是出口   
  89.             {   
  90.                 PrintItem(curPos); //打印当前位置   
  91.                 printf("OK!迷宫走完!\n"); //完成   
  92.                 break;   
  93.             }   
  94.             PrintItem(curPos); //打印当前位置   
  95.             getch(); //等待读入字符,暂停作用   
  96.             curPos=NextPos(curPos,0); //将栈顶位置指向栈顶位置左边的方块   
  97.             step++; //位置计数器加1   
  98.         }   
  99.         else    //当前位置不可通,则   
  100.         {   
  101.             Pop(s,&e);//取出栈顶结点   
  102.             while(e.di==4 && !IsEmpty(s) )//若栈不为空且栈顶方块位置的四周都不可通   
  103.             {   
  104.                 item[curPos.y][curPos.x]=3; //修改item的值,把不可通的位置,保存不可通的足迹   
  105.                 Pop(s,&e); //出栈,删去栈顶位置   
  106.             }   
  107.             if(e.di<3) //若栈不为空且还有其他方向未经探索   
  108.             {   
  109.                 e.di++; //改变方向   
  110.                 Push(s,&e); //把出栈的结点重新压入栈   
  111.                 curPos=NextPos(e.seat,e.di); //以新的方向往下找   
  112.             }   
  113.         }   
  114.     }while(!IsEmpty(s));   
  115.     getch(); //等待读入字符,暂停作用   
  116. }

3.简单队列

main.c

  1. //main.c   
  2.     
  3. //利用数组创建队列   
  4.     
  5. #include <stdio.h>   
  6.     
  7. #define MAX_SIZE 10   
  8.     
  9. int queue[MAX_SIZE]; //   
  10.     
  11. int rear = -1; //队尾   
  12. int front = -1; //队头   
  13.     
  14. int InQueue(int value)//进队列   
  15. {   
  16.     if(rear>=MAX_SIZE)   
  17.         return 0;   
  18.     rear++;   
  19.     queue[rear]=value;   
  20.     return 1;   
  21. }   
  22.     
  23. int OutQueue(int *value)//出队列   
  24. {   
  25.     if(front == rear)   
  26.         return 0;   
  27.     front++;   
  28.     *value=queue[front];   
  29.     return 1;   
  30. }   
  31.     
  32. void main()   
  33. {   
  34.     int temp;   
  35.     
  36.     while(1)   
  37.     {   
  38.         printf("1:存入;2:读取;=》:");   
  39.         scanf("%d",&temp);   
  40.         fflush(stdin);   
  41.         if(temp==1)   
  42.         {   
  43.             printf("请输入要存入的值:");   
  44.             scanf("%d",&temp);   
  45.             fflush(stdin);   
  46.             if(InQueue(temp)==1)   
  47.                 printf("插入队列成功\n");   
  48.             else  
  49.                 printf("队列已满!\n");   
  50.         }   
  51.         else if(temp==2)   
  52.         {   
  53.             if(OutQueue(&temp))   
  54.             {   
  55.                 printf("读取队列的值为:%d\n",temp);   
  56.             }   
  57.             else  
  58.             {   
  59.                 printf("队列为空!\n");   
  60.             }   
  61.         }   
  62.         else  
  63.             break;   
  64.     }   
  65. }  

 

4. 循环队列

mian.c

  1. //main.c   
  2.     
  3. //利用数组创建队列   
  4.     
  5. #include <stdio.h>   
  6.     
  7. #define MAX_SIZE 10   
  8.     
  9. int queue[MAX_SIZE]; //   
  10.     
  11. int rear = -1; //队尾   
  12. int front = -1; //队头   
  13.     
  14. int InQueue(int value)//进队列   
  15. {   
  16.     if(front==-1 && rear==MAX_SIZE-1||rear+1==front)//判断队列是否已满   
  17.         return 0;   
  18.     rear++;   
  19.     if(rear==MAX_SIZE)  rear=0;   
  20.     queue[rear]=value;   
  21.     return 1;   
  22. }   
  23.     
  24. int OutQueue(int *value)//出队列   
  25. {   
  26.     if(rear==front)//判断队列是否为空   
  27.         return 0;   
  28.     front++;   
  29.     if(front==MAX_SIZE) front=0;   
  30.     *value=queue[front];   
  31.     return 1;   
  32. }   
  33.     
  34. void main()   
  35. {   
  36.     int temp;   
  37.     
  38.     while(1)   
  39.     {   
  40.         printf("1:存入;2:读取;=》:");   
  41.         scanf("%d",&temp);   
  42.         fflush(stdin);   
  43.         if(temp==1)   
  44.         {   
  45.             printf("请输入要存入的值:");   
  46.             scanf("%d",&temp);   
  47.             fflush(stdin);   
  48.             if(InQueue(temp)==1)   
  49.                 printf("插入队列成功\n");   
  50.             else  
  51.                 printf("队列已满!\n");   
  52.         }   
  53.         else if(temp==2)   
  54.         {   
  55.             if(OutQueue(&temp))   
  56.             {   
  57.                 printf("读取队列的值为:%d\n",temp);   
  58.             }   
  59.             else  
  60.             {   
  61.                 printf("队列为空!\n");   
  62.             }   
  63.         }   
  64.         else  
  65.             break;   
  66.     }   

5. 双队列demo1

main.c

  1. //main.c   
  2.     
  3. //双队列   
  4.     
  5. #include<stdio.h>   
  6. #include<stdlib.h>   
  7.     
  8. typedef struct _queue   
  9. {   
  10.     int data;   
  11.     struct _queue *next;   
  12. }QUEUE;   
  13.     
  14. QUEUE * rear=NULL;   
  15. QUEUE * front=NULL;   
  16.     
  17. //输入限制型双队列   
  18. int InQueue(int value)   
  19. {   
  20.     QUEUE * q=(QUEUE *)malloc(sizeof(QUEUE));   
  21.     if(q==NULL) return 0;   
  22.     q->data=value;   
  23.     q->next=NULL;   
  24.     if(front==NULL)   
  25.         front=q;   
  26.     else  
  27.         rear->next=q;   
  28.     rear=q;   
  29.     return 1;   
  30. }   
  31.     
  32. int OutQueueByFront(int *value) //从队头取数据   
  33. {   
  34.     QUEUE *p=NULL;   
  35.     if(front==NULL)   
  36.         return 0;   
  37.     p=front;   
  38.     front=front->next;   
  39.     *value=p->data;   
  40.     free(p);   
  41.     return 1;   
  42. }   
  43.     
  44. int OutQueueByRear(int *value) //从队尾取数据   
  45. {   
  46.     QUEUE *p=NULL;   
  47.     if(rear==NULL) //如果队列为空   
  48.         return 0;   
  49.     if(rear==front) //如果队列中只有一个数据   
  50.     {   
  51.         *value=rear->data;   
  52.         free(rear);   
  53.         rear=NULL;   
  54.         front=NULL;   
  55.     }   
  56.     else //   
  57.     {   
  58.         p=front;   
  59.         while(p->next!=rear)//p不等于最后一个的前一个   
  60.             p=p->next;   
  61.         *value=rear->data;   
  62.         free(rear);   
  63.         rear=p;   
  64.         rear->next=NULL;   
  65.     }   
  66.     return 1;   
  67. }   
  68.     
  69. void main()   
  70. {   
  71.     int res,i,arr[5]={1,2,3,4,5};   
  72.     for(i=0;i<5;i++)   
  73.         InQueue(arr[i]);   
  74.     while(1)   
  75.     {   
  76.         printf("1:从队头取出;2:从队尾取出;3:退出=>");   
  77.         scanf("%d",&res);   
  78.         fflush(stdin);   
  79.         if(res==1)   
  80.         {   
  81.             if(OutQueueByFront(&res)==1)   
  82.                 printf("取出的值为:%d\n",res);   
  83.             else  
  84.                 printf("队列为空!\n");   
  85.         }   
  86.         else if(res==2)   
  87.         {   
  88.             if(OutQueueByRear(&res)==1)   
  89.                 printf("取出的值为:%d\n",res);   
  90.             else  
  91.                 printf("队列为空!\n");   
  92.         }   
  93.         else if(res==3)   
  94.             break;   
  95.     }   
  96. }  

6. 双队列demo2

main.c

  1. //main.c   
  2.     
  3. //输出限制型双队列   
  4.     
  5. #include<stdio.h>   
  6. #include<stdlib.h>   
  7.     
  8. typedef struct _queue   
  9. {   
  10.     int data;   
  11.     struct _queue *next;   
  12. }QUEUE;   
  13.     
  14. QUEUE * rear=NULL;   
  15. QUEUE * front=NULL;   
  16.     
  17. //输出限制型双队列   
  18. int OutQueue(int *value)   
  19. {   
  20.     QUEUE * p=NULL;   
  21.     if(front == NULL)   
  22.         return 0;   
  23.     p=front;   
  24.     front=front->next;   
  25.     *value = p->data;   
  26.     free(p);   
  27.     return 1;   
  28. }   
  29.     
  30. int InQueueByRear(int value)   
  31. {   
  32.     QUEUE *q=(QUEUE *)malloc(sizeof(QUEUE));   
  33.     if(q==NULL) return 0;   
  34.     q->data=value;   
  35.     q->next=NULL;   
  36.     if(rear==NULL)   
  37.         front=q;   
  38.     else  
  39.         rear->next=q;   
  40.     return 1;   
  41. }   
  42.     
  43. int InQueueByFront(int value)   
  44. {   
  45.     QUEUE *q=(QUEUE *)malloc(sizeof(QUEUE));   
  46.     if(q==NULL) return 0;   
  47.     q->data=value;   
  48.     q->next=front;   
  49.     front = q;   
  50.     if(rear==NULL)   
  51.         rear=q;   
  52.     return 1;   
  53. }   
  54.     
  55. void PrintQueue() //打印   
  56. {   
  57.     QUEUE *p=front;   
  58.     while(p)   
  59.     {   
  60.         printf("%5d",p->data);   
  61.         p=p->next;   
  62.     }   
  63.     printf("\n");   
  64. }   
  65.     
  66.     
  67. void main()   
  68. {   
  69.     int res;   
  70.     while(1)   
  71.     {   
  72.         printf("1:从队头存入;2:从队尾存入;3:退出 =");   
  73.         scanf("%d",&res);   
  74.         fflush(stdin);   
  75.         if(res==1)   
  76.         {   
  77.             printf("请输入要存入的值:");   
  78.             scanf("%d",&res);   
  79.             fflush(stdin);   
  80.             if(InQueueByFront(res))   
  81.             {   
  82.                 PrintQueue();   
  83.             }   
  84.             else  
  85.                 printf("存入失败!\n");   
  86.         }   
  87.         if(res==2)   
  88.         {   
  89.             printf("请输入要存入的值:");   
  90.             scanf("%d",&res);   
  91.             fflush(stdin);   
  92.             if(InQueueByRear(res))   
  93.             {   
  94.                 PrintQueue();   
  95.             }   
  96.             else  
  97.                 printf("存入失败!\n");   
  98.         }   
  99.         else if(res==3)   
  100.             break;   
  101.     }   

 

posted @ 2011-06-27 08:32  维唯为为  阅读(247)  评论(0编辑  收藏  举报