The prompt I was given asks for a program in c that implements a linked list and gives users options to perform different functions on the linked list. The needed functions are:
When I call the add_tail function it allows me to enter the value I wish to add, but after about a minute I get a message in the terminal saying the program was killed. What could be causing that? I'm also having issues with the while loop in the main function that is used to print the menu and take the users input. The menu will print normally when I first run the program, but after an action is completed the menu prints once but skips over the user input, and then prints again but accepts the user input the second time.
header file:
#ifndef LAB8_H_
#define LAB8_H_
#define FIND 'F'
#define EMPTY 'E'
#define ADD 'A'
#define INSERT 'I'
#define DELETE 'D'
#define REMOVE 'R'
#define REPLACE 'S'
#define GET 'G'
#define LIST 'L'
#define QUIT 'Q'
#define FAIL -100
struct node_t {
struct node_t * next;
int value;
};
struct node_t * create_node(int value);
struct node_t * add_tail(struct node_t * head, int value);
struct node_t * insert_node(struct node_t * head, int index, int value);
int is_empty(struct node_t * head);
int find_val(struct node_t * head, int value);
int get_value(struct node_t * head, int index);
struct node_t * replace_val(struct node_t * head, int index, int value);
struct node_t * remove_val(struct node_t * head, int value);
struct node_t * delete_node(struct node_t * head, int index);
void print_list(struct node_t * head);
#endif
.c file:
#include <stdio.h>
#include <stdlib.h>
#include "func.h"
struct node_t * create_node(int value)
{
struct node_t * new_node = malloc(sizeof(struct node_t));
new_node -> next = NULL;
new_node -> value = value;
return new_node;
}
struct node_t * add_tail(struct node_t * head, int value)
{
struct node_t * tmp;
if(head == NULL){
printf("Cannot add tail to an empty list. Try again\n");
return NULL;
} else{
while(head != NULL){
if(head -> next == NULL){
tmp = create_node(value);
head -> next = tmp;
} else{
head = head -> next;
}
}
}
return head;
}
struct node_t * insert_node(struct node_t * head, int index, int value)
{
struct node_t * tmp;
struct node_t * new;
if(index < 0){
printf("Index cannot be a negative number. Please try again\n");
return head;
}
if(index == 0){
tmp = head;
head = create_node(value);
head -> next = tmp;
} else {
tmp = head;
while(tmp != NULL){
if (index == 1){
struct node_t * prev = tmp;
tmp = tmp -> next;
new = create_node(value);
new -> next = tmp;
prev -> next = new;
head = prev;
} else if((tmp -> next == NULL) && (index != 0)){
printf("The index is not found in the bounds of the list. Try again\n");
return head;
} else{
tmp = tmp -> next;
index--;
}
}
}
return head;
}
int is_empty(struct node_t * head)
{
if(head == NULL){
return 0;
} else{
return 1;
}
}
int find_val(struct node_t * head, int value)
{
int index = 0;
if(head == NULL){
printf("Cannot find value in empty list! Try again\n");
index = -100;
} else{
while(head != NULL){
if((head -> next == NULL) && (head -> value != value)){
printf("The value does not exist in the list\n");
index = -100;
} else if(head -> value == value){
return index;
} else{
head = head -> next;
index++;
}
}
}
return index;
}
int get_value(struct node_t * head, int index)
{
int value;
if(index < 0){
printf("Index cannot be a negative number. Try again\n");
value = -100;
} else if(head == NULL){
printf("Cannot find index in empty list. Try again\n");
value = -100;
} else{
while(head != NULL){
if(index == 0){
value = head -> value;
} else if((head -> next == NULL) && (index != 0)){
printf("Index does not exist in the bounds of the list. Try again\n");
value= -100;
} else{
head = head -> next;
index--;
}
}
}
return value;
}
struct node_t * replace_val(struct node_t * head, int index, int value)
{
struct node_t * tmp;
if(index < 0){
printf("Index cannot be a negative number. Try again\n");
return NULL;
} else if(head == NULL){
printf("Cannot replace elements in an empty list. Try again\n");
return NULL;
} else{
while(head != NULL){
if (index == 0){
tmp = head;
tmp -> value = value;
free(head);
head = tmp;
} else if((head -> next == NULL) && (index != 0)){
printf("Index does not exist is the bounds of the list. Try again\n");
return NULL;
} else{
head = head -> next;
index--;
}
}
}
return head;
}
struct node_t * remove_val(struct node_t * head, int value)
{
struct node_t * tmp;
if(head == NULL){
printf("Value cannot be found in an empty list. Try again\n");
return NULL;
} else{
while(head != NULL){
if((head -> next == NULL) && (head -> value != value)){
printf("The value does not exist in the list. Try again\n");
return NULL;
} else if(head -> next -> value == value){
tmp = head -> next;
head -> next = tmp -> next;
free(tmp);
} else{
head = head -> next;
}
}
}
return head;
}
struct node_t * delete_node(struct node_t * head, int index)
{
struct node_t * tmp;
if(index < 0){
printf("Index cannot be a negative number. Try again\n");
return NULL;
} else if(head == NULL){
printf("Cannot delete elements from an empty list. Try again\n");
return NULL;
} else{
while(head != NULL){
if((head -> next == NULL) && (index != 0)){
printf("The index is not found in the bounds of the list. Try again\n");
return NULL;
} else if(index == 1){
tmp = head;
head = head -> next;
tmp -> next = head -> next;
head -> next = NULL;
free(head);
head = tmp;
} else{
head = head -> next;
index--;
}
}
}
return head;
}
void print_list(struct node_t * head)
{
if (head == NULL){
printf("The list is empty. Nothing to print\n");
} else{
while(head != NULL){
if(head -> next != NULL){
printf("%d, ", head -> value);
} else{
printf("%d", head -> value);
}
head = head -> next;
}
}
}
main file:
#include <stdio.h>
#include <stdlib.h>
#include "func.h"
int main(void)
{
struct node_t * head = NULL;
int index, value;
char choice;
int cont = 0;
while(cont == 0){
printf("Welcome to the Linked List operator. Here are the functions that can be performed:\n");
printf("A) Add element to the end of the list\n");
printf("I) Insert and element at a specific index\n");
printf("E) Check if the list is empty\n");
printf("F) Find a value in the list and return the index\n");
printf("G) Get the value at a specific index\n");
printf("S) Replace the value at a specific index\n");
printf("R) Remove the first occurance of a specific value\n");
printf("D) Delete the element at a specific index\n");
printf("L) List all the elements currently stored\n");
printf("Q) Quit the program\n");
printf("Please enter which action you would like to perform: ");
scanf("%c", &choice);
switch(choice){
case ADD:
printf("Please enter the value you would like to add: ");
scanf("%d", &value);
head = add_tail(head, value);
break;
case INSERT:
printf("Please enter the index at which you want to insert a new element: ");
scanf("%d", &index);
printf("Please enter the value you would like to insert: ");
scanf("%d", &value);
head = insert_node(head, index, value);
break;
case EMPTY:
if(is_empty(head) == 0){
printf("The list is empty!\n");
} else if(is_empty(head) == 1){
printf("The list is not empty!\n");
} else{
printf("Something went wrong\n");
}
break;
case FIND:
printf("Please enter the value that you would like to find in the list: ");
scanf("%d", &value);
index = find_val(head, value);
if(index == FAIL){
printf("Error. Try again\n");
} else{
printf("The index that the value %d exists at is %d\n", value, index);
}
break;
case GET:
printf("Please enter the index for which you would like to know the value: ");
scanf("%d", &index);
if(value == FAIL){
printf("Error. Try again\n");
} else{
printf("The value of the element at index %d is %d\n", index, value);
}
break;
case REPLACE:
printf("Please enter the index of the element that you would like to replace: ");
scanf("%d", &index);
printf("Please enter the new value: ");
scanf("%d", &value);
if(replace_val(head, index, value) == NULL){
printf("Error. Could not replace node\n");
} else{
printf("Success. Here is the new list:\n");
print_list(head);
}
break;
case REMOVE:
printf("Please enter the value that you would like to remove the first occurance of: ");
scanf("%d", &value);
if(remove_val(head, value) == NULL){
printf("Error. Could not remove node\n");
} else{
printf("Success! Here is the new list:\n");
print_list(head);
}
break;
case DELETE:
printf("Please enter the index of the element you would like to delete: ");
scanf("%d", &index);
if(delete_node(head, index) == NULL){
printf("Error. Could not delete selected element\n");
} else{
printf("Success! Here is the new list:\n");
print_list(head);
}
break;
case LIST:
printf("[");
print_list(head);
printf("]\n");
break;
case QUIT:
printf("You have chosen to quit the program. Goodbye!\n");
cont = 1;
break;
default:
printf("You entered an invalid choice. Please try again. Goodbye!\n");
}
}
return 0;
}
This if statement within the function add_tail
if(head == NULL){
printf("Cannot add tail to an empty list. Try again\n");
return NULL;
does not make sense. It should be removed.
Within the while loop in the function the pointer head
is changed.
while(head != NULL){
if(head -> next == NULL){
tmp = create_node(value);
head -> next = tmp;
} else{
head = head -> next;
}
}
And after adding a node the loop is not interrupted. It will again add a new node. That is the loop is infinite.
But even if you will insert the break statement
while(head != NULL){
if(head -> next == NULL){
tmp = create_node(value);
head -> next = tmp;
break;
} else{
head = head -> next;
}
}
the returned pointer from the function will not be the pointer that points to the head node of the list.
The function can be defined the following way
struct node_t * add_tail( struct node_t *head, int value )
{
struct node_t *new_node = create_node( value );
if ( head == NULL )
{
head = new_node;
}
else
{
struct node_t *current = head;
while ( current->next != NULL ) current = current->next;
current->next = new_node;
}
return head;
}
I am sure there are other problems with your code in your program. You can ask a new question after updating the function add_tail
to which you are referring in this your question.