Catoaim
2016-12-20 07:27
采纳率: 33.3%
浏览 907
已采纳

C语言,一个语句问题。

```#include "stdafx.h"

define MAXNUM 70

define FALSE 0

define TRUE 1

#include "stdio.h"
#include "stdlib.h"
#include "string.h"
typedef struct Qnode {char data[MAXNUM]; struct Qnode next;
}Qnodetype;
/
定义队列的结点*/
typedef struct
{
Qnodetype front;/头指针*/
Qnodetype rear; /尾指针*/
int number;/*短信数量*/
}Lqueue;
int initLqueue(Lqueue **q) {
/*创建一个空链队列q*/

if (((*q)->front = (Qnodetype*)malloc(sizeof(Qnodetype))) == NULL)
    return FALSE;
(*q)->rear = (*q)->front;
strcpy((*q)->front->data, "head"); 
(*q)->front->next = NULL;
(*q)->number = 0; 
return TRUE;

}
int LInQueue(Lqueue q, char x[])
{
/
将元素x插入到链队列q中,作为q的新队尾*/

Qnodetype *p;

if ((p = (Qnodetype*)malloc(sizeof(Qnodetype))) == NULL) 

return FALSE;

strcpy(p->data, x);

p->next = NULL;   /*置新结点的指针为空*/

q->rear->next = p;   /*将链队列中最后一个结点的指针指向新结点*/ 


q->rear = p;  /*将队尾指向新结点*/

return TRUE;
}

char * LOutQueue(Lqueue *q){

/*若链队列q不为空,则删除队头元素,返回其元素值*/ 
char  x[MAXNUM]; Qnodetype *p;

    if (q->front->next == NULL) return NULL;  /*空队列*/

    p = q->front->next;            /*取队头*/

q->front->next = p->next;           /*删除队头结点*/

if (p->next == NULL) q->rear = q->front;strcpy(x, p->data); 

    free(p); 
        return x;

}
void get(Lqueue q, char x[]) { /接受短信*/

int n;
if (q->number == 20)
{
    LOutQueue(q);q->number--;
} LInQueue(q, x); q->number++;

}
void deleteall(Lqueue q) { /删除所有短信*/
while (q->front != q->rear)

LOutQueue(q);
q->number = 0;
}
void deleteone(Lqueue q, int n) {/删除第n条短信*/
Lqueue p;Qnodetype *s;
int i;
p = q;
i = 1;
while (i {
p->front = p->front->next; i = i + 1;
}
s = p->front->next;
p->front->next = p->front->next->next; free(s);
q->number--;
}
void displayall(Lqueue *q) {/
显示所有短信*/
Lqueue p;char x[MAXNUM]; p = q;
while (p->front != q->rear) {
p->front = p->front->next;
printf("%s\n", p->front->data);
} printf("\n");
}
void displayone(Lqueue *q, int n) {/
显示第n条短信*/ Lqueue *p;Qnodetype *s; int i; p = q;i = 1; while (i {
p->front = p->front->next; i = i + 1;
}
s = p->front->next;
printf("%s\n", s->data);
}
void main()
{
Lqueue *Lp;
int i;
Qnodetype *headnode;
char command,ch[MAXNUM];
initLqueue(&Lp);
headnode = Lp->front;
while (1)
{
printf("Get information(%d),please enter R\n", Lp->number);
printf("Display one information(%d),please enter L\n", Lp->number);
printf("Display all information(%d),please enter A\n", Lp->number);
printf("Delete one information(%d),please enter D\n", Lp->number);
printf("Delete all information(%d),please enter U\n", Lp->number);
printf("Quit,please enter Q\n");
printf("please input command:");
scanf("%c", &command);
switch (command) {
case 'r'://case 'R': get(ch);Lp->front = headnode;get(Lp, ch);break;
case 'l':case 'L':printf("enter No.:"),
scanf("%d", &i);
Lp->front = headnode;displayone(Lp, i);
break;
case 'a':case 'A':Lp->front = headnode;displayall(Lp)
;break;
case 'd':case 'D':printf("enter No.:"), scanf("%d", &i);
Lp->front = headnode;deleteone(Lp, i);
break;
case 'u':case 'U':Lp->front = headnode;deleteall(Lp);break;
case 'q':case 'Q':printf("quit!");
}
if (command == 'q' || command == 'Q')
break;
}
}



initLqueue方法里
if (((*q)->front = (Qnodetype*)malloc(sizeof(Qnodetype))) == NULL)

总是报错,访问位置冲突。怎么解决?
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • shen_wei 2016-12-20 08:06
    已采纳

    http://www.cnblogs.com/renyuan/archive/2013/05/21/3091506.html
    参考这个全面的去对比看看吧。。

    打赏 评论
  • 鱼弦 2016-12-20 07:45

    typedef struct
    {
    Qnodetype front;/头指针*/
    Qnodetype rear; /尾指针*/
    int number;/*短信数量*/
    }Lqueue;

    结构里面front是对象,并不是指针, (Qnodetype*)malloc(sizeof(Qnodetype)) 这里申请的内存不符合

    打赏 评论
  • lx624909677 2016-12-20 07:49

    你的font是在栈上开辟的空间,不需要去malloc空间

    打赏 评论

相关推荐 更多相似问题