C: Queue and Memory Limit Excedeed - c

I'm a C beginner and decided to participate in a small online contest in order to practice.
In the current problem I'm asked to write a queue with a struct that responds to the commands PushBack and PopFront.
The input consists of
A number n (n <= 1000000) indicating the number of commands inputs.
n lines. Each line consists of two integer numbers a and b:
a is 2 for executing PopFront, in which case b is the expected popped value.
a is 3 for PushBack, in which case b is the value to be enqueued.
If we try to pop from an empty queue then the value returned is -1.
The task is to print YES or NO after executing the last command if the value returned by any PushBack during the program execution coincide or not with the expected value.
I implemented a version of this, but after submitting my answer the online judge gives Maximum-Limit-Excedeed (in the last test out of 27).
I was reading about it and this issue may be related to some of these:
Using an array or data structure too big.
There is an infinite (or too big) recursion in the program.
An incorrect usage of pointers (diagnosed as MLE).
I'm not sure what is the problem. It seems to me that in some of the tests the number of addition of nodes is way greater than that of deletions (which means that 1. takes place in my code) which, in turn, causes the while loop in EmptyQueue to be too big (2. also takes place). I'm not able to spot whether there is an incorrect usage of pointers.
My questions are:
What am I'm doing wrong here?
What should I do to fix this?
Code:
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
//===================
//Definitions:
typedef int Item;
typedef struct node
{
Item item;
struct node * next;
} Node;
typedef struct queue
{
Node * front;
Node * rear;
long counter;
} Queue;
//===================
//Function Prototypes:
void InitializeQueue(Queue * pq);
bool PushBack(Queue * pq, Item item);
int PopFront(Queue * pq);
void EmptyQueue(Queue * pq);
int main(void)
{
Queue line;
long n, i;
int command, expected, received;
bool check = true;
scanf("%ld", &n);
InitializeQueue(&line);
i = 0;
while (i < n)
{
scanf("%d %d", &command, &expected);
switch (command)
{
case 2:
received = PopFront(&line);
if (received != expected)
check = false;
break;
case 3:
PushBack(&line, expected);
break;
}
i++;
}
if (check == true)
printf("YES\n");
else
printf("NO\n");
// free memory used by all nodes
EmptyQueue(&line);
return 0;
}
void InitializeQueue(Queue * pq)
{
pq->front = NULL;
pq->rear = NULL;
pq->counter = 0;
}
bool PushBack(Queue * pq, Item item)
{
Node * pnode;
//Create node
pnode = (Node *)malloc(sizeof(Node));
if (pnode == NULL)
{
fputs("Impossible to allocate memory", stderr);
return false;
}
else
{
pnode->item = item;
pnode->next = NULL;
}
//Connect to Queue
if (pq->front == NULL)
{
pq->front = pnode;
pq->rear = pnode;
}
else
{
pq->rear->next = pnode;
pq->rear = pnode;
}
pq->counter++;
return true;
}
int PopFront(Queue * pq)
{
int popped;
Node * temp;
temp = pq->front;
if (pq->counter == 0)
return -1;
else
{
popped = pq->front->item;
pq->front = pq->front->next;
free(temp);
pq->counter--;
return popped;
}
}
void EmptyQueue(Queue * pq)
{
int dummy;
while (pq->counter != 0)
dummy = PopFront(pq);
}
Thanks.

I don't think there's actually anything wrong with that code functionally, though it could do with some formatting improvements :-)
I will mention one thing:
The task is to check whether the returned value after executing PopFront coincides with the expected one. If so, then print YES. Print NO, otherwise.
I would read this as a requirement on each PopFront. You appear to be storing the fault condition and only printing YES or NO once at the end.
I'd suggest fixing that as a start and see what the online judge comes back with.
This all ignores the fact that it's actually rather difficult to debug code unless you can reproduce the problem. If you can't get the data set from the online contest, it may be worth generating your own (large) one to see if you can get you code to fail.
Once you have a repeatable failure, debugging becomes massively easier.
Although it's unlikely, you may (as mch points out in a comment) be running afoul of limited memory. I consider this unlikely as your own comments indicate only 5meg of space is being used at the end, which is not onerous. However, if that is the case, it's probably due to the fact that every single integer has the overhead of a pointer carried along with it.
If you wanted to investigate that avenue, you could slightly adjust the structures as follows (getting rid of the unnecessary counter as well):
#define ITEMS_PER_NODE 1000
typedef struct node {
Item item[ITEMS_PER_NODE]; // array of items.
int startIndex; // start index (one to pop from).
int nextIndex; // next index (one to push at).
struct node *next; // next node.
} Node;
typedef struct queue {
Node *front; // first multi-item node.
Node *rear; // last multi-item node.
} Queue;
The idea is to store many items per node so that the overhead of the next pointer is greatly reduced (one pointer per thousand items rather than one per item).
The code for queue manipulation would then become slightly more complex but still understandable. First off, a helper function for creating a new node, ready for adding data to:
// Helper to allocate a new node and prep it for appending.
// Returns node or NULL (and prints error) if out of memory.
Node *GetNewNode(void) {
Node *pnode = malloc (sizeof(Node));
if (pnode == NULL)
fputs ("Impossible to allocate memory", stderr);
else
pnode->startIndex = pnode->nextIndex = 0;
return pnode;
}
Next, the mostly unchanged queue initialisation:
void InitializeQueue (Queue *pq) {
pq->front = pq->rear = NULL;
}
The pushback is slightly more complex in that it first adds a new multi-item node if the queue is empty or current last node has reached the end. Whether that happens or not, an item is added to the final node:
bool PushBack (Queue *pq, Item item) {
// Default to adding to rear node (assuming space for now).
Node *pnode = pq->rear;
// Make sure queue has space at end for new item.
if (pq->front == NULL) {
// Handle empty queue first, add single node.
if ((pnode = GetNewNode()) == NULL)
return false;
pq->front = pq->rear = pnode;
} else if (pq->rear->nextItem == ITEMS_PER_NODE) {
// Handle new node needed in non-empty queue, add to rear of queue.
if ((pnode = GetNewNode()) == NULL)
return false;
pq->rear->next = pnode;
pq->rear = pnode;
}
// Guaranteed space in (possibly new) rear node now, just add item.
pq->rear->item[pq->rear->nextIndex++] = item;
}
Popping is also a bit more complex - it gets the value to return then deletes the first node if it's now exhausted. That may also entail clearing the queue if the node it deletes was the only one:
int PopFront (Queue * pq) {
// Capture empty queue.
if (pq->first == NULL)
return -1;
// Get value to pop.
Node *currFront = pq->front;
int valuePopped = currFront->item[currFront->startIndex++];
// Detect current node now empty, delete it.
if (currFront->startItem == currFront->endIndex) {
// Detect last node in queue, just free and empty entire queue.
if (currFront == pq->rear) {
free (currFront);
pq->front = pq->rear = NULL;
} else {
// Otherwise remove front node, leaving others.
pq->front = currFront->next;
free (currFront);
}
}
// Regardless of queue manipulation, return popped value.
return valuePopped;
}
Emptying the queue is largely unchanged other than the fact we clear nodes rather than items:
void EmptyQueue (Queue * pq) {
// Can empty node at a time rather than item at a time.
while (pq->front != NULL) {
Node *currentFront = pq->front;
pq->front = pq->front->next;
free (currentFront);
}
}

