```#include "stdafx.h"
#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)
總是報錯,訪問位置沖突。怎麼解決?
http://www.cnblogs.com/renyuan/archive/2013/05/21/3091506.html
參考這個全面的去對比看看吧。。