/**
*
* Author: Het Raval,het.raval@volansys.com
*
* @file Description : This file consists of simple linked list creation and inserting data using various techniques like at begining, at end, and at index as well as after node
* and the same goes on for deleting start,index and at the end.
*
*/
#include <stdio.h>
#include <stdlib.h>
#define MAX_MENU_NAMES 7
#define MAX_LIST_LENGTH 10
#define MAX_HEADER_SIZE 20
/**
* @breif This structure consists of two members one as data and another as next which is a pointer which stores position of next pointer
*/
struct node{
int data;/**< It will store the data of type int*/
struct node *next;/**< It will store the pointer which will point to the next pointer location*/
};
/**
*
* @breif This function will print line required for header
*
* @param occurance Will be the number of times the character will be printed
* @param The charcter that will be printed
*
*/
void print_line(int occurance,char value){
for(int i=0; i<occurance; i++){
}
}
/**
*
* @breif This funtion will print the header for the program
*
*/
void print_header(void){
print_line(MAX_HEADER_SIZE,'*');
print_line(MAX_HEADER_SIZE,'*');
}
/**
*
* @breif This array will store the names of the menu
*
*/
char *menu_names[] = {
"Insert_at_begining",
"Insert_at_index",
"Insert_at_end",
"Remove_from_start",
"Remove_from_end",
"Remove_from_index",
"Exit"
};
/**
*
* @breif This enum will replace the numbers with the names for better code understanding
*
*/
enum menu_options{
INSERT_BEGIN = 1,
INSERT_INDEX,
INSERT_END,
REMOVE_START,
REMOVE_END,
REMOVE_INDEX,
EXIT
};
/**
*
* @breif This function will display all the menus that a user can use
*
*/
void print_menu(void){
for(int i=0; i<MAX_MENU_NAMES; i++)
{
printf("%d. %s\n",i
+1,menu_names
[i
]); }
}
/**
* @breif This is a structure function which will insert a node at the begining of the linkedlist
*
* @param head_ref This will give the refrence of the current head of the linkedlist
* @param new_data This will be storing the new data that is being entered by the user
*
* @return new_node This will be returned from the function as the result and a new element will be added to the linkedlist.
*
*/
struct node * insert_at_begining(struct node* head_ref,int new_data){
struct node
* new_node
= (struct node
*)malloc(sizeof(struct node
)); if(NULL == new_node)
{
printf("Memory allocation unsuccessfull for node of insert at begining"); }
new_node->next = head_ref;
new_node->data = new_data;
head_ref = new_node;
return head_ref;
}
/**
* @breif This is a structure function which will insert a node at the specific index of the linkedlist
*
* @param head_ref This will give the refrence of the current head of the linkedlist.
* @param index It will store the index data at which the node you want to insert data at.
* @param new_data This will be storing the new data that is being entered by the user.
*
* @return head_ref This will be returned from the function as the result and a new element will be added to the linkedlist.
*
*/
struct node *insert_at_index(struct node* head_ref,int new_data,int index){
struct node
* new_node
= (struct node
*) malloc(sizeof(struct node
)); if(NULL == new_node)
{
printf("Memory allocation unsuccessfull for node of insert at index"); }
new_node->data = new_data;
struct node *ptr_for_traversing = head_ref;
int i=0;
while(i != index-1)
{
ptr_for_traversing = ptr_for_traversing->next;
i++;
}
new_node->next = ptr_for_traversing->next;
ptr_for_traversing->next = new_node;
return head_ref;
}
/**
* @breif This is a structure function which will insert a node at the end of the linkedlist
*
* @param head_ref This will give the refrence of the current head of the linkedlist
* @param new_data This will be storing the new data that is being entered by the user
*
* @return head_ref This will be returned from the function as the result and a new element will be added to the linkedlist.
*
*/
struct node *insert_at_end(struct node* head_ref,int new_data){
struct node
* new_node
= (struct node
*) malloc(sizeof(struct node
)); if(NULL == new_node)
{
printf("Memory allocation unsuccessfull for node of insert at end"); }
new_node->data = new_data;
struct node *ptr_for_traversing = head_ref;
while (ptr_for_traversing->next != NULL)
{
ptr_for_traversing = ptr_for_traversing->next;
}
ptr_for_traversing->next = new_node;
new_node->next = NULL;
return head_ref;
}
/**
* @breif This is a structure function which will remove a node from the start of the linkedlist
*
* @param head_ref This will give the refrence of the current head of the linkedlist
*
* @return head_ref This will be returned from the function as the result and a element will be deleted from the linkedlist.
*
*/
struct node * remove_first_node(struct node* head_ref){
struct node *ptr_for_traversing = head_ref;
head_ref = head_ref->next;
free(ptr_for_traversing
); return head_ref;
}
/**
* @breif This is a structure function which will remove a node as per the index entered by the user.
*
* @param head_ref This will give the refrence of the current head of the linkedlist.
* @param index This will be storing index entered by the user which evere node he wants to remove.
*
* @return head_ref This will be returned from the function as the result and a node as per the index will be removed.
*
*/
struct node * remove_at_index(struct node* head_ref, int index){
struct node *ptr_for_traversing = head_ref;
struct node *ptr_to_remove_node = head_ref->next;
for(int i=0; i<index-1;i++){
ptr_for_traversing = ptr_for_traversing->next;
ptr_to_remove_node = ptr_to_remove_node->next;
}
ptr_for_traversing->next = ptr_to_remove_node->next;
free(ptr_to_remove_node
); return head_ref;
}
/**
* @breif This is a structure function which will remove a node from the end of the linkedlist
*
* @param head_ref This will give the refrence of the current head of the linkedlist
*
* @return head_ref This will be returned from the function as the result and a element will be deleted from the linkedlist.
*
*/
struct node * remove_at_end(struct node * head_ref){
struct node * ptr_for_traversing = head_ref;
struct node * ptr_to_remove_node = head_ref->next;
while(ptr_to_remove_node->next != NULL){
ptr_for_traversing = ptr_for_traversing->next;
ptr_to_remove_node = ptr_to_remove_node->next;
}
ptr_for_traversing->next = NULL;
free(ptr_to_remove_node
); return head_ref;
}
/**
* @breif This is a structure function which will insert a node at the end of the linkedlist
*
* @param head_ref This will give the refrence of the current head of the linkedlist
* @param new_data This will be storing the new data that is being entered by the user
*
* @return head_ref This will be returned from the function as the result and a new element will be added to the linkedlist.
*
*/
struct node * remove_after_node(struct node * head_ref,struct node * prev_node){
struct node * ptr_to_remove_node = prev_node->next;
prev_node->next = ptr_to_remove_node->next;
free(ptr_to_remove_node
); return head_ref;
}
/**
* @breif This function will print the entire linked_list
*
* @param head_ref This will give the refrence of the current head of the linkedlist
*
*/
int Displaylist(struct node* node_ptr){
int i=0;
while(node_ptr != NULL){
printf("The value at element[%d] = %d\n",i
,node_ptr
->data
); node_ptr = node_ptr->next;
i++;
}
return 0;
}
void calculate_list_size(struct node* head_ref){
if(head_ref == NULL)
{
printf("No link list available"); }
else
{
int count = 0;
while(head_ref != NULL)
{
head_ref = head_ref->next;
count++;
}
printf("The size of the linked list is : %d\n",count
); }
}
void delete_linklist(struct node* head_ref){
if(head_ref != NULL)
{
struct node* ptr_for_traversing;
struct node* ptr_to_remove_node = head_ref;
while(ptr_for_traversing != NULL)
{
ptr_for_traversing = ptr_to_remove_node->next;
free(ptr_to_remove_node
); ptr_to_remove_node = ptr_for_traversing;
}
printf("Linked list deleted successfully"); }
else
{
printf("No list available to delete"); }
}
int main(){
struct node *head = NULL;
int no_of_nodes = 0;
int data = 0;
print_header();
printf("Please enter the number of nodes you want to add in linklist (MAX 10 at a time):"); scanf("%d", &no_of_nodes
); while(0 > no_of_nodes || MAX_LIST_LENGTH < no_of_nodes)
{
printf("Invalid Input!!\nplease enter a valid input :"); scanf("%d",&no_of_nodes
); }
for(int i = 0;i<no_of_nodes;i++)
{
printf("Enter the data for element[%d]:",i
); if(NULL == head)
{
head
= (struct node
*)malloc(sizeof(struct node
)); if(NULL == head)
{
printf("Memory allocation unsuccessfull"); }
head->data = data;
head->next = NULL;
}
else
{
head = insert_at_end(head,data);
}
}
printf("The linked list is as follows : \n\n"); Displaylist(head);
printf("Press Enter to continue"); int index;
int choice;
do{
print_header();
print_menu();
printf("Please select an operation that you want to perform for link list : ");
switch(choice)
{
case INSERT_BEGIN:
printf("enter the data you want to give to node :"); head = insert_at_begining(head,data);
printf("The linked list is as follows : \n\n"); Displaylist(head);
printf("Press Enter to continue"); break;
case INSERT_INDEX:
printf("enter the data you want to give to node :"); printf("enter the index you want the node after node :"); head = insert_at_index(head,data,index);
printf("The linked list is as follows : \n\n"); Displaylist(head);
printf("Press Enter to continue"); break;
case INSERT_END:
printf("enter the data you want to give to node :"); head = insert_at_end(head,data);
printf("The linked list is as follows : \n\n"); Displaylist(head);
printf("Press Enter to continue"); break;
case REMOVE_START:
head = remove_first_node(head);
printf("The linked list is as follows : \n\n"); Displaylist(head);
printf("Press Enter to continue"); break;
case REMOVE_END:
head = remove_at_end(head);
printf("The linked list is as follows : \n\n"); Displaylist(head);
printf("Press Enter to continue"); break;
case REMOVE_INDEX:
printf("enter the index you want to remove :"); head = remove_at_index(head,index);
printf("The linked list is as follows : \n\n"); Displaylist(head);
printf("Press Enter to continue"); break;
case EXIT:
break;
}
}while(choice != EXIT);
calculate_list_size(head);
delete_linklist(head);
return 0;
}