I think is better to use a more simple approach like that in the code I post here.
The code in the following lines doesn't match the input/output required by the contest, but contains a functional and simple approach to solve the problem: A simple stack manager! (if I correctly understood).
#include <stdio.h>
#include <malloc.h>
int * stack;
int * base;
int cnt;
/* To emulate input file */
struct stFile {
int n;
struct stCmd {
int a;
int b;
} cmd[200]; // 200 is an arbitrary value.
} fdata = {
20,
{
{2,0},
{2,0},
{2,0},
{3,35},
{2,0},
{3,4},
{2,0},
{2,0},
{2,0},
{3,12},
{3,15},
{3,8},{3,18},
{2,0},
{2,0},
{3,111},
{2,0},
{2,0},
{2,0},
{2,0},
{3,8},{3,18},{3,8},{3,18},{3,8},{3,18},{3,8},{3,18},{3,8},{3,18},
{3,11},{3,13},{3,11},{3,11},{3,11},{3,11},{3,11},{3,11},
{3,11},{3,13},{3,11},{3,11},{3,11},{3,11},{3,11},{3,11},
{2,0},
{2,0},
{2,0},
{2,0},
{2,0},
{2,0},
{2,0},
{2,0},
{0,0}
}
};
int push(int item)
{
if (cnt) {
*stack = item;
stack++;
cnt--;
return 0;
} else {
return 1;
}
}
int pop(int *empty)
{
if (stack!=base) {
stack--;
cnt++;
if (empty)
*empty = 0;
} else {
if (empty)
*empty = 1;
}
return *stack;
}
int main(void)
{
int i=0,e=0;
cnt = fdata.n;
base = stack = malloc(cnt*sizeof(int));
if (!base) {
puts("Not enough memory!");
return 1;
}
while(fdata.cmd[i].a!=0) {
switch(fdata.cmd[i].a) {
case 2:
printf("popping ...: %d ",pop(&e));
printf("empty: %d\n",e);
break;
case 3:
e = push(fdata.cmd[i].b);
printf("pushing ...: %d %s\n",fdata.cmd[i].b,(e)?"not pushed":"pushed");
break;
default:
break;
};
i++;
}
if (base)
free(base);
return 0;
}

Related

