zoukankan      html  css  js  c++  java
  • 【C】栈和队列的实现

    Stack.h

    #ifndef _STACK_H_
    #define _STACK_H_
    #include<stdio.h>
    #include<assert.h>
    #include<string.h>
    #include<stdlib.h>
    #include<windows.h>
    
    typedef int DataType;
    
    typedef struct Stack{
    	DataType *_arr;
    	size_t top;
    	size_t end;
    }Stack;
    
    void StackInit(Stack *st);
    void StackExpand(Stack *st);
    void StackPush(Stack *st,DataType x);
    void StackPop(Stack *st);
    void printSatck(Stack st);
    int StackEmpty(Stack st);
    DataType StackTop(Stack *st);
    size_t StackSize(Stack st);
    void test1();
    
    #endif

    Stack.c

    #include "Stack.h"
    
    void StackInit(Stack *st){
    	assert(st);
    	st->top=0;
    	st->end=1;
    	st->_arr=(DataType *)malloc(sizeof(DataType)*st->end);
    }
    
    void StackExpand(Stack *st){
    	assert(st);
    	st->_arr=(DataType *)realloc(st->_arr,sizeof(DataType)*st->end*2);
    	st->end*=2;
    }
    int StackEmpty(Stack st){
    	if(st.top==0)
    		return 0;
    	return 1;
    }
    size_t StackSize(Stack st){
    	return st.top;
    }
    
    void StackPush(Stack *st,DataType x){
    	assert(st);
    	if(st->top < st->end){
    		st->_arr[st->top]=x;
    		++st->top;
    	}
    	else{
    		StackExpand(st);
    		st->_arr[st->top]=x;
    		++st->top;
    	}
    }
    
    void StackPop(Stack *st){
    	if(st->top>0)
    		--st->top;
    	else{
    		printf("Stack is empty
    ");
    	}
    }
    
    void printSatck(Stack st){
    	while(st.top){
    		printf("%d
    ",st._arr[--st.top]);
    	}
    }
    
    DataType StackTop(Stack *st){
    	if(st->top>0)
    		return st->_arr[st->top-1];
    	return NULL;
    }
    
    void test1(){
    	Stack st;
    	StackInit(&st);
    	StackPush(&st,1);
    	StackPush(&st,2);
    	StackPush(&st,3);
    	StackPush(&st,4);
    	StackPush(&st,5);
    	printSatck(st);
    	printf("size :%d
    ",StackSize(st));
    	StackPop(&st);
    	StackPop(&st);
    	printSatck(st);
    	printf("size :%d
    ",StackSize(st));
    	printf("%d 
    ",StackTop(&st));
    	StackPop(&st);
    	printf("%d 
    ",StackTop(&st));
    	StackPop(&st);
    	printf("%d 
    ",StackTop(&st));
    	StackPop(&st);
    	printf("%d 
    ",StackTop(&st));
    	StackPop(&st);
    	printf("%d 
    ",StackTop(&st));
    	StackPop(&st);
    	printf("%d 
    ",StackTop(&st));
    
    }

    Queue.h

    #ifndef _QUEUE_H_
    #define _QUEUE_H_
    #include<stdio.h>
    #include<assert.h>
    #include<string.h>
    #include<stdlib.h>
    #include<windows.h>
    
    typedef int DataType;
    
    typedef struct QueueNode{
    	DataType _data;
    	struct QueueNode *_next;
    }QueueNode;
    
    typedef struct Queue{
    	QueueNode *_head;
    	QueueNode *_tail;
    }Queue;
    
    void QueueInit(Queue* q); 
    QueueNode* BuyQueueNode(DataType x);
    void QueuePush(Queue* q, DataType x); 
    void QueuePop(Queue* q); 
    DataType QueueFront(Queue* q); 
    DataType QueueBack(Queue* q); 
    size_t QueueSize(Queue* q); 
    int QueueEmpty(Queue* q); 
    void TestQueue();
    
    #endif

    Queue.c

    #include"Queue.h"
    
    void QueueInit(Queue* q){
    	assert(q);
    	q->_head = q->_tail =(QueueNode*)malloc(sizeof(QueueNode));
    	q->_tail->_next=NULL;
    }
    QueueNode* BuyQueueNode(DataType x){
    	QueueNode *qNode;
    	qNode = (QueueNode*)malloc(sizeof(QueueNode));
    	qNode->_data=x;
    	qNode->_next=NULL;
    	return qNode;
    }
    void QueuePush(Queue* q, DataType x){
    	QueueNode *newNode;
    	assert(q);
    	newNode = BuyQueueNode(x);
    	q->_tail->_next = newNode;
    	q->_tail = q->_tail->_next;
    }
    
    void QueuePop(Queue* q){
    	QueueNode *head;
    	assert(q);
    	head = q->_head;
    	if(q->_head==q->_tail){
    		printf("队空
    ");
    		return;
    	}
    	q->_head=q->_head->_next;
    	free(head);
    }
    
    void printQueue(Queue q){
    	QueueNode *cur;
    	cur=q._head->_next;
    	while(cur!=q._tail->_next){
    		printf("%d
    ",cur->_data);
    		cur=cur->_next;
    	}
    }
    
    DataType QueueFront(Queue* q){
    	assert(q);
    	if(QueueEmpty(q))
    		return q->_head->_next->_data;
    	return NULL;
    }
    DataType QueueBack(Queue* q){
    	assert(q);
    	if(QueueEmpty(q))	
    		return q->_tail->_data;
    	return NULL;
    }
    
    size_t QueueSize(Queue* q){
    	int n=0;
    	QueueNode *cur;
    	assert(q);
    	cur = q->_head->_next;
    	while(cur){
    		n++;
    		cur = cur->_next;
    	}
    	return n;
    }
    
    int QueueEmpty(Queue* q){
    	assert(q);
    	if(q->_head==q->_tail)
    		return 0;
    	return 1;
    }
    
    
    void TestQueue(){
    	Queue q;
    	QueueInit(&q);
    	QueuePush(&q,1);
    	QueuePush(&q,2);
    	QueuePush(&q,3);
    	QueuePush(&q,4);
    	printQueue(q);
    	printf("front %d 
    ",QueueFront(&q));
    	printf("back %d 
    ",QueueBack(&q));
    	printf("size %d 
    ",QueueSize(&q));
    	QueuePop(&q);
    	QueuePop(&q);
    	QueuePop(&q);
    	QueuePop(&q);
    	QueuePop(&q);
    	QueuePop(&q);
    	printQueue(q);
    	printf("front %d 
    ",QueueFront(&q));
    	printf("back %d 
    ",QueueBack(&q));
    	printf("size %d 
    ",QueueSize(&q));
    }
    

    main.c

    #include"Stack.h"
    #include"Queue.h"
    
    int main(){
    //	test1();
    	TestQueue();
    	system("pause");
    	return 0;
    }



  • 相关阅读:
    Processing中PImage类和loadImage()、createImage()函数的相关解析
    基于Maxmspjitter的基础【pixel shader】绘制模板Patcher
    Processing多窗口程序范例(三)
    SpringBoot:基于注解的@CachePut
    Android开发—错误记录1:W/System.err: java.net.ConnectException: Connection refused
    自控力第一章-我要做,我不要,我想要:什么是意志力?为什么意志力至关重要
    超算结课小结
    linpack_2
    搭建Linpack
    汇编程序返回dos
  • 原文地址:https://www.cnblogs.com/yongtaochang/p/13615376.html
Copyright © 2011-2022 走看看