栈、链、队列操作的 c代码

来源:互联网 发布:java 线程池 executor 编辑:程序博客网 时间:2024/04/30 22:44

 

                                                 栈操作

//进栈算法

#include "stdio.h"

#define stacksize 100                   /*定义stacksize为常数100 */

 

int push(int s[],int x,int *ptop)

{   

       int top;

     top=*ptop;                                //ptop是指针变量;*ptop获得实际栈顶指针

       if ( top==stacksize )             //栈满

     {

              printf("overflow/n");

              return 0;

       }

       else

       {

              s[top]=x;

              top++;

              *ptop=top;                           //实际栈顶指针加1,返回到调用函数处

              return 1;  

       }

}

//出栈算法

int pop(int s[],int *py,int *ptop)

{

       int top;

       top=*ptop;                                  //ptop是指针变量;*ptop获得实际栈顶指针

       if (top==0)                                 //栈空    

       {

          printf("stack empty/n");

          return 0;

       }

   else

   {      

          --top;

          *py=s[top];                           //实际栈顶指针减1,返回到调用函数处

       *ptop=top;   

              return 1;

   }

}

 

 

int main()

{

       static int s[stacksize];

    int top=0,result,y;

       int i;

 

    result=push(s,11,&top);        //11压进栈中

       result=push(s,22,&top);        //22压进栈中

       result=push(s,33,&top);        //33压进栈中

    printf("top=%d/n",top);

 

       for (i=0;i<3;i++)

       {

              result=pop(s,&y,&top);    //11从栈中弹出

              printf("top=%d,y=%d/n",top,y);

       }

 

}

 

链栈算法

//进栈算法

#include "stdio.h"

typedef struct node

{

    int data;

    int n;

    struct node *link;

}NODE;

 

NODE *top,*head;

void push(int x)

{   

    NODE *temp = (NODE *) malloc(sizeof(NODE));

    temp->data=x;                                  //将数据插入节点

    temp->link=NULL;

 

    temp->link = top;                   //调整节点指针

    top = temp;

}

 

int pop(int *py)

{

    NODE *temp=top;

 

    if (top == head)                                //判断是否栈空

    {

        printf("stach empty!");

        return 0;

    }

    else

    {

        *py = top->data;            //将栈中数据读出

        top = top->link;              //调整栈指针

        free (temp);

        return 1;

    }

 

}

 

 

int main()

{

    int i,b,y;

    head=top=(NODE *)malloc(sizeof(NODE));

 

    push(12);         //11压进栈中

    push(13);         //11压进栈中

    push(14);         //11压进栈中

 

    for( i= 0;i<4;i++)

    {

       

        if ( (b=pop(&y) )!=0)          //11从栈中弹出

            printf("%d/n",y);

        else

             break;

     }

        getch();

   

}

队列操作

//循环队列中加入一个元素的算法:

//Q[MAX]表示循环队列

 

#define MAX 7

#include "stdio.h"

 

int EnQueue(int *Q,int x,int *pf,int *pr)

{

       int front,rear;

       front = *pf;

       rear=*pr;

       if ( (rear+1) % MAX == front )   //判断队满

              return 0;                      

       else

       {

              Q[rear]=x;                                  //将数据存入队列

              rear = ( rear+1 ) % MAX;     //队尾指针移位

         *pr=rear;                                  //保存队尾指针

         return 1;

       }

}

 

 

int DeQueue(int *Q,int *py,int *pf,int *pr)

{

       int front,rear;

       front=*pf;

       rear=*pr;

       if (front==rear)                           //判断队空

              return 0;                                    

       else

       {

              *py=Q[front];                      //将数据读出

              front=(front+1) % MAX;             //队头指针移动

              *pf=front;                                  //保存队头指针

        return 1; 

       }

}  

 

 

int main()

{

       static int s[MAX];

      

       int rear = 0,front = 0;

       int b=0;

       EnQueue(s,11,&rear,&front);              //入队操作

       EnQueue(s,12,&rear,&front);              //入队操作

       EnQueue(s,13,&rear,&front);              //入队操作

 

       DeQueue(s,&b,&rear,&front);             //出队操作

       printf("%d/n",b);

       DeQueue(s,&b,&rear,&front);             //出队操作

       printf("%d/n",b);

       DeQueue(s,&b,&rear,&front);             //出队操作

       printf("%d/n",b);

 

}

 

 

链队列

#include "stdio.h"

//链队列的类型定义:

 typedef  struct Qnode

{

    int  data;

    struct Qnode *next;

 }Qnode;

 

Qnode  *front;                   //队头指针

Qnode  *rear;                    //队尾指针

 

int EnQueue( int *e)

{

    Qnode *p;

    p=(Qnode *)malloc(sizeof(Qnode));

    if(p == NULL)

        return 0;

 

    p->data=*e;         //将数据加入到队列中

    p->next=NULL;

 

    rear->next=p;       //调整队尾指针位置,为下一次压栈做准备

    rear=p;

    return  1;

}

 

 

 

int DeQueue( int *e)

{

    Qnode *p;

 

    if(front==rear)

        return 0;              //队空

 

    p=front->next;          //获得出队指针

    *e=p->data;             //将待出队节点的数据取出

    front->next= p->next;   //调整对头指针,为下次出队做准备

 

    if(p == rear)           //若队头指针追上队尾指针,调整队头指针,此时队空

        front = rear;

    free(p);                //释放已出队数据

    return 1;

}

 

int main()

{

    int a = 11;

    int b = 0;

    rear=front=(Qnode *)malloc(sizeof(Qnode));

    EnQueue(&a);        //插入队列

    printf("%d/n",a);

 

    DeQueue(&b);        //弹出队列

    printf("%d/n",b);

}