数据结构-栈与队列

概念

栈: 先进后出

队列:先进先出

实现

数组仿真栈

#include"stdio.h"
#define MaxSize 10
int stack[MaxSize];
int top=-1;
void push(int value)
{
    int i;
    if(top>=MaxSize)
        printf("\nThe stack is full!\n");
    else
        {
            printf("\nThe stack content before(top->bottom):");
            for(i=top;i>=0;i--)
                printf("[%d]",stack[i]);
            top++;
            stack[top]=value;
            printf("\nThe stack content after push(top->bottom):");
            for(i=top;i>=0;i--)
                printf("[%d]",stack[i]);
            printf("\n");
        }
}
int pop()
{
    int temp;
    int i;
    if(top<0)
        {
            printf("\nThe stack is empty!\n");
            return -1;
        }
        printf("\nThe stack content before(top->bottom):");
        for(i=top;i>=0;i--)
            printf("[%d]",stack[i]);
        temp=stack[top];
        top--;
        printf("\nThe pop value is [%d]",temp);
        printf("\nThe stack content after pop(top->bottom):");
        for(i=top;i>=0;i--)
                printf("[%d]",stack[i]);
            printf("\n");
        return temp;
}
void main()
{
    int select;
    int stack[5];
    int i,value;
    printf("\n(1)Input a stack data");
    printf("\n(2)Output a stack data");
    printf("\n(3)Exit");
    printf("\nPlease select one=>");
    scanf("%d",&select);
    do
    {
        switch(select)
        {
            case 1:printf("\nPlease input the data=>");
            scanf("%d",&value);
            push(value);
            break;
            case 2:value=pop();
            break;
        }
        printf("\n(1)Input a stack data");
        printf("\n(2)Output a stack data");
        printf("\n(3)Exit");
        printf("\nPlease select one=>");
        scanf("%d",&select);
        printf("\n");
    }while(select!=3);
}

链表仿真栈

#include <stdio.h>

struct s_node{
    int data;
    struct s_node *next;
};

typedef struct s_node s_list;
typedef s_list *link;

link stack = NULL;

void print_stack(){
    link temp = NULL;
    temp = stack;
    if(temp == NULL){
        printf("Stack is null!\n");
    }else{
        while(temp != NULL){
            printf("[%d] ", temp->data);
            temp = temp->next;
        }
        putchar(10);
    }
}

void push(int value){
    link newnode;
    printf("input:");
    print_stack();
    newnode = (link)malloc(sizeof(s_list));
    newnode->data = value;
    newnode->next = stack;
    stack = newnode;
}

int pop(){
    link top;
    int temp;
    print_stack();

    if(stack != NULL){
        top = stack;
        stack=stack->next;
        temp = top->data;
        free(top);
        return temp;
    }else
        return -1;
}

void main()
{
    link point;
    int select;
    int i,value;
    printf("\n(1)Input a stack data");
    printf("\n(2)Output a stack data");
    printf("\n(3)Exit");
    printf("\nPlease select one=>");
    scanf("%d",&select);
    do
    {
        switch(select)
        {
            case 1:printf("\nPlease input the data=>");
            scanf("%d",&value);         
            push(value);
            printf("\nThe stack content current(top->bottom):");
            print_stack();
            break;
            case 2:value=pop();
            printf("\nThe output value is [%d]",value);
            printf("\n");
            printf("The stack content currnet(top->bottom):");
            print_stack();
            break;
        }
        printf("\n(1)Input a stack data");
        printf("\n(2)Output a stack data");
        printf("\n(3)Exit");
        printf("\nPlease select one=>");
        scanf("%d",&select);
    }while(select!=3);
}

顺序栈公用

#include "stdio.h"
#define MEMORY_SIZE  100  /* 内存大小 */
#define MAX_STACKS 10      /* 栈的个数加1*/
typedef char element;
element memory [MEMORY_SIZE];
int top[MAX_STACKS-1];
int boundary[MAX_STACKS];
/* 置栈空*/
void Initial(int n)
{/*将各个顺序栈置空*/
    int i;
    top [0] = boundary [0] = -1;
    for ( i =1;  i<n;  i ++ )
        top[i]=boundary[i]= (MEMORY_SIZE/n)*i;
    boundary[n]=MEMORY_SIZE-1;
}
/*进栈*/
void Push(int i, element item)
{
    if (top[i] == boundary[i+1] )
    {
        printf("第%d个栈已满。",i);
        exit(1);
    }
    memory[++top[i]] = item;
}
/*退栈*/
element Pop(int i)
{
    if ( top[i] == boundary[i] )
    {
        printf("第%d个栈已空。",i);
        exit(1);
    }
    return memory[top[i]--];
}
void main()
{
    int  n=2;      /* 使用的栈的个数 */
    element first,sec;
    Initial(n);
    Push(1,'a');
    Push(0,'b');
    sec=Pop(1);
    first=Pop(0);
}

