【发布时间】:2018-06-16 09:35:03
【问题描述】:
上周我参加了数据结构期末考试。我正在尝试解决考试中的一个问题。我想出了一个解决方案,但我编写的代码在我的计算机上不起作用。我可能做错了什么。
问题是这样的:给出了 2 个队列,我们应该从这些队列中取出元素并将它们排序到一个链表中。
我想出了一个主意。将有一个函数以 2 个队列和一个链表的根作为参数,它将使每个队列出队,直到没有任何剩余时才开始使另一个队列出队。链表有一个添加功能,按排序添加。
这是出列并添加到链表的函数 `
void sortedlist(queue *q1, queue *q2, node *root) {
while (q1->counter != 0) {
sortedInsert(root, dequeue(q1));
}
while (q2->counter != 0) {
sortedInsert(root, dequeue(q2));
}
}
`
这是将元素按排序添加到链表的功能。
node* sortedInsert(node *root, int x) {
if (root == NULL) {
root = (node *)malloc(sizeof(node*));
root->x = x;
root->next = NULL;
return root;
}
if (x< root->x) {
node *temp = (node *)malloc(sizeof(node));
temp->next = root;
temp->x = x;
return temp;
}
node *iter = root;
while (iter->next != NULL && iter->next->x < x) {
iter = iter->next;
}
node *temp = (node *)malloc(sizeof(node));
temp->x = x;
temp->next = iter->next;
iter->next = temp;
return root;
}
我将在这里编写所有代码。也许有些人可以告诉我我做错了什么。提前感谢您的考虑。
#include <stdio.h>
#include <stdlib.h>
#define MAX 100
//there are 2 queues, and one empty linked list
//take elements from queues and sort it on linked list
struct q {
int queue[MAX];
int front;
int rear;
int counter;
};
struct node {
int x;
struct node *next;
};
typedef struct node node;
typedef struct q queue;
void enqueue(queue *queue, int x) {
if (!(queue->counter == MAX)) {
queue->rear++;
queue->counter++;
if (queue->rear == MAX)
queue->rear = 0;
queue->queue[queue->rear] = x;
}
}
int dequeue(queue *queue) {
if (!(queue->counter == 0)) {
int x = queue->queue[queue->front];
queue->counter--;
queue->front++;
if (queue->front == MAX)
queue->front = 0;
return x;
}
else
return - 1;
}
node* sortedInsert(node *root, int x) {
if (root == NULL) {
root = (node *)malloc(sizeof(node*));
root->x = x;
root->next = NULL;
return root;
}
if (x< root->x) {
node *temp = (node *)malloc(sizeof(node));
temp->next = root;
temp->x = x;
return temp;
}
node *iter = root;
while (iter->next != NULL && iter->next->x < x) {
iter = iter->next;
}
node *temp = (node *)malloc(sizeof(node));
temp->x = x;
temp->next = iter->next;
iter->next = temp;
return root;
}
void printlist(node *root) {
while (root != NULL) {
printf("%d \n", root->x);
root = root->next;
}
}
void initialize(queue *queue) {
queue->counter = 0;
queue->front = 0;
queue->rear = -1;
}
void sortedlist(queue *q1, queue *q2, node *root) {
while (q1->counter != 0) {
sortedInsert(root, dequeue(q1));
}
while (q2->counter != 0) {
sortedInsert(root, dequeue(q2));
}
}
int main(void) {
queue *q1 = NULL, *q2 = NULL;
node *root = NULL;
initialize(q1);
initialize(q2);
enqueue(q1, 10);
enqueue(q1, 20);
enqueue(q1, 30);
enqueue(q1, 40);
enqueue(q1, 50);
enqueue(q1, 60);
enqueue(q1, 70);
enqueue(q2, 15);
enqueue(q2, 25);
enqueue(q2, 35);
enqueue(q2, 45);
enqueue(q2, 55);
enqueue(q2, 65);
enqueue(q2, 75);
enqueue(q2, 85);
enqueue(q2, 95);
enqueue(q2, 105);
enqueue(q2, 115);
sortedlist(q1, q2, root);
printlist(root);
}
【问题讨论】:
-
只需使用
gdb突出显示您遇到问题的步骤 -
你从不使用返回值,看起来是主要问题
-
不说怎么出错,很难找到错误。
-
我很抱歉,我是 StackOverflow 的超级新手。我将使用 gdb 并更具体地说明我的代码中的问题。我发这个的时候是凌晨两点。谢谢指出!
标签: c data-structures linked-list queue