链式队列

来源:互联网 发布:c语言中栈是什么 编辑:程序博客网 时间:2024/05/16 11:37
#include<stdio.h>#include<malloc.h>#include<stdlib.h>#include<string.h>typedef char DataType;#include "LinkStack.h"#include "LinkQueue.h"void main(){LinkQueue LQ1, LQ2;LinkStack LS1, LS2;char str1[] = "abdfdba";char str2[] = "asdascs";int i, j = strlen(str1);char q1, q2, s1, s2;InintQueue(&LQ1);InintQueue(&LQ2);InitStack(&LS1);InitStack(&LS2);for (i = 0; i < j; i++){EnQueue(&LQ1, str1[i]);EnQueue(&LQ2, str2[i]);PushStack(LS1, str1[i]);PushStack(LS2, str2[i]);}printf("字符数列1:\n");printf("出队序列  出栈序列\n");while (!StackEmpty(LS1)){DeQueue(&LQ1, &q1);PopStack(LS1, &s1);printf("%5c", q1);printf("%10c\n", s1);if (q1 != s1){printf("字符序列1不是回文序列!");return;}}printf("字符序列1是回文序列!");printf("字符数列2:\n");printf("出队序列  出栈序列\n");while (!StackEmpty(LS2)){DeQueue(&LQ2, &q2);PopStack(LS2, &s2);printf("%5c", q2);printf("%10c\n", s2);if (q2 != s2){printf("字符序列2不是回文序列!");return;}}printf("字符序列2是回文序列!");}

/*LinkQueue.h函数*/#include<stdio.h>#include<malloc.h>#include<stdlib.h>//typedef char DataType;typedef struct QNode{DataType data;struct QNode *next;}LQNode,*LinkQueue;void InintQueue(LinkQueue *rear){if ((*rear = (LinkQueue)malloc(sizeof(LQNode))))exit(-1);else(*rear)->next = *rear;}int QueueEmpty(LinkQueue rear){if (rear->next == rear)return 1;elsereturn 0;}int EnQueue(LinkQueue *rear, DataType e){LinkQueue s;if ((s = (LinkQueue)malloc(sizeof(LQNode))) == NULL)exit(-1);s->data = e;s->next = (*rear)->next;(*rear)->next = s;*rear = s;return 1;}int DeQueue(LinkQueue *rear, DataType *e){LinkQueue f, p;if (*rear == (*rear)->next)return 0;else{f = (*rear)->next;p = f->next;if (p == *rear){*rear = (*rear)->next;(*rear)->next = *rear;}elsef->next = p->next;*e = p->data;free(p);return 1;}}

/*LinkStack.h函数*/#include<stdio.h>#include<malloc.h>#include<stdlib.h>//typedef int DataType;typedef struct node{DataType data;struct node *next;}LstackNode,*LinkStack;/*链栈初始化操作*/void InitStack(LinkStack *top){if ((*top = (LinkStack)malloc(sizeof(LstackNode))) == NULL)exit(-1);(*top)->next = NULL;  //将头结点的指针域置为空}/*判断链栈是否为空*/int StackEmpty(LinkStack top){if (top->next == NULL)return 1;elsereturn 0;}/*进栈操作*/int PushStack(LinkStack top, DataType e){LinkStack p;if ((p = (LinkStack)malloc(sizeof(LstackNode))) == NULL){printf("内存分配失败!");exit(-1);}p->data = e;//指针p指向头结点p->next = top->next;top->next = p;return 1;}/*出栈操作*/int PopStack(LinkStack top, DataType *e){LinkStack p;p = top->next;if (!p){printf("栈已空!");return 0;}top->next = p->next;  //将栈顶结点与链表断开,即出栈*e = p->data;         //将出栈元素赋给efree(p);  //释放p指向的结点return 1;}/*取栈顶元素*/int GetStack(LinkStack top, DataType *e){LinkStack p;p = top->next;if (!p){printf("栈已空!");return 0;}*e = p->data;         //将p指向的结点元素赋给ereturn 1;}/*求表长操作*/int LengthStack(LinkStack top){LinkStack p;int count = 0;p = top;//p指向栈顶指针while (p->next != NULL){p = p->next;   //依次访问栈中的结点count++;   //每次找到一个结点,计数器加一}return count;      //返回栈的长度}/*销毁链栈*/void DestoryStack(LinkStack top){LinkStack p,q;p = top;while (!p){q = p;p = p->next;free(q);}}

0 0