进制转换

#include "stdio.h"
#define StackSize 100 /*假定预分配的栈空间最多为100个元素*/  	
typedef int DataType;/*假定栈元素的数据类型为字符*/  	
typedef struct{
      DataType data[StackSize];
      int top;
}SeqStack;   
/* 置栈空*/
void Initial(SeqStack *S)
{/*将顺序栈置空*/
      S->top=-1;
} 
/*判栈空*/
int IsEmpty(SeqStack *S)
{
    return S->top==-1;
}
/*判栈满*/
int IsFull(SeqStack *S)
{
	return S->top==StackSize-1;
}
/*进栈*/
void Push(SeqStack *S,DataType x)
{
    if (IsFull(S))
	{
		printf("栈上溢"); /*上溢,退出运行*/
		exit(1);
	}
    S->data[++S->top]=x;/*栈顶指针加1后将x入栈*/
}
/*出栈*/
DataType Pop(SeqStack *S)
{
	if(IsEmpty(S))
	{
		printf("栈为空"); /*下溢,退出运行*/
		exit(1);
	}
	return S->data[S->top--];/*栈顶元素返回后将栈顶指针减1*/
}
/* 取栈顶元素*/
DataType Top(SeqStack *S)
{
	if(IsEmpty(S))
	{
		printf("栈为空"); /*下溢,退出运行*/
		exit(1);
	}
	return S->data[S->top];
}
void MultiBaseOutput (int N,int B)
{/*假设N是非负的十进制整数,输出等值的B进制数*/
	int i;
	SeqStack S;
	Initial(&S);
	while(N){  /*从右向左产生B进制的各位数字,并将其进栈*/
		Push(&S,N%B); /*将bi进栈0<=i<=j*/
		N=N/B;
	}
	while(!IsEmpty(&S)){  /*栈非空时退栈输出*/
		i=Pop(&S);
		printf("%d",i);
	}
}
void main()
{
	MultiBaseOutput(1023,2);
}

顺序队列

#include "stdio.h"
#define QueueSize 100 /*假定预分配的队列空间最多为100个元素*/  	
typedef int DataType;/*假定队列元素的数据类型为字符*/  	
typedef struct{
      DataType data[QueueSize];
      int front;/*头指针*/
	  int rear;/*尾指针*/
}SeqQueue;   
/* 置队列空*/
void Initial(SeqQueue *Q)
{/*将顺序队列置空*/
      Q->front=Q->rear=0;
} 
/*判队列空*/
int IsEmpty(SeqQueue *Q)
{
    return Q->front==Q->rear;
}
/*判队列满*/
int IsFull(SeqQueue *Q)
{
	return Q->rear==QueueSize-1+Q->front;
}
/*进队列*/
void Push(SeqQueue *Q,DataType x)
{
    if (IsFull(Q))
	{
		printf("队列上溢"); /*上溢,退出运行*/
		exit(1);
	}
    Q->data[Q->rear++]=x;/*队列顶指针加1后将x入队列*/
}
/*出队列*/
DataType Pop(SeqQueue *Q)
{
	if(IsEmpty(Q))
	{
		printf("队列为空"); /*下溢,退出运行*/
		exit(1);
	}
	return Q->data[Q->front++];/*队列顶元素返回后将队列顶指针减1*/
}
/* 取队列顶元素*/
DataType Top(SeqQueue *Q)
{
	if(IsEmpty(Q))
	{
		printf("队列为空"); /*下溢,退出运行*/
		exit(1);
	}
	return Q->data[Q->front];
}
void main()
{
	SeqQueue s;
	DataType first,sec;
	Initial(&s);
	Push(&s,'a');
	Push(&s,'b');
	first=Top(&s);
	Pop(&s);
	sec=Top(&s);
	Pop(&s);
}

循环队列

#include "stdio.h"
#define QueueSize 100 /*假定预分配的队列空间最多为100个元素*/  	
typedef int DataType;/*假定队列元素的数据类型为字符*/  	
typedef struct{
	DataType data[QueueSize];
	int front;/*头指针*/
	int rear;/*尾指针*/
	int count; /*计数器,记录队中元素总数*/           
}CirQueue;   
/* 置队列空*/
void Initial(CirQueue *Q)
{/*将顺序队列置空*/
	Q->front=Q->rear=0;
	Q->count=0;     /*计数器置0*/       
} 
/* 判队列空*/
int IsEmpty(CirQueue *Q)
{
	return Q->front==Q->rear;
}
/*判队列满*/
int IsFull(CirQueue *Q)
{
	return Q->rear==QueueSize-1+Q->front;
}
/*进队列*/
void EnQueue(CirQueue *Q,DataType x)
{
	if (IsFull(Q))
	{
		printf("队列上溢"); /*上溢,退出运行*/
		exit(1);
	}
	Q->count ++;                        /*队列元素个数加1*/
	Q->data[Q->rear]=x;                 /*新元素插入队尾*/
	Q->rear=(Q->rear+1)%QueueSize;      /*循环意义下将尾指针加1*/
}
/*出队列*/
DataType DeQueue(CirQueue *Q)
{
	DataType temp;
	if(IsEmpty(Q))
	{
		printf("队列为空"); /*下溢,退出运行*/
		exit(1);
	}
	temp=Q->data[Q->front];
	Q->count--;                        /*队列元素个数减1*/
	Q->front=(Q->front+1)%QueueSize;   /*循环意义下的头指针加1*/
	return temp; 
	
}
/* 取队列顶元素*/
DataType Front(CirQueue *Q)
{
	if(IsEmpty(Q))
	{
		printf("队列为空"); /*下溢,退出运行*/
		exit(1);
	}
	return Q->data[Q->front];
}
void main()
{
	CirQueue s;
	DataType first,sec;
	Initial(&s);
	EnQueue(&s,'a');
	EnQueue(&s,'b');
	first=Front(&s);
	DeQueue(&s);
	sec=Front(&s);
	DeQueue(&s);
}

链式队列

#include "stdio.h"
#define QueueSize 100 /*假定预分配的队列空间最多为100个元素*/  
typedef char DataType ; /*假定队列元素的数据类型为字符*/
typedef struct node{
	DataType data;
	struct node *next;
}QueueNode;
typedef struct{
	QueueNode *front;  /*头指针*/
	QueueNode *rear;
}LinkQueue;
/* 置队列空*/

void Initial(LinkQueue *Q)
/*将顺序队列置空*/

{    Q->front=Q->rear=NULL;
} 

/*判队列空*/
int IsEmpty(LinkQueue *Q)
{
    return Q->front==NULL&&Q->rear==NULL;
}

/*进队列*/
void Push(LinkQueue *Q,DataType x)
{
/*将元素x插入链队列尾部*/
	QueueNode *p=(QueueNode *)malloc(sizeof(QueueNode));/*申请新结点*/
	p->data=x;
	p->next=NULL;
    if(IsEmpty(Q))
		Q->front=Q->rear=p;  /*将x插入空队列*/
	else 
	{ /*x插入非空队列的尾*/
		Q->rear->next=p;     /*p链到原队尾结点后*/
		Q->rear=p;           /*队尾指针指向新的尾*/
	}
}

/*出队列*/
DataType Pop(LinkQueue *Q)
{
	DataType x;
	QueueNode *p;
	if(IsEmpty(Q))
	{
		printf("队列为空");/*下溢*/
		exit(1);
	}
	p=Q->front;                   /*指向对头结点*/
	x=p->data;                    /*保存对头结点的数据*/
	Q->front=p->next;             /*将对头结点从链上摘下*/
    if(Q->rear==p)/*原队中只有一个结点,删去后队列变空,此时队头指针已为空*/
		Q->rear=NULL;
	free(p);   /*释放被删队头结点*/
	return x;  /*返回原队头数据*/
}

/* 取队列顶元素*/
DataType Front(LinkQueue *Q)
{
	if(IsEmpty(Q))
	{
		printf("队列为空"); /*下溢,退出运行*/
		exit(1);
	}
	return Q->front->data;
}

void main()
{
	LinkQueue s;
	DataType first,sec;
	Initial(&s);
	Push(&s,'a');
	Push(&s,'b');
	first=Front(&s);
	Pop(&s);
	sec=Front(&s);
	Pop(&s);
}

posted @ 2017-06-05 21:45  self-imporvement  阅读(378)  评论(0编辑  收藏  举报