This is probably just a pointer issue or some stupid thing I'm doing, I'm doing a linked list program for a data structures class and unfortunately they want us to use C, when I could do this in Java in about 15 minutes. I've fixed bug after bug after bug and finally the instructor helped me look and we found one bug after about an hour but after that, nothing, still having issues.
Here's my mainline:
Code:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char * argv [])
{
typedef struct node * link;
struct node
{
int data;
int hasNext;
link next;
};
//Variables
link head = NULL;
link tail = NULL;
int exists = 0;
char * input;
int ninput;
//Main program "Infinite" Loop
while(1)
{
//Get command from user
printf("Enter command (Insert, Delete, Print, Done):");
scanf("%s", &input);
//INSERT
if(strcmp(&input, "Insert") == 0)
{
//Get data to be stored
printf("Enter an integer: ");
scanf("%d", &ninput);
//If empty
if(!exists)
{
head = createNode();
initNodes(ninput,head);
tail = head;
exists = 1;
printf("Head created\nData: %i\n", head->data);
}
//If list has at least 1 element
else
{
link curr = createNode();
initNodes(ninput, curr);
insertNext(tail, curr);
tail = curr;
}
}
//END OF INSERT
//DELETE (Most complex due to many cases to take into account)
else if(strcmp(&input, "Delete") == 0)
{
printf("Enter an integer: ");
scanf("%i", &ninput);
//If list is empty
if(!exists)
{
printf("List is empty\n");
}
//If list has at least 1 element
else
{
int found = 0;
link curr = head;
//If the first element is to be deleted
if(head->data == ninput)
{
//If there is only one element
if(head == tail)
{
free(head);
head = NULL;
tail = NULL;
exists = 0;
}
//If there are multiple elements
else
{
curr = (link)head->next;
deleteNext(&head);
head = curr;
}
}
//end of if
//If it's not the first element
else
{
link temp;
//Search list for element
while(curr != NULL)
{
if(curr->next == NULL) break;
temp = (link)curr->next;
if(temp->data == ninput)
{
found = 1;
break;
}
curr = (link)curr->next;
}
//If the element is found
if(found == 1)
{
if((link)curr->next == tail) tail = curr;
deleteNext(&curr);
}
//If it doesn't exist, print an error
else printf("Number couldn't be found.\n");
}
//end else
}
//end outer else
}
//END OF DELETE
//PRINT (simply calls printList)
else if(strcmp(&input, "Print") == 0)
{
if(!exists) printf("List is empty\n");
else
{
link curr = (link)head;
while(curr->hasNext)
{
printf("%i\n", curr->data);
curr = curr->next;
}
printf("%d\n", curr->data);
}
}
//DONE (exits loop)
else if(strcmp(&input, "Done") == 0) break;
//Invalid Input
else printf("Invalid input, try again.\n");
}
//Clean-Up + Exit
if(exists) deleteList(head);
return 0;
}
I'm using the int exists thing because for some reason I found out that forcing head to NULL then trying if(head == NULL) wasn't working, so I know I'm doing that wrong but I'm having issues with the print statement but before I talk about that let me paste my actual linked list code.
Code:
#include <stdio.h>
#include <stdlib.h>
typedef struct node * link;
struct node
{
int data;
int hasNext;
link next;
};
//Declarations
link createNode(void);
void initNodes(int num, link newNode);
void insertNext(link prevNode, link newNode);
link deleteNext(link prevNode);
void freeNode(link oldNode);
void printList(link head);
void deleteList(link head);
//Allocates memory for link
link createNode(void)
{
link temp = (link)malloc(sizeof(struct node));
return temp;
}
//Initializes node
void initNodes(int num, link newNode)
{
newNode->next = NULL;
newNode->data = num;
newNode->hasNext = 0;
}
//Inserts element into node
void insertNext(link prevNode, link newNode)
{
prevNode->next = newNode;
prevNode->hasNext = 1;
}
//Deletes the next node
link deleteNext(link prevNode)
{
link temp = prevNode->next;
if(temp->next != NULL)
{
prevNode->next = temp->next;
}
free(temp);
prevNode->next == NULL;
}
//Frees a node
void freeNode(link oldNode)
{
free(oldNode);
}
//Prints the list
void printList(link head)
{
//if(head == NULL) return; NOT NEEDED
link curr = head;
while(curr->hasNext)
{
printf("%i\n", curr->data);
curr = curr->next;
}
printf("%i\n", curr->data);
}
//Deletes the list
void deleteList(link head)
{
link curr = head;
while(curr->hasNext)
{
curr = curr->next;
free(head);
head = curr;
}
free(head);
}
The declarations are in there because they were specified to be for the assignment.
Here's an example of output:
Enter command (Insert, Delete, Print, Done):Insert 4
Enter an integer: Head created
Data: 4
Enter command (Insert, Delete, Print, Done):Print
36
Enter command (Insert, Delete, Print, Done)one
Segmentation fault
It always prints 36, and I put the printList code into the mainline to test if that was an issue but it's still doing it. Very odd. I'm using gcc compiler on OS X and linking the two files together correctly, I just know that whatever's going on likely has something to do with pointers and memory.
Sorry if this post is really long, if anyone can help that would be really appreciated, thanks!