|
该列队链接能正确的编译运行完,但在运行完之后出现一个"段错误",为什么?
- #include <stdio.h>
- typedef struct linked_queue
- {
- int data;
- struct linked_queue *next;
- } LqueueTp;
- typedef struct queueptr
- {
- LqueueTp *front, *rear;
- } QueptrTp;
- int main()
- {
- QueptrTp *lq;
- int input;
- int i = 0;
- int outnum;
- InitQueue(&lq);
- printf("请输入一些数字:");
- scanf("%d", &input);
- while (input != 0)
- {
- EnQueue(&lq, input);
- scanf("%d", &input);
- }
- GetHead(&lq, &input);
- printf("当前链接列队的队头为:%d", input);
- printf("\n请输入要出队的个数:");
- scanf("%d", &outnum);
- printf("依此出队的数为:");
- while (i < outnum)
- {
- OutQueue(&lq, &input);
- printf("%d ", input);
- i++;
- }
- printf("\n当前列队排列:");
- while (! EmptyQueue(&lq))
- {
- OutQueue(&lq, &input);
- printf("%d ", input);
- }
- printf("\n\n");
- }
- int InitQueue (QueptrTp **lq)
- {
- LqueueTp *p;
- p = (LqueueTp *) malloc (sizeof(LqueueTp));
- (*lq) -> front = p;
- (*lq) -> rear = p;
- ((*lq) -> front) -> next = NULL;
- }
- int EnQueue (QueptrTp **lq, int x)
- {
- LqueueTp *p;
- p = (LqueueTp *) malloc (sizeof(LqueueTp));
- p -> data = x;
- p -> next = NULL;
- ((*lq) -> rear) -> next = p;
- (*lq) -> rear = p;
- }
- int OutQueue (QueptrTp **lq, int *x)
- {
- LqueueTp *s;
- if ((*lq) -> front == (*lq) -> rear)
- {
- printf("队空!\n");
- return 0;
- }
- else
- {
- s = ((*lq) -> front) -> next;
- *x = s -> data;
- ((*lq) -> front) -> next = s -> next;
- if (s -> next == NULL)
- (*lq) -> rear = (*lq) -> front;
- free(s);
- return 1;
- }
- }
- int EmptyQueue (QueptrTp **lq)
- {
- if (((*lq) -> rear) == ((*lq) -> front))
- return 1;
- else
- return 0;
- }
- int GetHead (QueptrTp **lq, int *x)
- {
- LqueueTp *p;
- if ((*lq) -> rear == (*lq) -> front)
- return 0;
- else
- {
- p = ((*lq) -> front ) -> next;
- *x = p -> data;
- return 1;
- }
- }
复制代码 |
|