Hello,
I am new to C. I have some experience with Java, because that is what they are teaching us. But I really want to learn C because I want to get a decent job doing decent code, so I am redoing my homeworks in C, or at least attempting to. Anyway I have a long way to go.
I am struggling with my attempt to use a linked list to implement a queue.
I include my code below.
Basically I want a queue struct containing a counter (for O(1) queue-count) and a link to the first node of the queue.
I then have a node struct.
My problem is with the add function - it should add new entries to the end of the linked list - which is accessed through the queue struct instance.
However it is hanging in the node traversal loop. I do not know why. I have included print statements to see/show where the problem is.
I am probably misunderstanding something fundamental, but I do not know what it is, and I have researched the web for hours, but have not found an example that works for me. I am using VS2010.
The relevant code, I think, is the main method, the makeQueue() method, and the add() method. I include the rest simply to give the full context.
I would be really grateful is someone could give me some constructive help here, as I would really love to improve my core programming skills. For the same reason I do not want to use any higher order library functions - I want to use the basic functionalities of C for this.
Thanks so much for your help!
Code:
#include "stdafx.h"
#include <stdio.h>
#include <iostream>
#include "stdlib.h"
#include <string.h>
typedef struct node{ //a string node struct
char* val;
struct node* next;
} list_node;
typedef struct queue{ //queue struct
struct node* first;
int count;
} list_queue;
list_queue* makeQueue();
void add(list_queue*, char*);
char* remove(list_queue*);
int isEmpty();
void print(list_queue*);
int main()
{
list_queue* myQueue = makeQueue();
int xx = 1;
while(xx){
printf("Enter: Insert, Remove, GetSize, Quit:\n");
char input[16];
scanf("%s",input);
//printf("%i",strlen(input));
if(*input == 'I' || *input == 'i'){
char name[30];
printf("Enter Name:\n");
scanf("%s",name);
add(myQueue, name);
}
else if(*input == 'R' || *input == 'r'){
remove(myQueue);
}
else if(*input == 'G' || *input == 'g'){
printf("%i\n",myQueue->count);
}
else if(*input == 'Q' || *input == 'q'){
break;
}
print(myQueue);
}
printf("\n");
char* cl;
printf("\n\nEnter any character and press enter:");
scanf("%1s", &cl);
return 0;
}
list_queue* makeQueue(){ //return *list_queue
list_queue* l_s = (list_queue*) malloc(sizeof(list_queue));
l_s->first = NULL;
l_s->count = 0;
return l_s;
}
void add(list_queue* s, char* c){
if(s->first == NULL){
printf("FIRST NODE\n");
s->first = (list_node*) malloc(sizeof(list_node));
s->first->val = c;
s->first->next = NULL;
}
else{
printf("HELLO\n");
list_node* prior = s->first;
list_node* current = s->first;
printf("HELLO2\n"); //THIS IS WHERE IT ALL GOES WRONG
if(current!=NULL) printf("NOT NULL\n");
while(current != NULL);
{ printf("HELLO3\n");
prior = current;
current = current->next;
}
current = (list_node*) malloc(sizeof(list_node));
current->val = c;
current->next = NULL;
prior->next = current;
}
s->count++;
}
char* remove(list_queue* s){
char* c = s->first->val;
list_node* t = s->first;
s->first = s->first->next;
free(t);
s->count--;
return c;
}
void print(list_queue* s){
list_node* t = s->first;
while(t != NULL){
int n = strlen(t->val);
char* str = t->val;
for(int i = 0; i < n; i++){
printf("%c", *(str+i));
}
printf(" ");
t = t->next;
}
printf("\n");
}