BEN10, i explicitly let know that actually it returns pointer to my struct type.
Okay, i have worked a little on my code, and decided to implement this linked listed in a better way, and more natural as it should be.
So if u would like just take a quick look, say if something is wrong. Thank you.
Below is the working code of linked list implementation:
Code:
#include <stdio.h>
#include <stdlib.h>
#define snapd(expr) printf(#expr " = %d\n", expr);
#define snapd(expr) printf(#expr " = %d\n", expr);
/* Function prototypes */
void printlist(struct node *list);
void find(struct node *list, int value);
struct node *removenode(struct node *list, int value);
struct node *insert(struct node *list, int value);
/* Node definition */
struct node
{
int data;
struct node *ptr;
};
/* This function prints out the contents of the linked list */
void printlist(struct node *list)
{
if (list == NULL)
{
printf("The list is empty!\n");
return;
}
while (list->ptr != NULL)
{
printf("%d\n", list->data);
list = list->ptr;
}
if (list->ptr == NULL)
{
printf("%d\n", list->data);
}
}
void find(struct node *list, int value)
{
struct node *previous = list;
if (list->data == value)
{
printf("The element %d have been at the beginning of the list before %d\n", value, list->ptr->data);
return;
}
while (list->ptr != NULL)
{
if (list->data == value && list->ptr != NULL)
{
printf("The element %d have been found between %d and %d\n", value, previous->data, list->ptr->data);
return;
}
previous = list;
list = list->ptr;
}
if (list->data == value)
{
printf("The element %d have been found at the end of the list after %d\n", value, previous->data);
return;
}
printf("The element %d is not found in the list...\n");
}
/* This function inserts the value in the appropriate position in the linked list */
struct node *insert(struct node *list, int value)
{
struct node *newnode, *previous = list, *first = list;
//if the list is empty, add the first element
if (list == NULL)
{
newnode = (struct node *)malloc(sizeof(struct node));
newnode->data = value;
newnode->ptr = NULL;
return newnode;
}
// Add value before the first element
if (value < list->data)
{
snapd(list->data);
newnode = (struct node *)malloc(sizeof(struct node));
newnode->data = value;
newnode->ptr = list;
return newnode;
}
if (value == list->data)
{
printf("There is already element %d in the list!\n", value);
return first;
}
// Add the value in the middle of the list
while (list->ptr != NULL)
{
if (value < list->data)
{
newnode = (struct node *)malloc(sizeof(struct node));
newnode->data = value;
newnode->ptr = list;
previous->ptr = newnode;
return first;
}
if (value == list->data)
{
printf("There is already element %d in the list!\n", value);
return first;
}
previous = list;
list = list->ptr;
}
if (list->ptr == NULL && value == list->data)
{
printf("There is already element %d in the list!\n", value);
return first;
}
// Add after the last element of the list
if (list->ptr == NULL && value > list->data)
{
newnode = (struct node *)malloc(sizeof(struct node));
newnode->data = value;
newnode->ptr = NULL;
list->ptr = newnode;
return first;
}
// Add before the last element of the list
if (list->ptr == NULL && value < list->data)
{
newnode = (struct node *)malloc(sizeof(struct node));
newnode->data = value;
newnode->ptr = list;
previous->ptr = newnode;
return first;
}
}
/* This function deletes the specified node */
struct node *removenode(struct node *list, int value)
{
struct node *first = list, *previous = list;
//handle the empty list
if (list == NULL)
{
printf("The list is already empty!\n");
return first;
}
//handle the deletion of single element in the list
if (list->ptr == NULL && list->data == value)
{
free(list);
return NULL;
}
//handle the deletion of first element
if (list->ptr != NULL && list->data == value)
{
first = list->ptr;
list->ptr = NULL;
free(list);
return first;
}
while (list->ptr != NULL)
{
if (list->data == value)
{
previous->ptr = list->ptr;
free(list);
return first;
}
previous = list;
list = list->ptr;
}
// handle the deletion of the last element in the list
if (list->ptr == NULL && list->data == value)
{
previous->ptr = NULL;
free(list);
return first;
}
//the value to be removed havent been found in the list
printf("There is no %d in the list!\n", value);
return first;
}
void main (int argc, char *argv[])
{
struct node *mylist = NULL;
int value;
int command;
printf("Available commands:\n");
printf("1 - add new element:\n");
printf("2 - remove element:\n");
printf("3 - print the list:\n");
printf("4 - find a value in a list:\n");
printf("0 - quit program:\n");
printf("Enter command: ");
for(;;)
{
scanf("%d", &command);
switch (command)
{
case 1:
printf("Enter the value to be added in the list: ");
scanf("%d", &value);
mylist = insert(mylist,value);
printf("Enter the next command ");
break;
case 2:
printf("Enter the value to be removed: ");
scanf("%d", &value);
mylist = removenode(mylist, value);
printf("Enter the next command ");
break;
case 3:
printf("Linked list values:\n");
printf("===================\n");
printlist(mylist);
printf("===================\n");
printf("Enter the next command ");
break;
case 4:
printf("Enter the value to be searched for: ");
scanf("%d", &value);
find(mylist, value);
printf("Enter the next command ");
break;
case 0:
return;
break;
default:
printf("wrong command!\n");
printf("Available commands:\n");
printf("1 - add new element:\n");
printf("2 - remove element:\n");
printf("3 - print the list:\n");
printf("4 - find a value in a list:\n");
printf("0 - quit program:\n");
printf("Enter command: ");
break;
}
}
}