C programming linked list and queue with an array [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I need to enqueue a whole struct customer in the queue - I think. This is my homework. I am not asking for an answer. I need a mentor to explain what I am missing and what I have right per the assignment. I am very new. This is my second program. see below... thanks btw.
A store is having customers queue in several lines. After the cashier finishes helping a customer, he will survey all of the lines that are currently queued. Of all of the customers at the front of those lines, he’ll take the customer who has the fewest number of items. If there are two customers with the same number of items, he’ll take the customer who comes from the smaller line number. The lines are numbered 1 through 12. It’s possible that some of these lines will be empty, in which case these lines are ignored. The number of seconds the store clerk takes to check out a customer is 30 plus 5 times the number of items. Thus, if a customer has 8 times, the clerk would check her out in 30 + 8*5 = 70 seconds.
The Problem
You will write a program that reads in information about customers: which line they go to the back of (1 through 12), at what time (in seconds) they enter that line, and the number of items they have, and determines at what time each customer will check out.
Sample Input:
2
5
10 1 STEVEN 12
12 6 AHMAD 8
13 1 JENNY 40
22 6 JERMAINE 39
100000 12 AMALIA 53
6
100 1 A 100
200 2 B 99
300 3 C 98
400 4 D 97
500 5 E 96
600 6 F 95
Sample Output:
STEVEN from line 1 checks out at time 100.
AHMAD from line 6 checks out at time 170.
JERMAINE from line 6 checks out at time 395.
JENNY from line 1 checks out at time 625.
AMALIA from line 12 checks out at time 100295.
A from line 1 checks out at time 630.
F from line 6 checks out at time 1135.
E from line 5 checks out at time 1645.
D from line 4 checks out at time 2160.
C from line 3 checks out at time 2680.
B from line 2 checks out at time 3205.
Implementation Restrictions:
You must create a struct that stores information about a customer (name, number of items, line number, time entering line). Note that the storage of the line number is redundant, but is designed to ease implementation.
You must create a node struct for a linked list of customers. This struct should have a pointer to a customer struct, and a pointer to a node struct.
You must create a struct to store a queue of customers. This struct should have two pointers – one to the front of the queue and one to the back.
You must implement all of the lines that form as an array of size 12 (stored as a constant) of queues.
You must dynamically allocate memory as appropriate for linked lists.
Your queue must support the following operations:
a. Enqueue
b. Dequeue
c. Return the front of the queue WITHOUT dequeing d. Empty (returns 1 if the queue is empty, 0 if it is not)
You must free memory appropriately. Namely, when you dequeue, you’ll free memory for a node, but you will NOT free memory for the customer. You will free this memory a bit later right after you calculate when that customer will finish checking out.
Due to the nature of the problem, when you process the input, you can add everyone into their appropriate lines right at the beginning, before checking anyone out.
This wouldn’t work in all simulations (some of which you have to do in time order), but because there is ONLY one check out line, you can get away with it. The only thing you have to be cognizant about is that when you select a line, if the current time is, 100 for example, and three lines have customers who arrived before time 100 and the other lines have customers in the front who arrived AFTER time 100, you have to ignore the customers in those lines who arrived after time 100. In the case that all the lines have customers who arrived after time 100, you would take the line which has a customer who arrived first. You are guaranteed no ties for arrival time so this would be unique.
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#define TRUE 1
#define FALSE 0
typedef char customerName[9];
typedef struct node
{
int data;
struct node *next;
}node;
typedef struct queue
{
int size;
int front;
int back;
int *array;
unsigned capacity;
}queue;
typedef struct customer
{
customerName name;//1-9 upper case letters
int lineNumber;
int time;
int numberItems;
} customer;
int isEmpty(queue *q);
void initialize(queue *q);
void initialize(queue *q)
{
q->size = 0;
q->front = -1;
q->back = -1;
}
int isEmpty(queue *q){
return (q->size == 0);//returns 1 if empty or 0 if false
}
int isFull(queue *q)
{ return (q->size == q->capacity); }
void enqueue(queue *q, int item)
{
if (isFull(q))
return;
q->back = (q->back + 1)%q->capacity;
q->array[q->back] = item;
q->size = q->size + 1;
}
int dequeue(queue *q)
{
if (isEmpty(q))
return 0;
int item = q->array[q->front];
q->front = (q->front + 1)%q->capacity;
q->size = q->size - 1;
return item;
}
int front(queue* q){
if(isEmpty(q)){
return 0;
}
return q->array[q->front];
}
int main(int argc, const char * argv[]) {
int testCases = 0;
scanf("%d", &testCases);
if(testCases > 0 && testCases <= 25){//shortcircuiting???
while (testCases--){
queue *q;
q = malloc(sizeof(queue));
initialize(q);// starting new queue
int numCustomers;
scanf("%d", &numCustomers);
if(numCustomers < 0 || numCustomers > 11){
return 0;
}
struct customer newCustomer[1];
for ( int i = 0; i < numCustomers; i++){
scanf("%d", &newCustomer[i].time);
scanf("%d", &newCustomer[i].lineNumber);
scanf("%s", newCustomer[i].name);
scanf("%d", &newCustomer[i].numberItems);
enqueue(q, newCustomer[i].time);
enqueue(q, newCustomer[i].lineNumber);
}
for ( int i = 0; i < numCustomers; i++){
printf("%d %d %s %d\n", newCustomer[i].time, newCustomer[i].lineNumber, newCustomer[i].name, newCustomer[i].numberItems);
}
}
}
return 0;
}
Here is my latest code:
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#define TRUE 1
#define FALSE 0
#define NUMLINES 12
int currtime = 0;
typedef char customerName[9];
typedef struct node
{
int data;
struct customer* data;
struct node* next;
}node;
typedef struct queue
{
node* front;
node* back;
}queue;
typedef struct customer
{
customerName name;//1-9 upper case letters
int lineNumber;
int time;
int numberItems;
} customer;
typedef struct node *newNode;//define a node pointer
node createNode()
{
newNode temp;//declare node
temp = (newNode)malloc(sizeof(struct node));//allocate memory
temp->next = NULL;//next point to null
return *temp;// return the new node
}
struct queue* createQueue()
{
struct queue* q = (struct queue*)malloc(sizeof(struct queue));
q->front = q->back = NULL;
return q;
}
int isEmpty(queue *q){
return (q->back == NULL);//returns 1 if empty or 0 if false
}
void enqueue(queue *q, customer* data)
{
// Create a new LL node
struct node* temp = createNode(data);
// If queue is empty, then new node is front and back both
if (q->back == NULL) {
q->front = q->back = temp;
return;
}
// Add the new node at the end of queue and change back
q->back->next = temp;
q->back = temp;
}
void dequeue(queue *q)
{
if (q->front == NULL)
return;
// Store previous front and move front one node ahead
struct node* temp = q->front;
q->front = q->front->next;
// If front becomes NULL, then change rear also as NULL
if (q->front == NULL)
q->back = NULL;
free(temp);
}
int front(queue* q){
if(isEmpty(q)){
return 0;
}
return q->front;
}
int main(int argc, const char * argv[]) {
int testCases = 0;
scanf("%d", &testCases);
if(testCases > 0 && testCases <= 25){//shortcircuiting???
while (testCases--){
queue *q;
q = malloc(sizeof(queue));
initialize(q);// starting new queue
int numCustomers;
scanf("%d", &numCustomers);
if(numCustomers < 0 || numCustomers > 11){
return 0;
}
struct customer newCustomer[11];
for ( int i = 0; i < numCustomers; i++){
scanf("%d", &newCustomer[i].time);
scanf("%d", &newCustomer[i].lineNumber);
scanf("%s", newCustomer[i].name);
scanf("%d", &newCustomer[i].numberItems);
enqueue(q, newCustomer[i].time);
enqueue(q, newCustomer[i].lineNumber);
}
for ( int i = 0; i < numCustomers; i++){
printf("%d %d %s %d\n", newCustomer[i].time, newCustomer[i].lineNumber, newCustomer[i].name, newCustomer[i].numberItems);
}
}
}
return 0;
}
int main(int argc, const char * argv[]) {
int testCases = 0;
scanf("%d", &testCases);
if(testCases > 0 && testCases <= 25){//shortcircuiting???
while (testCases--){
queue *q;
q = malloc(sizeof(queue));
qinit(q);// starting new queue
int numCustomers;
scanf("%d", &numCustomers);
if(numCustomers < 0 || numCustomers > 11){
return 0;
}
queue* customerArray = (queue*) malloc(sizeof(queue) * 12);
for ( int i = 0; i < numCustomers; i++){
customer* newCustomer = (customer*) malloc(sizeof(customer));
scanf("%d", &(newCustomer->time));
scanf("%d", &(newCustomer->lineNumber));
scanf("%s", newCustomer->name);
scanf("%d", &(newCustomer->numberItems));
enqueue(&customerArray[newCustomer->lineNumber - 1], newCustomer);
}
int totalTime = INT_MAX;
for(int i=0;i<12;i++)
{
customer* frontCustomer = qfront(&customerArray[i]);
if(totalTime < frontCustomer->time)
{
totalTime = frontCustomer->time;
}
free(frontCustomer);
}
while(numCustomers--) {
int customerToCheckOutLine = 0; int minNumberOfItems = INT_MAX;
for( int j=11 ; j>=0; j--){
customer* frontCustomer = qfront(&customerArray[j]);
if(frontCustomer->time <= totalTime)
{
if(frontCustomer->numberItems < minNumberOfItems)
{
customerToCheckOutLine = frontCustomer->lineNumber;
minNumberOfItems = frontCustomer->numberItems;
}
free(frontCustomer);
}
}
customer* customerToCheckOut = qfront(&customerArray[customerToCheckOutLine -1 ]);
totalTime += 30;
totalTime += (customerToCheckOut->numberItems) * 5;
dequeue(&customerArray[customerToCheckOutLine - 1]);
}
free(customerArray);
}
}
return 0;
}
Your queue related functions look okay.
But, there are some bugs and the code needs refactoring to handle more things.
Your queue element array should be of type node * instead of int *.
You need the newCustomer array to be of length numCustomers (i.e. not 1).
At present, you have a single queue. But, you need an array of queues, one for each line, so an array length of 12.
When you queue a customer, you must select the queue [from the array of queues] based on the line number the customer is getting on.
Here's a biggie:
You can't queue two separate entries, one being time and the other being lineNumber. Well, you can I guess, but it's messy.
The way I'd do it is to change the data element of the node struct to be a pointer to the customer record. That way, every bit of data you need to determine the servicing order is [already] in the customer struct.
Anyway, here's some refactored code.
Per your request, it's not a solution, but some sample code as a basis for implementing the above fixes/suggestions.
You still have to change the queue struct and all queue related functions (if you decide to change the struct as I've suggested).
And, you still have to implement the selection/ordering process. And, the cleanup/deallocation.
This won't compile as is, because it's a "suggestion", but here it is:
typedef char customerName[9];
typedef struct customer {
customerName name; // 1-9 upper case letters
int lineNumber;
int time;
int numberItems;
} customer;
typedef struct node {
#if 0
int data;
#else
customer *data;
#endif
struct node *next;
} node;
typedef struct queue {
int size;
int front;
int back;
#if 0
int *array;
#else
node *array;
#endif
unsigned capacity;
} queue;
#if 1
#define NUMLINES 12
queue queue_list[NUMLINES];
#endif
int isEmpty(queue *q);
void initialize(queue * q);
void
initialize(queue *q)
{
q->size = 0;
q->front = -1;
q->back = -1;
}
int
isEmpty(queue *q)
{
return (q->size == 0); // returns 1 if empty or 0 if false
}
int
isFull(queue *q)
{
return (q->size == q->capacity);
}
void
enqueue(queue *q, int item)
{
if (isFull(q))
return;
q->back = (q->back + 1) % q->capacity;
q->array[q->back] = item;
q->size = q->size + 1;
}
int
dequeue(queue *q)
{
if (isEmpty(q))
return 0;
int item = q->array[q->front];
q->front = (q->front + 1) % q->capacity;
q->size = q->size - 1;
return item;
}
int
front(queue *q)
{
if (isEmpty(q)) {
return 0;
}
return q->array[q->front];
}
int
main(int argc, const char *argv[])
{
queue *q;
int testCases = 0;
scanf(" %d", &testCases);
// NOTE/BUG: you need a separate queue for each line
for (int qidx = 0; qidx < NUMLINES; ++qidx) {
q = &queue_list[qidx];
initialize(q);
}
// shortcircuiting???
if (testCases > 0 && testCases <= 25) {
while (testCases--) {
#if 0
queue *q;
#endif
#if 0
q = malloc(sizeof(queue));
initialize(q); // starting new queue
#endif
int numCustomers;
scanf(" %d", &numCustomers);
if (numCustomers < 0 || numCustomers > 11) {
return 0;
}
// NOTE/BUG: you need a larger array of customers
#if 0
struct customer newCustomer[1];
#else
struct customer newCustomer[numCustomers];
#endif
for (int i = 0; i < numCustomers; i++) {
customer *cust = &newCustomer[i];
scanf(" %d", &cust->time);
scanf(" %d", &cust->lineNumber);
scanf(" %s", cust->name);
scanf(" %d", &cust->numberItems);
// NOTE/BUG: customer must be queued to the queue for the
// line number they're on
// NOTE/BUG: queue a single entry for each customer
#if 0
enqueue(q, cust->time);
enqueue(q, cust->lineNumber);
#else
q = &queue_list[cust->lineNumber - 1];
enqueue(q,cust);
#endif
}
for (int i = 0; i < numCustomers; i++) {
printf("%d %d %s %d\n",
newCustomer[i].time,
newCustomer[i].lineNumber,
newCustomer[i].name,
newCustomer[i].numberItems);
}
}
}
return 0;
}
UPDATE:
woah, this is amaze balls. thank you so much. I think the assignment is confusing because he says the assignment is queues with linked list not an array but like you said it needs to be an array of queues. that would suggest multiple entries into a single line. so in a sense this is both an queues with linked lists and array?
You need an array of queues, indexed by linenumber. This is not to be confused with the q->array element within the queue, which is different.
I just completed a working and tested version [for myself, because I'm crazy that way :-)]. I had the same confusion.
The queue is a ring queue [with array]. If the array pointer is a customer *, I couldn't find a good way to use node. The only way to do it would be to convert queue into a linked list header struct. That would require queue to be restructured [along will all the access functions for it ;-)].
But, before doing that, if we keep the array/ring method, then initialize needs to take a capacity arg (called with initialize(q,numCustomer);). And, it needs to do (e.g.) q->array = realloc(q->array,sizeof(customer *) * capacity);
And, enqueue needs a fix: the increment of q->back needs to be moved after the q->array[q->back] = item;. Currently, there is an off by one gap.
To convert to a linked list:
The structs change a bit:
typedef struct node {
customer *data;
struct node *next;
} node;
typedef struct queue {
node *front;
} queue;
You have an isFull function. That's not part of the requirements. If we switch queue into a linked list header for a list of node, it isn't even relevant/meaningful since, now, there is no maximum [as there was with q->capacity].
Now, when doing enqueue(q,cust);, the function would malloc a new node and append it to the end of the linked list. And, setup of (e.g.) newnode->next and newnode->data = cust;
When doing dequeue, it pops off the front element (a node), does cust = curnode->data;, then free(curnode), and returns cust.
Based on my rereading the requirements, I believe that this is what's required, and actually makes sense of all of them.
In other words, you can implement a queue with just a linked list (vs. what you did which was a ring queue with array--which I've also done in the past).
the ordering process is part of the reason I am so confused. I don't know where to do this. The list is already ordered by time the customer went to check out. I just need to calculate the total cashier time.there is only one cashier so all the people are served in order of entering the queue.
This is a bit tricky. You have to have a curtime global to keep track of the current time. Initially, this has to be set to the minimum of all cust->time values.
As you loop through all queues [i.e. waiting lines] to look at the first customer on the line, you have to do the following:
You skip the queue if it's empty.
You skip any customer if they have an arrival date in the future (i.e. cust->time > curtime).
Now you can "select" the customer. You need the find "best" customer [for the next one to be serviced]. That is, the one with the smallest order. The 2nd criteria of: with a tie, the customer with the lowest line number wins is handled automatically if you only update the "best" value if (cust->numberItems < best->numberItems) because we're traversing the queue_list array from low to high.
Then, service the "best" customer (i.e. print the message). Then, increment curtime by the elapsed time it takes to service the customer.
Repeat this until all queues are empty.
But, there is one gotcha. It happens with AMALIA.
The arrival time for this customer is so far in the future that the customer selection will fail to find any "best" customer because amalia->time is greater than curtime. Thus, it seems like AMALIA is not ready to be serviced.
So, if the selection fails (i.e. best is null), we have to loop through all front customers [again] and set curtime to the minimum of all their arrival times.
Then, we rescan/reselect for "best". Now, AMALIA will be selected
UPDATE #2:
in your example I don't understand what the #elseif etc. what are those? other than the obvious, I have never seen this
The lines starting with #if are preprocessor statements [just like #include, #ifdef, #else, #endif
The preprocessor is used to include/exclude code at compile time (vs using a real if language statement). You can think of the preprocessor as a separate pass before compilation that performs the conditional actions before passing the resultant file to the compile stage.
In my examples, I'm using them to show old code [yours] vs. new code [mine]:
#if 0
// old code ...
x = 1;
#else
// new code ...
x = 2;
#endif
When compiled, the old code is excluded, the new code is included as if we had done:
// new code ...
x = 2;
This is a useful technique in general. It allows you to "comment out" code that you suspect might be a bug. Or, for debug printf code. Or, you have working code (the "old" code), but you want to try something possibly cleaner or more efficient (the "new" code). You can flip back and forth during development just by changing #if 0 to #if 1 or vice versa
You can see the intermediate file by doing:
cc -E -P -o myfile.i myfile.c
Later on, when your code is [fully ;-)] debugged, you could hand edit out the #if cases you don't need/want.
can you do that same kind of example but for the linked list only implementation? or is that asking too much? :) why is there only a *front and no back of the list?
For a simple singly linked list, we only truly need a head/front pointer.
We don't need size because the empty case is: q->front == NULL. size is primarily useful if we had to sort the linked list (it saves a pass on the list to get the number of elements).
Using q->back adds a slight degree of complexity. It's a speedup when appending to the queue/list. If we don't have it, we find the last element in the list by traversing it. If we have back, that is the last element.
A singly linked list is sufficient for your use case. But, just for completeness, I've added doubly linked list support. We add a prev element to node [the backwards link].
Below, I've coded up the linked list structs and functions. To allow you to see the various modes, each optional element is wrapped in preprocessor #if/#endif pairs to include/exclude code, based on setting the given option on/off
DLINK [doubly linked list] is off by default
To enable it on a one time basis, you could do:
cc -DDLINK=1 -c myfile.c
Likewise for other options.
Anyway, here's the queue code based on a linked list:
// customer record
typedef char customerName[9];
typedef struct customer {
customerName name; // 1-9 upper case letters
int lineNumber; // line number customer gets on
int time; // arrival time at line
int numberItems; // number of items customer has
} customer;
// 1=use queue q->back
// need this for _doubly_ linked list
// speedup for append to end of queue for singly linked
#ifndef QBACK
#define QBACK 1
#endif
// 1=use queue q->size
#ifndef QSIZE
#define QSIZE 1
#endif
// 1=use doubly linked list
#ifndef DLINK
#define DLINK 0
#endif
// force QBACK on if doing doubly linked list
#if DLINK
#undef QBACK
#define QBACK 1
#endif
// [singly linked] queue element
typedef customer *qitem;
typedef struct node {
qitem data; // pointer to actual data
struct node *next; // forward pointer to next item
#if DLINK
struct node *prev; // backward pointer to previous item
#endif
} node;
// queue definition (singly linked list)
// NOTE:
typedef struct queue {
node *front; // pointer to first node in list
#if QBACK
node *back; // pointer to last node in list
#endif
#if QSIZE
int size; // current number of elements in list
#endif
} queue;
// list of queues (indexed by lineNumber - 1)
#define NUMLINES 12
queue queue_list[NUMLINES];
// qinit -- initialize/reset queue
void
qinit(queue *q)
{
q->front = NULL;
#if QSIZE
q->size = 0;
#endif
#if QBACK
q->back = NULL;
#endif
}
// qempty -- returns 1 if empty or 0 if false
int
qempty(queue *q)
{
// NOTE: size really isn't needed
#if 0
return (q->size == 0);
#else
return (q->front == NULL);
#endif
}
// enqueue -- append element to end of queue
void
enqueue(queue *q, qitem data)
{
node *newnode;
node *prev;
newnode = malloc(sizeof(node));
newnode->next = NULL;
newnode->data = data;
// NOTE: this is the only place where back is a speedup
#if QBACK
prev = q->back;
#else
// what we have to do find the back of the queue with only a front pointer
prev = NULL;
for (node *cur = q->front; cur != NULL; cur = cur->next)
prev = cur;
#endif
// append to tail of list
if (prev != NULL)
prev->next = newnode;
// add to end of empty list
else
q->front = newnode;
#if DLINK
newnode->prev = prev;
#endif
#if QBACK
q->back = newnode;
#endif
#if QSIZE
q->size += 1;
#endif
}
// dequeue -- dequeue from the front of the queue
qitem
dequeue(queue *q)
{
node *curnode;
qitem data;
do {
curnode = q->front;
// bug out if list is empty
if (curnode == NULL) {
data = NULL;
break;
}
// get node's data value (e.g. pointer to customer struct)
data = curnode->data;
#if QBACK
node *back = q->back;
#if DLINK
curnode->prev = back;
#endif
if (curnode == back)
q->back = curnode->next;
#endif
q->front = curnode->next;
#if QSIZE
q->size -= 1;
#endif
// release the node's storage back to the heap
free(curnode);
} while (0);
return data;
}
// qfront -- peek at front of queue
qitem
qfront(queue *q)
{
node *curnode;
qitem data;
curnode = q->front;
if (curnode != NULL)
data = curnode->data;
else
data = NULL;
return data;
}
UPDATE #3:
so my main function has a memory leak and I cannot find it. I am trying to learn Valgrind and gdb but there is a learning curve. My Valgrind outran is like 90,000 characters for some reason and doesn't say hey I have a memory leak. I don't even know what line. I appended my new main to the bottom of the above code.
A few issues:
You malloc the q pointer at the top of the outer while (testCases--) loop but do not free it.
But, as I mentioned, you need an array of queues [which can be a fixed array] and you've already done that.
But, q was your original/legacy code. The rest of your code no longer uses q. You just forgot to remove it.
You are now using customerArray instead. But, you are not calling qinit on each array element, so you could have a random data in each.
In the problem definition, the number of customer lines is fixed at 12, so you really don't need to malloc and free it. That's why I used a global array instead.
You have a bug in your totalTime calculation loop. You're doing a free of the customer record. So, the record is no longer valid in the loop below it.
And, in that second loop, you're doing a second free of the same struct. So, at present, this is a "double free" bug. The second loop is actually the correct place for the free [not the first loop]
This program is ambitious for a second ever assignment, so you're "being thrown out into the middle of the lake and being asked to swim to shore".
I would have expected a few more intermediate steps/programs before trying to code up this one.
When starting out, as you are, sometimes it's as important to study working code as it is to write it from scratch.
So, spoiler alert, here's my final, fully working code:
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#define TIME_PAST -1 // infinite time in the past
#define TIME_FUTURE INT_MAX // infinite time in the future
int curtime; // current time
// customer record
typedef char customerName[9];
typedef struct customer {
customerName name; // 1-9 upper case letters
int lineNumber; // line number customer gets on
int time; // arrival time at line
int numberItems; // number of items customer has
} customer;
// 1=use queue q->back
// need this for _doubly_ linked list
// speedup for append to end of queue for singly linked
#ifndef QBACK
#define QBACK 1
#endif
// 1=use queue q->size
#ifndef QSIZE
#define QSIZE 1
#endif
// 1=use doubly linked list
#ifndef DLINK
#define DLINK 0
#endif
// force QBACK on if doing doubly linked list
#if DLINK
#undef QBACK
#define QBACK 1
#endif
// [singly linked] queue element
typedef customer *qitem;
typedef struct node {
qitem data; // pointer to actual data
struct node *next; // forward pointer to next item
#if DLINK
struct node *prev; // backward pointer to previous item
#endif
} node;
// queue definition (singly linked list)
// NOTE:
typedef struct queue {
node *front; // pointer to first node in list
#if QBACK
node *back; // pointer to last node in list
#endif
#if QSIZE
int size; // current number of elements in list
#endif
} queue;
// list of queues (indexed by lineNumber - 1)
#define NUMLINES 12
queue queue_list[NUMLINES];
// qinit -- initialize/reset queue
void
qinit(queue *q)
{
q->front = NULL;
#if QSIZE
q->size = 0;
#endif
#if QBACK
q->back = NULL;
#endif
}
// qempty -- returns 1 if empty or 0 if false
int
qempty(queue *q)
{
// NOTE: size really isn't needed
#if 0
return (q->size == 0);
#else
return (q->front == NULL);
#endif
}
// enqueue -- append element to end of queue
void
enqueue(queue *q, qitem data)
{
node *newnode;
node *prev;
newnode = malloc(sizeof(node));
newnode->next = NULL;
newnode->data = data;
// NOTE: this is the only place where back is a speedup
#if QBACK
prev = q->back;
#else
// what we have to do find the back of the queue with only a front pointer
prev = NULL;
for (node *cur = q->front; cur != NULL; cur = cur->next)
prev = cur;
#endif
if (prev != NULL)
prev->next = newnode;
else
q->front = newnode;
#if DLINK
newnode->prev = prev;
#endif
#if QBACK
q->back = newnode;
#endif
#if QSIZE
q->size += 1;
#endif
}
// dequeue -- dequeue from the front of the queue
qitem
dequeue(queue *q)
{
node *curnode;
node *nextnode;
qitem data;
do {
// get the first node
curnode = q->front;
// if none, return null data
if (curnode == NULL) {
data = NULL;
break;
}
// get the data payload
data = curnode->data;
// point to the next node (i.e. second node)
nextnode = curnode->next;
#if QBACK
node *back = q->back;
#if DLINK
curnode->prev = back;
#endif
if (curnode == back)
q->back = nextnode;
#endif
q->front = nextnode;
#if QSIZE
q->size -= 1;
#endif
free(curnode);
} while (0);
return data;
}
// qfront -- peek at front of queue
qitem
qfront(queue *q)
{
node *curnode;
qitem data;
curnode = q->front;
if (curnode != NULL)
data = curnode->data;
else
data = NULL;
return data;
}
// custfind -- find next customer to serve
customer *
custfind(void)
{
int pass;
int qidx;
queue *q;
int mintime;
int moreflg;
customer *cust;
customer *best;
// pass 1:
// - remember the minimum time
// - find best using curtime
for (pass = 1; pass <= 2; ++pass) {
best = NULL;
mintime = TIME_FUTURE;
moreflg = 0;
// scan all queues -- examine _first_ customer on each line
for (qidx = 0; qidx < NUMLINES; ++qidx) {
q = &queue_list[qidx];
// get first customer on current line
cust = qfront(q);
if (cust == NULL)
continue;
// remember that there is at least _one_ customer waiting
moreflg = 1;
// remember the minimum time
if (cust->time < mintime)
mintime = cust->time;
// has the customer actually arrived yet? -- skip if not
if (cust->time > curtime)
continue;
// no previous "best" customer -- set from current
if (best == NULL) {
best = cust;
continue;
}
// get better customer (has fewer items to check out)
if (cust->numberItems < best->numberItems) {
best = cust;
continue;
}
}
// no more customers
if (! moreflg)
break;
// found a best match
// dequeue and free node
if (best != NULL) {
q = &queue_list[best->lineNumber - 1];
dequeue(q);
break;
}
// no best match found
// all customers are in the future [based on curtime]
// set new current time based on minimum time of all remaining customers
// the second pass _will_ find at least one after we do this
curtime = mintime;
}
return best;
}
// custdo -- check out customer
void
custdo(customer *cust)
{
int elap;
// current time has to be _at least_ the arrival time
if (curtime < cust->time)
curtime = cust->time;
// get amount of time it takes to service this customer
elap = 0;
elap += 30;
elap += cust->numberItems * 5;
// set current time after servicing the customer
curtime += elap;
printf("%s from line %d checks out at time %d.\n",
cust->name,cust->lineNumber,curtime);
// release the customer record storage
free(cust);
}
// testcase -- process a test case
void
testcase(FILE *fi)
{
queue *q;
customer *cust;
// reset all queues
// NOTE: probably not required
for (int qidx = 0; qidx < NUMLINES; ++qidx) {
q = &queue_list[qidx];
qinit(q);
}
// get number of customers for this case
int numCustomers;
fscanf(fi," %d", &numCustomers);
// read in all customer records for this test case
for (int icust = 0; icust < numCustomers; ++icust) {
cust = malloc(sizeof(*cust));
fscanf(fi," %d", &cust->time);
fscanf(fi," %d", &cust->lineNumber);
fscanf(fi," %s", cust->name);
fscanf(fi," %d", &cust->numberItems);
// add customer to appropriate line/queue
q = &queue_list[cust->lineNumber - 1];
enqueue(q,cust);
}
// set current time to [way in the] past
curtime = TIME_PAST;
while (1) {
// find the next customer
cust = custfind();
// no more customers
if (cust == NULL)
break;
// service customer
custdo(cust);
}
}
int
main(int argc, char **argv)
{
char *ifile;
FILE *fi;
--argc;
++argv;
// open the input file
if (argc > 0)
ifile = *argv;
else
ifile = "input.txt";
fi = fopen(ifile,"r");
if (fi == NULL) {
perror(ifile);
exit(1);
}
// get number of test cases
int testCases;
fscanf(fi," %d", &testCases);
// process all test cases
for (; testCases > 0; --testCases)
testcase(fi);
fclose(fi);
return 0;
}

A pointer points to a NULL pointer

code from cs50 harvard course dealing with linked list:
---The problem I do not understand is that when node *ptr points to numbers, which is a null pointer, how can the for loop: (node *ptr = numbers; ptr != NULL) run at all since *numbers = NULL?---
full version of the codes can be found at: https://cdn.cs50.net/2017/fall/lectures/5/src5/list2.c
#include <cs50.h>
#include <stdio.h>
typedef struct node
{
int number;
struct node *next;
}
node;
int main(void)
{
// Memory for numbers
node *numbers = NULL;
// Prompt for numbers (until EOF)
while (true)
{
// Prompt for number
int number = get_int("number: ");
// Check for EOF
if (number == INT_MAX)
{
break;
}
// Check whether number is already in list
bool found = false;
for (node *ptr = numbers; ptr != NULL; ptr = ptr->next)
{
if (ptr->number == number)
{
found = true;
break;
}
}
The loop is to check for prior existence in the list actively being built. If not there (found was never set true), the remaining inconveniently omitted code adds it to the list.
On initial run, the numbers linked list head pointer is null, signifying an empty list. That doesn't change the algorithm of search + if-not-found-insert whatsoever. It just means the loop is never entered because the bail-case is immediately true. in other words, with numbers being NULL
for (node *ptr = numbers; ptr != NULL; ptr = ptr->next)
the condition to continue, ptr != NULL is already false, so the body of the for-loop is simply skipped. That leads to the remainder of the code you didn't post, which does the actual insertion. After that insertion, the list now has something, and the next iteration of the outer-while loop will eventually scan the list again after the next prospect value is read. This continues until the outer-while condition is no longer satisfied.
A Different Approach
I have never been fond of the cs50 development strategy, and Harvard's technique for teaching C to entry-level CS students. The cs50 header and lib has caused more transitional confusion to real-world software engineering than one can fathom. Below is an alternative for reading a linked list of values, keeping only unique entries. It may look like a lot, but half of this is inline comments describing what is going on. Some of it will seem trivial, but the search-and-insert methodology is what you should be focusing on. It uses a strategy of pointer-to-pointer that you're likely not familiar with, and this is a good exposure.
Enjoy.
#include <stdio.h>
#include <stdlib.h>
struct node
{
int value;
struct node *next;
};
int main()
{
struct node *numbers = NULL;
int value = 0;
// retrieve list input. stop when we hit
// - anything that doesn't parse as an integer
// - a value less than zero
// - EOF
while (scanf("%d", &value) == 1 && value >= 0)
{
// finds the address-of (not the address-in) the first
// pointer whose node has a value matching ours, or the
// last pointer in the list (which points to NULL).
//
// note the "last" pointer will be the head pointer if
// the list is empty.
struct node **pp = &numbers;
while (*pp && (*pp)->value != value)
pp = &(*pp)->next;
// if we didn't find our value, `pp` holds the address of
// the last pointer in the list. Again, not a pointer to the
// last "node" in the list; rather the last actual "pointer"
// in the list. Think of it as the "next" member of last node,
// and in the case of an empty list, it will be the address of
// the head pointer. *That* is where we will be hanging our
// new node, and since we already know where it goes, there is
// no need to rescan the list again.
if (!*pp)
{
*pp = malloc(sizeof **pp);
if (!*pp)
{
perror("Failed to allocate new node");
exit(EXIT_FAILURE);
}
(*pp)->value = value;
(*pp)->next = NULL;
}
}
// display entire list, single line
for (struct node const *p = numbers; p; p = p->next)
printf("%d ", p->value);
fputc('\n', stdout);
// free the list
while (numbers)
{
struct node *tmp = numbers;
numbers = numbers->next;
free(tmp);
}
return EXIT_SUCCESS;
}
This approach is especially handy when building sorted lists, as it can be altered with just a few changes to do so.
If you examine rest of the code which is also within the while loop, you can see alteration of numbers on the shared link.
if (!found)
{
// Allocate space for number
node *n = malloc(sizeof(node));
if (!n)
{
return 1;
}
// Add number to list
n->number = number;
n->next = NULL;
if (numbers)
{
for (node *ptr = numbers; ptr != NULL; ptr = ptr->next)
{
if (!ptr->next)
{
ptr->next = n;
break;
}
}
}
else
{
numbers = n;
}
}
Besides, it doesn't hit body of the for loop at first, so your thinking is correct.

Segmentation fault while creating a linked list

I am writing a small program which stores data and key inside a linked list structure, and retrieves data based on a key from the user. The program also checks whether it is a unique key and if it so it stores the data by creating a node at the front of the list. But the below code throws segmentation fault all the time.
#include<stdlib.h>
/* Node having data, unique key, and next */.
struct node
{
int data;
int key;
struct node *next;
}*list='\0',*p;
/* Create a node at the front */
void storeData(int data_x,int key_x)
{
int check_key;
position *nn; //nn specifies newnode
nn=(position)malloc(sizeof(struct node));
/* Segmentation Fault occurs here */
if(list->next==NULL)
{
nn->next=list->next;
nn->data = data_x;
nn->key = key_x;
list->next = nn;
}
else
{
check_key=checkUniqueKey(key_x);
if(check_key != FALSE)
{
printf("The entered key is not unique");
}
else
{
nn->data = data_x;
nn->key = key_x;
nn->next=list->next;
list->next=nn;
}
}
}
/* Retreive data based on a key */
int retreiveData(int key_find)
{
int ret_data = NULL;
p=list->next;
while(p->next != NULL)
{
if(p->key == key_find)
{
ret_data = p->data;
break;
}
p=p->next;
}
return(ret_data);
}
/* Checks whether user key is unique */
int checkUniqueKey(int key_x)
{
int key_check = FALSE;
p=list->next;
while(p->next != NULL)
{
if(p->key == key_x)
{
key_check = TRUE;
break;
}
p=p->next;
}
return(key_check);
}
The segmentation fault occurs in the storeData function after the dynamic allocation.
There are some problems in your code:
your list handling is flawed: you always dereference the global pointer list, even before any list items are created. You should instead test if the list is empty by comparing list to NULL.
type position is not defined. Avoid hiding pointers behind typedefs, this is a great cause of confusion, which explains your mishandling of list pointers.
avoid defining a global variable with the name p, which is unneeded anyway. Define p as a local variable in the functions that use it.
NULL is the null pointer, 0 a zero integer value and \0 the null byte at the end of a C string. All 3 evaluate to 0 but are not always interchangeable.
For better portability and readability, use the appropriate one for each case.
Here is an improved version:
#include <stdio.h>
#include <stdlib.h>
/* Node having data, unique key, and next */.
struct node {
int data;
int key;
struct node *next;
} *list;
/* Create a node at the front */
void storeData(int data_x, int key_x) {
if (checkUniqueKey(key_x)) {
printf("The entered key is not unique\n");
} else {
/* add a new node to the list */
struct node *nn = malloc(sizeof(struct node));
if (nn == NULL) {
printf("Cannot allocate memory for node\n");
return;
}
nn->data = data_x;
nn->key = key_x;
nn->next = list;
list = nn;
}
}
/* Retrieve data based on a key */
int retrieveData(int key_find) {
struct node *p;
int ret_data = 0;
for (p = list; p != NULL; p = p->next) {
if (p->key == key_find) {
ret_data = p->data;
break;
}
}
return ret_data;
}
/* Checks whether user key is unique */
int checkUniqueKey(int key_x) {
struct node *p;
int key_check = FALSE;
for (p = list; p != NULL; p = p->next) {
if (p->key == key_x) {
key_check = TRUE;
break;
}
}
return key_check;
}
You try to cast your address on a position structure instead of a position*
nn=(position)malloc(sizeof(struct node));
Compile your code with gcc flags -Wextra and -Wall to prevent this kind of issue.
Moreover I don't know is it is a mistake but malloc a size of struct node and your nn variable is a pointer on position.
When you initialized your list pointer you set it to NULL(as '\0'), when the program accesses address 0x00 it goes out of its boundaries and the operating system kills the process.
To avoid the segfault you can have "list" of non pointer type thus allocating on stack, when you want to access list as pointer you can do &list. Another solution would involve having variable on stack "root_node" and initialize list pointer as list = &root_node.

How to free memory occupied by a Tree, C?

I'm currently dealing with a generic Tree with this structure:
typedef struct NODE {
//node's keys
unsigned short *transboard;
int depth;
unsigned int i;
unsigned int j;
int player;
int value;
struct NODE *leftchild; //points to the first child from the left
struct NODE *rightbrothers; //linked list of brothers from the current node
}NODE;
static NODE *GameTree = NULL;
While the function that allocates the different nodes is (don't bother too much at the keys' values, basically allocates the children-nodes. If there aren't any the new child goes to leftchild, otherwise it goes at the end of the list "node->leftchild->rightbrothers"):
static int AllocateChildren(NODE **T, int depth, unsigned int i, unsigned int j, int player, unsigned short *transboard) {
NODE *tmp = NULL;
if ((*T)->leftchild == NULL) {
if( (tmp = (NODE*)malloc(sizeof(NODE)) )== NULL) return 0;
else {
tmp->i = i;
tmp->j = j;
tmp->depth = depth;
(player == MAX ) ? (tmp->value = 2 ): (tmp->value = -2);
tmp->player = player;
tmp->transboard = transboard;
tmp->leftchild = NULL;
tmp->rightbrothers = NULL;
(*T)->leftchild = tmp;
}
}
else {
NODE *scorri = (*T)->leftchild;
while (scorri->rightbrothers != NULL)
scorri = scorri->rightbrothers;
if( ( tmp = (NODE*)malloc(sizeof(NODE)) )== NULL) return 0;
else {
tmp->i = i;
tmp->j = j;
tmp->depth = depth;
(player == MAX) ? (tmp->value = 2) : (tmp->value = -2);
tmp->player = player;
tmp->transboard = transboard;
tmp->leftchild = NULL;
tmp->rightbrothers = NULL;
}
scorri->rightbrothers = tmp;
}
return 1;
}
I need to come up with a function, possibly recursive, that deallocates the whole tree, so far I've come up with this:
void DeleteTree(NODE **T) {
if((*T) != NULL) {
NODE *tmp;
for(tmp = (*T)->children; tmp->brother != NULL; tmp = tmp->brother) {
DeleteTree(&tmp);
}
free(*T);
}
}
But it doesn't seem working, it doesn't even deallocate a single node of memory.
Any ideas of where I am being wrong or how can it be implemented?
P.s. I've gotten the idea of the recursive function from this pseudocode from my teacher. However I'm not sure I've translated it correctly in C with my kind of Tree.
Pseudocode:
1: function DeleteTree(T)
2: if T != NULL then
3: for c ∈ Children(T) do
4: DeleteTree(c)
5: end for
6: Delete(T)
7: end if
8: end function
One thing I like doing if I'm allocating lots of tree nodes, that are going to go away at the same time, is to allocate them in 'batches'. I malloc then as an array of nodes and dole them out from a special nodealloc function after saving a pointer to the array (in a function like below). To drop the tree I just make sure I'm not keeping any references and then call the free routine (also like below).
This can also reduce the amount of RAM you allocate if you're lucky (or very smart) with your initial malloc or can trust realloc not to move the block when you shrink it.
struct freecell { struct freecell * next; void * memp; } * saved_pointers = 0;
static void
save_ptr_for_free(void * memp)
{
struct freecell * n = malloc(sizeof*n);
if (!n) {perror("malloc"); return; }
n->next = saved_pointers;
n->memp = memp;
saved_pointers = n;
}
static void
free_saved_memory(void)
{
while(saved_pointers) {
struct freecell * n = saved_pointers;
saved_pointers = saved_pointers->next;
free(n->memp);
free(n);
}
}
I've just realized my BIG mistake in the code and I'll just answer myself since no one had found the answer.
The error lies in this piece of code:
for(tmp = (*T)->children; tmp->brother != NULL; tmp = tmp->brother) {
DeleteTree(&tmp);
}
First of all Ami Tavory was right about the for condition, i need to continue as long as tmp != NULL
Basically it won't just work because after the DeleteTree(&tmp), I can no longer access the memory in tmp because it's obviously deleted, so after the first cycle of for ends I can't do tmp = tmp->rightbrother to move on the next node to delete because tmp->rightbrother no longer exists as I just deleted it.
In order to fix it I just needed to save the tmp->brother somewhere else:
void DeleteTree(NODE **T) {
if((*T) != NULL) {
NODE *tmp, *deletenode, *nextbrother;
for(tmp = (*T)->children; tmp != NULL; tmp = nextbrother) {
nextbrother = tmp->rightbrother;
DeleteTree(&tmp);
}
canc = (*T);
free(*T);
(*T) = NULL;
}
}
Just for the sake of completeness I want to add my version of DeleteTree
void DeleteTree(NODE *T) {
if(T != NULL) {
DeleteTree(T->rightbrothers);
DeleteTree(T->leftchild);
free(T);
}
}
I think it is much less obscure and much easier to read. Basically it solves the issue in DeleteTree but through eliminating the loop.
Since we free the nodes recursively we might as well do the whole process recursively.

C - Segfault when accessing struct member in a HashTable (insert function)

I am new to C and am having issues implementing an insert function for my HashTable.
Here are my structs:
typedef struct HashTableNode {
char *url; // url previously seen
struct HashTableNode *next; // pointer to next node
} HashTableNode;
typedef struct HashTable {
HashTableNode *table[MAX_HASH_SLOT]; // actual hashtable
} HashTable;
Here is how I init the table:
HashTable *initTable(){
HashTable* d = (HashTable*)malloc(sizeof(HashTable));
int i;
for (i = 0; i < MAX_HASH_SLOT; i++) {
d->table[i] = NULL;
}
return d;
}
Here is my insert function:
int HashTableInsert(HashTable *table, char *url){
long int hashindex = JenkinsHash(url, MAX_HASH_SLOT);
int uniqueBool = 2; // 0 for true, 1 for false, 2 for init
HashTableNode* theNode = (HashTableNode*)malloc(sizeof(HashTableNode));
theNode->url = url;
if (table->table[hashindex] != NULL) { // if we have a collision
HashTableNode* currentNode = (HashTableNode*)malloc(sizeof(HashTableNode));
currentNode = table->table[hashindex]->next; // the next node in the list
if (currentNode == NULL) { // only one node currently in list
if (strcmp(table->table[hashindex]->url, theNode->url) != 0) { // unique node
table->table[hashindex]->next = theNode;
return 0;
}
else{
printf("Repeated Node\n");
return 1;
}
}
else { // multiple nodes in this slot
printf("There was more than one element in this slot to start with. \n");
while (currentNode != NULL)
{
// SEGFAULT when accessing currentNode->url HERE
if (strcmp(currentNode->url, table->table[hashindex]->url) == 0 ){ // same URL
uniqueBool = 1;
}
else{
uniqueBool = 0;
}
currentNode = currentNode->next;
}
}
if (uniqueBool == 0) {
printf("Unique URL\n");
theNode->next = table->table[hashindex]->next; // splice current node in
table->table[hashindex]->next = theNode; // needs to be a node for each slot
return 0;
}
}
else{
printf("simple placement into an empty slot\n");
table->table[hashindex] = theNode;
}
return 0;
}
I get SegFault every time I try to access currentNode->url (the next node in the linked list of a given slot), which SHOULD have a string in it if the node itself is not NULL.
I know this code is a little dicey, so thank you in advance to anyone up for the challenge.
Chip
UPDATE:
this is the function that calls all ht functions. Through my testing on regular strings in main() of hash table.c, I have concluded that the segfault is due to something here:
void crawlPage(WebPage * page){
char * new_url = NULL;
int pos= 0;
pos = GetNextURL(page->html, pos, URL_PREFIX, &new_url);
while (pos != -1){
if (HashTableLookup(URLsVisited, new_url) == 1){ // url not in table
printf("url is not in table......\n");
hti(URLsVisited, new_url);
WebPage * newPage = (WebPage*) calloc(1, sizeof(WebPage));
newPage->url = new_url;
printf("Adding to LIST...\n");
add(&URLList, newPage); // added & to it.. no seg fault
}
else{
printf("skipping url cuz it is already in table\n");
}
new_url = NULL;
pos = GetNextURL(page->html, pos, URL_PREFIX, &new_url);
}
printf("freeing\n");
free(new_url); // cleanup
free(page); // free current page
}
Your hash table insertion logic violates some rather fundamental rules.
Allocating a new node before determining you actually need one.
Blatant memory leak in your currentNode allocation
Suspicious ownership semantics of the url pointer.
Beyond that, this algorithm is being made way too complicated for what it really should be.
Compute the hash index via hash-value modulo the table size.
Start at the table slot of the hash index, walking node pointers until one of two things happens:
You discover the node is already present
You reach the end of the collision chain.
Only in #2 above do you actually allocate a collision node and chain it to your existing collision list. Most of this is trivial when employing a pointer-to-pointer approach, which I demonstrate below:
int HashTableInsert(HashTable *table, const char *url)
{
// find collision list starting point
long int hashindex = JenkinsHash(url, MAX_HASH_SLOT);
HashTableNode **pp = table->table+hashindex;
// walk the collision list looking for a match
while (*pp && strcmp(url, (*pp)->url))
pp = &(*pp)->next;
if (!*pp)
{
// no matching node found. insert a new one.
HashTableNode *pNew = malloc(sizeof *pNew);
pNew->url = strdup(url);
pNew->next = NULL;
*pp = pNew;
}
else
{ // url already in the table
printf("url \"%s\" already present\n", url);
return 1;
}
return 0;
}
That really is all there is to it.
The url ownership issue I mentioned earlier is addressed above via string duplication using strdup(). Although not a standard library function, it is POSIX compliant and every non-neanderthal half-baked implementation I've seen in the last two decades provides it. If yours doesn't (a) I'd like to know what you're using, and (b) its trivial to implement with strlen and malloc. Regardless, when the nodes are being released during value-removal or table wiping, be sure and free a node's url before free-ing the node itself.
Best of luck.

Resources