Skip to content

Instantly share code, notes, and snippets.

@satheesh-chandran
Created April 25, 2020 09:02
Show Gist options
  • Save satheesh-chandran/4bcc58afe2943ebed3f6c2c9981ee4ae to your computer and use it in GitHub Desktop.
Save satheesh-chandran/4bcc58afe2943ebed3f6c2c9981ee4ae to your computer and use it in GitHub Desktop.
Program for accessing user input and searching the positions of the entered position using linked list in C
#include <stdio.h>
#include <stdlib.h>
#include "list.h"
NODE* create_node(int value)
{
NODE *node = malloc(sizeof(NODE));
node->value = value;
node->next = NULL;
return node;
}
LIST* create_list(void)
{
LIST* list = malloc(sizeof(LIST));
list->first = NULL;
list->last = NULL;
return list;
}
void insert_node(LIST* list, int value)
{
NODE *node = create_node(value);
if (list->first == NULL)
{
list->first = node;
list->last = node;
return;
}
list->last->next = node;
list->last = node;
}
void destroy_list(LIST* list)
{
NODE *node_to_be_free = list->first;
NODE *next_node = list->first->next;
while (node_to_be_free != list->last)
{
free(node_to_be_free);
node_to_be_free = next_node;
next_node = next_node->next;
}
free(list->last);
free(list);
}
int find_index_of(LIST *list, int number)
{
int index = 0;
NODE *p_walk = list->first;
while (p_walk->next != NULL)
{
if (p_walk->value == number) return index;
p_walk = p_walk->next;
index++;
}
if (p_walk->value == number) return index;
return -1;
}
typedef struct node
{
int value;
struct node *next;
} NODE;
typedef struct list
{
NODE *first;
NODE *last;
} LIST;
NODE* create_node(int value);
LIST* create_list(void);
void insert_node(LIST* list, int value);
void access_input(LIST *list);
void print_list(LIST *list);
void search_positions(LIST *list);
void print_position(int number, int position);
int find_index_of(LIST *list, int number);
void destroy_list(LIST *list);
#include <stdio.h>
#include <stdlib.h>
#include "list.h"
int main(void)
{
LIST *list = create_list();
access_input(list);
search_positions(list);
print_list(list);
destroy_list(list);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include "list.h"
void access_input(LIST* list)
{
int number;
while (1)
{
printf("Please enter a number: ");
scanf("%d", &number);
if (number == -99)
{
return;
}
insert_node(list, number);
}
}
void print_position(int number, int position)
{
if (position == -1)
{
printf("%d is not present in the list\n", number);
return;
}
printf("%d is present in the list at position %d\n", number, position);
}
void search_positions(LIST *list)
{
int number;
while (1)
{
printf("What number would you like to search for? ");
scanf("%d", &number);
if (number == -99)
{
return;
}
int index = find_index_of(list, number);
print_position(number, index);
}
}
void print_list(LIST* list)
{
NODE *p_walk = list->first;
while (p_walk->next != NULL)
{
printf("%d ", p_walk->value);
p_walk = p_walk->next;
}
printf("%d\n", p_walk->value);
}
@vharidas
Copy link

LIST seems like a constant not type. List is good enough for type name.

Duplication of list->last = node; in insert_node

destroy_list is complex, you need to use free on memory twice because you did not simplify your logic.

find_index_of is also not reviewed or thought through.
You are doing if (p_walk->value == number) return index; once inside loop, once outside

Why do you need <stdio.h> in list.c, there is no IO operation in it.

Why are access_input, search_positions declared inside the list.h ?

While(1) is a very poor idea, what is the point in infinite loop and an "if" inside it ?

Overall, seems like the code has not been reviewed/refactored enough.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment