队列和栈(C语言)

栈和队列的基本性质

  1. 栈是先进后出的结构(弹夹)
  2. 队列是先进先出的(排队)
  3. 栈和队列在实现结构上可以有数组和链表两种方式

栈结构的基本操作:

1、弹栈

2、访问栈顶元素

3、压栈操作

4、返回当前栈中的元素个数

 

队列结构的基本操作:

1、队列元素进队列是从队尾插入的

2、队列元素出队列是从对头弹出的             (类似于日常排队) 

 

/*栈的结构定义(顺序栈)*/

#define MAXSIZE    100

typedef int ElemType;    /*视栈中要存放的具体元素类型而定*/
typedef int Status;

typedef struct 
{
    ElemType data[MAXSIZE];  //利用系统的栈来存放栈元素
    int top;        
}Stack;


typedef struct
{
    ElemType *base;        //利用堆来存放栈的元素
    ElemType *top;         //栈顶指针和栈底指针
    int count;             //栈的大小
}Stack1;


 

/*栈的初始化*/

Status InitStack(Stack *s)
{
    if(s == NULL)
    {
        return -1;
    }
    
    s->top = -1;       //初始化栈顶指针
    return 0;
}


#define NUM    10

Status InitStack(Stack1 *s1)
{
    if(s1 == NULL)
    {
        return -1;
    }
    
    s1->top = (ElemType *)malloc(sizeof(ElemType)*NUM);    //初始化栈顶指针
    if(NULL == s1->top)
    {
        return -1;  //申请内存失败
    }
    s1->base = s1->top;  
    s1->count = 0;
    
    return 0;
}


 

/*压栈*/

Status push(Stack *s, ElemType e)
{
    if(s->top == MAXSIZE-1)
    {
        //栈满
        return -1;
    }
    s->top++; 
    s->data[s->top] = e;
           
    return 0;
}

Status push(Stack1 *s1, ElemType e)
{
    if(s1->top == s1->base+NUM-1)
    {
        //栈满
        return -1;
    }
 
    *(s1->top) = e;
    s1->top++;
    s1->count++;      
    return 0;
}

/*出栈*/

Status pop(Stack *s, ElemType *e)
{
    if(s->top == -1)
    {
        //栈空
        return -1;
    }

    *e = s->data[s->top];
    s->top--;
    return 0;
}

Status pop(Stack1*s1, ElemType *e)
{
    if(s1->top == s1->base)
    {
        //栈空
        return -1;
    }

    *e = *(s1->top);
    s1->top--;
    s->count--;
    return 0;
}

 

/*链式栈*/

/*栈元素的(节点的结构)和栈结构的定义*/

typedef int ElemType;
typedef int Status;

/*结点的结构*/
typedef struct node 
{
    ElemType data;
    struct node *next;
}Node;


/*栈的结构*/
typedef struct 
{
    struct node *top;    //栈顶指针
    int count;           //用来记录栈元素的个数
}Stack;

 

/*链栈初始化*/

Status InitStack(Stack *s)
{
    if(NULL == s)
    {
        return -1;
    }
    
    s->top = NULL;    //栈空,栈顶指针指向NULL
    s->count = 0;    
    return 0;
}

 

/*把链表的头指针当作栈的栈顶指针,链表的尾结点当作栈底*/

/*栈元素只在栈顶进栈或者出栈*/

/*压栈*/

Status push(Stack *s, ElemType e)
{
    if(NULL == s)
    {
        return -1;
    }
    Node *p = (Node *)malloc(sizeof(Node));    //申请一个节点
    if(NULL == p)
    {
        return -1;
    }
    p->data = e;
    p->next = s->top;      //令新创建的节点指向当前的栈顶
    s->top = p;            //栈顶指针指向新压栈的元素
    s->count++;
    return 0;       
}

 

/*出栈*/

Status pop(Stack *s, ElemType *e)
{
    if(NULL==s || NULL==s->top)
    {
        return -1;
    }
    
    *e = s->top->data;        //取出栈顶元素的值
    Node *temp = s->top;      //保存当前要出栈的栈顶元素的地址
    s->top = s->top->next;    //将栈顶指针移向下一个元素
    s->count--;
    free(temp);               //释放当前栈顶元素的指针
    return 0;
}

 

 

 

/*循环队列(顺序结构)*/

#define MAXSIZE    10    //队列的大小

typedef int ElemType;
typedef int Status;


/*顺序队列结构*/
typedef struct 
{
    ElemType data[MAXSIZE];    //利用系统的栈来存放队列的元素
    int front;        //队头(出队)
    int rear;         //队尾(进队)
}Queue;


 

/*队列的初始化*/

Status InitQueue(Queue *q)
{
    if(NULL == q)
    {
        return -1;
    }
    
    q->rear = 0;
    q->front = 0;
    return 1;
}

 

/*返回循环队列的元素个数*/

int NumQueue(Queue *q)
{
    return (q->rear-q->front+MAXSIZE)%MAXSIZE;
}

 

/*进队列(从队尾进队)*/

Status EnQueue(Queue *q, ElemType e)
{
    if(NULL==q || (q->rear+1)%MAXSIZE==q->front)
    {
        return -1;
    }
    
    q->data[q->rear] = e;
    q->rear = (q->rear+1) % MAXSIZE;
    return 0;
}

 

/*出队列(从队头出队)*/

Status OutQueue(Queue *q, ElemType *e)
{
    if(q==NULL || q->rear==q->front)
    {
        return -1;
    }
    
    *e = q->data[q->front];
    q->front = (q->front+1) % MAXSIZE;
    
    return 0;
}

 

