问题
My list :
list= C,3 -->C,5,---> A,7 --> A,5 --> G,2--> C,11 -->A,4
my outuput :
output= C,5 C,11 G,2 A,7 A,4 A,5 C,3
but there is some mistakes. I write my code like in answers and I write charcmp function and replace with strcmp with charcmp. It works sometime corrcectly. But usually first element is in the wrong place .My sorting code likes answer's code
回答1:
The following code should do the work. It will add to the list in increasing age order:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
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;
}
So the following adding:
enqueue("Kill", 15);
enqueue("Bill", 2);
enqueue("Kill", 7);
Will store it in the order printed by printNodes()
:
Bill, 2
Kill, 7
Kill, 15
UPDATED:
Added name sorting then age.
回答2:
try this:-
#include<stdio.h>
#include<stdlib.h>
/*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));
}
回答3:
A solution using double pointers is probably the best. You are implementing a priority queue technically, not a list. You implement it by keeping a linked list that is sorted. The dequeue operation on a priority queue will remove the first item from the list.
As this is a homework assignment I set it up for you but you need to complete the rest so you can actually learn the important stuff.
EXIT CONDITION HERE -> This is where you want to check when you have found the item where you will insert after. Write this condition so that the loop exits when this occurs.
EDIT ^^ -> When I say the item where you will insert after what I mean is that the item you currently are looking at is the first item that is "greater" than temp. queue
will be pointing to the next pointer to that item. I also added a line in the code that will make set temp
's next to point to that item. When you update *queue
you are saying that you want to make the next pointer on the previous item point to *temp
.
update the pointer here -> This is where you update queue
to point to the next pointer of the next person in the list/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.
来源:https://stackoverflow.com/questions/23824948/how-i-sort-list-with-enqueue-function-in-c