我如何在c中使用enqueue函数对列表进行排序?

我的列表 :

list= C,3 -->C,5,---> A,7 --> A,5 --> G,2--> C,11 -->A,4 

我的输出:

 output= C,5 C,11 G,2 A,7 A,4 A,5 C,3 

但是有一些错误。 我在答案中编写代码,然后编写charcmp函数并用strcmp替换为charcmp。 它有时会发挥作用。 但通常第一个元素是在错误的地方。我的排序​​代码喜欢答案的代码

以下代码应该完成这项工作。 它将按年龄顺序增加到列表中:

 #include  #include  #include  typedef struct _node_t { int age; char *name; struct _node_t *nextPtr; } node_t; node_t *node; void enqueue(char *name, int age) { node_t *p, *prev, *n; /* if empty list create first element and return */ if (node == NULL) { node = (node_t *)malloc(sizeof(node_t)); node->nextPtr = NULL; node->name = (char *)malloc(strlen(name) + 1); strcpy(node->name, name); node->age = age; return; } p = prev = node; /* search last element or element with superior age value */ while((p->nextPtr != NULL) && strcmp(p->name, name) < 0) { prev = p; p = p->nextPtr; } if (strcmp(p->name, name) == 0) { while((p->nextPtr != NULL) && p->age < age) { prev = p; p = p->nextPtr; } } /* create the new element and store the data */ n = (node_t *)malloc(sizeof(node_t)); n->name = (char *)malloc(strlen(name) + 1); strcpy(n->name, name); n->age = age; /* insert the new element */ if ((strcmp(p->name, name) < 0) || (strcmp(p->name, name) == 0 && p->age < age)) { n->nextPtr = p->nextPtr; p->nextPtr = n; } else { n->nextPtr = p; if (prev->nextPtr == p) { prev->nextPtr = n; } else if (node == p) { node = n; } } } void printNodes() { node_t *p; p = node; while(p != NULL) { printf("%s, %d\n", p->name, p->age); p = p->nextPtr; } } int main(int argc, char **argv) { node = NULL; enqueue("Kill", 15); enqueue("Bill", 2); enqueue("Kill", 7); printNodes(); return 0; } 

所以添加以下内容:

 enqueue("Kill", 15); enqueue("Bill", 2); enqueue("Kill", 7); 

将它存储在printNodes()打印的顺序中:

 Bill, 2 Kill, 7 Kill, 15 

更新:

添加名称排序然后年龄。

试试这个:-

 #include #include /*Queue has five properties. capacity stands for the maximum number of elements Queue can hold. Size stands for the current size of the Queue and elements is the array of elements. front is the index of first element (the index at which we remove the element) and rear is the index of last element (the index at which we insert the element) */ typedef struct Queue { int capacity; int size; int front; int rear; int *elements; }Queue; /* crateQueue function takes argument the maximum number of elements the Queue can hold, creates a Queue according to it and returns a pointer to the Queue. */ Queue * createQueue(int maxElements) { /* Create a Queue */ Queue *Q; Q = (Queue *)malloc(sizeof(Queue)); /* Initialise its properties */ Q->elements = (int *)malloc(sizeof(int)*maxElements); Q->size = 0; Q->capacity = maxElements; Q->front = 0; Q->rear = -1; /* Return the pointer */ return Q; } void Dequeue(Queue *Q) { /* If Queue size is zero then it is empty. So we cannot pop */ if(Q->size==0) { printf("Queue is Empty\n"); return; } /* Removing an element is equivalent to incrementing index of front by one */ else { Q->size--; Q->front++; /* As we fill elements in circular fashion */ if(Q->front==Q->capacity) { Q->front=0; } } return; } int front(Queue *Q) { if(Q->size==0) { printf("Queue is Empty\n"); exit(0); } /* Return the element which is at the front*/ return Q->elements[Q->front]; } void Enqueue(Queue *Q,int element) { /* If the Queue is full, we cannot push an element into it as there is no space for it.*/ if(Q->size == Q->capacity) { printf("Queue is Full\n"); } else { Q->size++; Q->rear = Q->rear + 1; /* As we fill the queue in circular fashion */ if(Q->rear == Q->capacity) { Q->rear = 0; } /* Insert the element in its rear side */ Q->elements[Q->rear] = element; } return; } int main() { Queue *Q = createQueue(5); Enqueue(Q,1); Enqueue(Q,2); Enqueue(Q,3); Enqueue(Q,4); printf("Front element is %d\n",front(Q)); Enqueue(Q,5); Dequeue(Q); Enqueue(Q,6); printf("Front element is %d\n",front(Q)); } 

使用双指针的解决方案可能是最好的。 您在技术上实现优先级队列,而不是列表。 您可以通过保留已排序的链接列表来实现它。 优先级队列上的出列操作将从列表中删除第一个项目。

由于这是一项家庭作业,我为你设置了但你需要完成剩下的工作,这样你才能真正学到重要的东西。

在这里退出条件 – >这是您要在何时找到要插入的项目时检查的位置。 写下这个条件,以便在发生这种情况时循环退出。

编辑^^ – >当我说出你要插入的项目后,我的意思是你当前正在看的项目是第一个比temp更“大”的项目。 queue将指向该项的下一个指针。 我还在代码中添加了一行,它将使set temp的下一个指向该项。 当你更新*queue你说你想让前一个项目上的下一个指针指向*temp

在这里更新指针 – >这是更新queue以指向列表/队列中下一个人的下一个指针的位置。

 typedef struct Person { char name[20]; int age; struct Person *next; } Person; void enqueue(Person **queue, char *name, int age) { Person *temp = calloc(1, sizeof(Person)); strncpy(temp->name, name, 20); temp->age = age; while (*queue && "EXIT CONDITION HERE") //update the pointer here; temp->next = *queue; //forgot this line originally. You need it!! *queue = temp; //.this sets the next pointer of last item or head to temp } ... Person *queue = NULL; enqueue(&queue, "Bob", 10); enqueue(&queue, "John", 5); Then print the list to check that enqueue works correctly.