/*用链表实现队列*/

/*队列元素的结点结构和队列结构*/

/*队列存储的元素的类型*/
typedef int ElemType;
typedef int Status;

/*结点结构*/
typedef struct node
{
    ElemType data;
    struct node *next;
}Node;

/*队列结构(一个队头指针,一个队尾指针)*/
typedef struct 
{
    struct node *front;
    struct node *rear;
}Queue;

 

/*链式队列的初始化*/

/*无头结点*/
Status InitQueue(Queue *q)
{
    if(NULL == q)
    {
        return -1;
    }
    q->front = NULL;
    q->rear = NULL; 
    return 0;   
}

/*有头结点*/

Status InitQueue(Queue *q)
{
    if(NULL == q)
    {
        return -1;
    }

    /*创建一个头结点*/
    Node *p = (Node *)malloc(sizeof(Node));
    if(NULL == p)
    {
        return -1;
    }

    /*初始化头结点和队头队尾指针*/
    p->next = NULL;
    q->front = p;
    q->rear = p;

    return 0;
}

 

/*元素进队列*/

Status EnQueue(Queue *q, ElemType e)
{
    if(NULLL == q)
    {
        return -1;
    }
    Node *temp = (Node *)malloc(sizeof(Node));
    if(NULL == temp)
    {
        //申请内存失败
        return -1;
    }
    /*初始化新插入的节点*/
    temp->next = NULL;
    temp->data = e;
    /*从队尾连接新插入的节点并把队尾指针指向新插入的节点*/
    q->rear->next = temp;
    q->rear = temp;
    return 0;
}

 

/*元素出队列*/

/*无头结点版本,从队头出队*/
Statu OutQueue(Queue *q, ElemType *e)
{
    if(NULL == q || (NULL==q->rear))
    {
        return -1;
    }
    if(q->front == q->rear)
    {
        /*如果大家都指向同一个节点证明队列只有一个元素,那么q->rear也应该要改变*/
        free(q->front);
        q->front = NULL;
        q->rear = NULL;  
        return 0;
    }
  
    Node *temp = q->front;
    *e = q->front->data; 
     
    q->front = q->front->next;
    free(temp);

    return 0;
}


/*有头结点版本*/
Status OutQueue(Queue *q, ElemType *e)
{
    if(NULL==q || (q->rear==q->front))
    {   
        return -1;
    }  
    Node *temp = q->front->next; 
    if(q->front->next == q->rear)
    {
        q->rear = q->front;
    }
    q->front->next = temp->next;
    *e = temp->data;
    free(temp);
    
    return 0;
}

/*计算队列元素的数量*/

/*无头结点*/
int NumQueue(Queue *q)
{
	if(NULL == q)
	{
		return -1;
	}
	int count = 1;
	Node *temp1 = q->front;
	Node *temp2 = q->rear;
	if(temp1==NULL && temp2==NULL)
	{
		return 0;
	}
	else if(temp1 == temp2)
	{
		return count;
	}
	
	while(temp1 != temp2)
	{
		count++;
		temp1 = temp1->next;;
	}
	return count;
}

 

 

  • 1
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1. 首先定义两个队列queue1和queue2,其中queue2始终为空。 2. 入操作:将元素插入queue1队列中。 3. 出操作:将queue1队列中的元素依次出队,直到队列中只剩下一个元素,然后将该元素出队并返回。 4. 反复进行入和出操作时,每次出前需要先将queue1中的元素依次出队并插入到queue2中,然后再将queue1中的最后一个元素出队并返回,最后将queue2中的元素再次依次出队并插入到queue1中,以维持queue2为空的状态。 以下是示例代码: ``` #include <stdio.h> #include <stdlib.h> #define MAX_QUEUE_SIZE 100 typedef struct { int data[MAX_QUEUE_SIZE]; int front; int rear; } Queue; void initQueue(Queue *queue) { queue->front = 0; queue->rear = 0; } int isQueueEmpty(Queue *queue) { return queue->front == queue->rear; } int isQueueFull(Queue *queue) { return (queue->rear + 1) % MAX_QUEUE_SIZE == queue->front; } int enqueue(Queue *queue, int value) { if (isQueueFull(queue)) { return 0; } queue->data[queue->rear] = value; queue->rear = (queue->rear + 1) % MAX_QUEUE_SIZE; return 1; } int dequeue(Queue *queue) { if (isQueueEmpty(queue)) { return 0; } int value = queue->data[queue->front]; queue->front = (queue->front + 1) % MAX_QUEUE_SIZE; return value; } typedef struct { Queue queue1; Queue queue2; } Stack; void initStack(Stack *stack) { initQueue(&stack->queue1); initQueue(&stack->queue2); } int isStackEmpty(Stack *stack) { return isQueueEmpty(&stack->queue1); } void push(Stack *stack, int value) { enqueue(&stack->queue1, value); } int pop(Stack *stack) { int value = 0; while (!isQueueEmpty(&stack->queue1)) { value = dequeue(&stack->queue1); if (!isQueueEmpty(&stack->queue1)) { enqueue(&stack->queue2, value); } } while (!isQueueEmpty(&stack->queue2)) { enqueue(&stack->queue1, dequeue(&stack->queue2)); } return value; } int main() { Stack stack; initStack(&stack); push(&stack, 1); push(&stack, 2); push(&stack, 3); printf("%d\n", pop(&stack)); printf("%d\n", pop(&stack)); printf("%d\n", pop(&stack)); return 0; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值