[C语言]栈和队列的输入输出

[C语言]栈和队列的输入输出,第1张

编写程序选用顺序存储结构和链式存储结构实现抽象数据类型栈和队列,再利用栈和队列,输入若干个整数,将输入后的正整数和负整数分别保存起来,输入完成后,首先将以输入相反的次序输出所有保存的正整数,再以输入相同次序输出所有保存的负整数。


栈:先进后出 队列:先进先出

输入样例:
100 2 3 -2 -8 -6 -9 -10 50 2 -1
输出样例:
    2   50    3    2  100
   -2   -8   -6   -9  -10   -1
 代码如下:
#include 
#include 
//定义结构体
typedef struct node {
    int data;
    struct node *next;
}Node;

typedef struct Stack {
    Node *top;
    int count;
}Stack;

typedef struct Queue {
    Node *phead;
    Node *ptail;
    int count;
}Queue;
//创建栈结构体
Stack *_createStack();
//创建队列结构体
Queue *_createQueue();
//创建节点结构体
Node *_createNode();
//出栈
int _pop(Stack **s);
//进栈
void _push(Stack **s,int a);
//判空栈
int _emptyStack(Stack *S);
//获取非空栈顶元素
int _getTop(Stack *S);
//销毁栈
void _destroyStack(Stack *S);

//出队列
int _out(Queue **s);
//进队列
void _in(Queue **s,int a);
//判空队列
int _emptyQueue(Queue *S);
//获取非空队列首元素
int _getHead(Queue *S);
//销毁队列
void _destroyQueue(Queue *S);

int main()
{
    //创建栈
    Stack *s1;
    s1 = _createStack();
    //创建队列
    Queue *s2;
    s2 = _createQueue();

    //输入并分别储存在栈里
    int num;
    do {
        scanf("%d",&num);
        if(num > 0) {
            _push(&s1,num);
        }
        else if(num < 0) {
            _in(&s2,num);
        }
    }
    while(getchar() == ' ');

    //出栈
    while(_emptyStack(s1) == 0) {
        printf("%5d",_getTop(s1));
        _pop(&s1);
    }
    printf("\n");
    //出队列
    while(_emptyQueue(s2) == 0) {
        printf("%5d",_getHead(s2));
        _out(&s2);
    }

    //销毁
    _destroyStack(s1);
    _destroyQueue(s2);

    return 0;
}
//_createStack
Stack *_createStack() {
    Stack *p;
    p = (Stack*)malloc(sizeof(Stack));
    p -> count = 0; //初始化栈
    p -> top = NULL;
    return p;
}
//_createQueue
Queue *_createQueue() {
    Queue *p;
    p = (Queue*)malloc(sizeof(Queue));
    p -> count = 0; //初始化栈
    p -> phead = NULL;
    p -> ptail = NULL;
    return p;
}
//_createNode
Node *_createNode() {
    Node *p;
    p = (Node*)malloc(sizeof(Node));
    p -> next = NULL;
    return p;
}
//_emptyStack
int _emptyStack(Stack *S){
    return (S -> count == 0) ? 1 : 0;
}
//_getTop
int _getTop(Stack *S) {
    if(S == NULL)
        return 0;
    else
        return (S -> top -> data);
}
//_pop
int _pop(Stack **s){
	if (NULL == (*s) -> top)
		return 0;
    int a;
    a = (*s) -> top -> data;
    (*s) -> top = (*s) -> top -> next;
    (*s) -> count--;
	return a;
}
//_push
void _push(Stack **s,int a) {
    Node *p = (Node*)malloc(sizeof(Node));
    p -> data = a;
    p -> next = (*s) -> top;
    (*s) -> top = p;
    (*s) -> count++;
}
//_out
int _out(Queue **s) {
    int a;
    a = (*s) -> phead -> data;
    (*s) -> phead = (*s) -> phead -> next;
    return a;
}
//_in
void _in(Queue **s,int a) {
    Node *p = _createNode();
    p -> data = a;

    if(!((*s) -> phead)) {
        (*s) -> phead = p;
        (*s) -> ptail = p;
    }
    else
    {
        (*s) -> ptail -> next = p;
        (*s) -> ptail = p;
    }

}
//_emptyQueue
int _emptyQueue(Queue *S) {
    return (S -> phead == NULL ? 1 : 0);
}
//获取非空队列首元素
int _getHead(Queue *S) {
    return (S -> phead -> data);
}
//_destroyStack
void _destroyStack(Stack *S) {
    Node *p;
    p = S -> top;
    while(p) {
        S -> top = S -> top -> next;
        free(p);
        p = S -> top;
    }
}
//_destroyQueue
void _destroyQueue(Queue *S) {
    Node *p;
    p = S -> phead;
    while(p) {
        S -> phead = p -> next;
        free(p);
        p = S -> phead;
    }
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/567947.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-09
下一篇 2022-04-09

发表评论

登录后才能评论

评论列表(0条)

保存