用c语言进行链式队列的创建,编译链接没错,但是运行的时候程序被终止。以下是源代码和注释

Python012

用c语言进行链式队列的创建,编译链接没错,但是运行的时候程序被终止。以下是源代码和注释,第1张

自行比对这两个函数吧

void insert_link(struct linkqueue *ps,int val)//完成队列的增加。

{

    struct node * pnew=(struct node*)malloc(sizeof(struct node))//申请一个节点

    pnew->data=val//将要放入队列的值赋给节点的数据域

    pnew->next=NULL    

//  pnew=ps->rear->next//将rear指向新的节点,并将新的节点的指针域置空。

    ps->rear->next=pnew

    ps->rear=ps->rear->next

//  pnew->next=NULL

}

void traverse_link(struct linkqueue *ps)//完成队列遍历

{

    struct node *p=ps->front->next//申请一个临时指针变量,以完成队列遍历。因为头节点没有存放数据所以让指针指向front下一个节点

    while(/*p->next*/p!=NULL) { //当指针指向的节点的指针域不为空时就继续下移,并且输出本节点的数据

        printf("%d ",p->data)

        p=p->next

    }

}

#include <stdio.h>

#include <string.h>

#include <stdlib.h>

const MaxStackSize = 50

typedef char ElemType

typedef struct snode {

ElemType data

struct snode *next

}*LinkStack,*psNode

LinkStack GetEmptyStack() { // 初始化

LinkStack head = (psNode)malloc(sizeof(struct snode))

if((head) == NULL) {

printf("内存空间不足无法插入!\n")

return NULL

}

head->data = '0'

head->next = NULL

return head

}

int StackNotEmpty(LinkStack head) { // 是否为空

return (head->next != NULL)

}

int StackPush(LinkStack head,ElemType x) { // 入栈

psNode p = (psNode)malloc(sizeof(struct snode))

if(p == NULL) {

printf("内存空间不足无法插入!\n")

return 0

}

p->data = x

p->next = head->next

head->next = p

return 1

}

int StackPop(LinkStack head,ElemType *x) { //删除元素(弹出)

psNode p = head->next

if(StackNotEmpty(head)) {

*x = p->data

head->next = p->next

free(p)

return 1

}

return 0

}

typedef struct qnode  { // 队列

ElemType data

struct qnode *next

}*pqNode

typedef struct queue{

pqNode front

    pqNode rear

}*LinkQueue

LinkQueue GetEmptyQueue() { //初始化

LinkQueue Q = (struct queue *)malloc(sizeof(struct queue))

if(Q == NULL) {

printf("内存空间不足无法插入!\n")

return NULL

}

Q->rear = NULL

    Q->front = NULL

return Q

}

int QueueNotEmpty(LinkQueue Q) { //是否为空

return (Q->front != NULL)

}

int QueueAppend(LinkQueue Q,ElemType x) { //入队列

struct qnode *p = (struct qnode *)malloc(sizeof(struct qnode))

if(p == NULL) {

printf("内存空间不足!\n")

return 0

}

p->data = x

p->next = NULL

if(Q->front == NULL) // 队列为空

Q->front = Q->rear = p

else {

Q->rear->next = p

Q->rear = p

}

return 1

}

int QueueDelete(LinkQueue Q,ElemType *d) { //出队列(删除)

pqNode p

if(QueueNotEmpty(Q)) {

*d = Q->front->data

p = Q->front

Q->front = p->next

if(p->next == NULL) Q->rear = NULL

free(p)

return 1

}

printf("队列已空无数据出队列!\n")

return 0

}

typedef struct sqstack { //顺序栈(进制)

ElemType stack[MaxStackSize]

int size

}SequenceStack

void StackInitiate(SequenceStack *S) {

S->size = 0

}

int StackNotEmpty(SequenceStack *S) {

return (S->size > 0)

}

int StackPush(SequenceStack *S,ElemType x) {

if(S->size >= MaxStackSize) {

printf("堆栈已满无法插入!\n")

return 0

}

S->stack[S->size] = x

++S->size

return 1

}

int StackPop(SequenceStack *S,ElemType *d) {

if(S->size == 0) {

printf("堆栈已空!\n")

return 0

}

--S->size

*d = S->stack[S->size]

return 1

}

int main() { //主函数在这里

LinkStack head = GetEmptyStack()

LinkQueue Q = GetEmptyQueue()

// SequenceStack myStack

char x,d

int i = 0,flag

char str[MaxStackSize]

    

    printf("输入字符串:")

scanf("%s",str)

while(str[i]) {

StackPush(head,str[i])

QueueAppend(Q,str[i])

++i

}

while(StackPop(head,&x) && QueueDelete(Q,&d) && flag)

flag = (x == d)

if(flag) printf("是回文!\n")

else printf("不是回文!\n")

return 0  

}