fork download
  1. /**
  2.  *
  3.  * Author: Het Raval,het.raval@volansys.com
  4.  *
  5.  * @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
  6.  * and the same goes on for deleting start,index and at the end.
  7.  *
  8.  */
  9.  
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12.  
  13. #define MAX_MENU_NAMES 7
  14. #define MAX_LIST_LENGTH 10
  15. #define MAX_HEADER_SIZE 20
  16.  
  17. /**
  18.  * @breif This structure consists of two members one as data and another as next which is a pointer which stores position of next pointer
  19.  */
  20. struct node{
  21. int data;/**< It will store the data of type int*/
  22. struct node *next;/**< It will store the pointer which will point to the next pointer location*/
  23. };
  24.  
  25. /**
  26.  *
  27.  * @breif This function will print line required for header
  28.  *
  29.  * @param occurance Will be the number of times the character will be printed
  30.  * @param The charcter that will be printed
  31.  *
  32.  */
  33. void print_line(int occurance,char value){
  34. for(int i=0; i<occurance; i++){
  35. printf("%c",value);
  36. }
  37. }
  38.  
  39. /**
  40.  *
  41.  * @breif This funtion will print the header for the program
  42.  *
  43.  */
  44. void print_header(void){
  45. print_line(MAX_HEADER_SIZE,'*');
  46. printf(" Singly Link List ");
  47. print_line(MAX_HEADER_SIZE,'*');
  48. printf("\n\n");
  49. }
  50.  
  51. /**
  52.  *
  53.  * @breif This array will store the names of the menu
  54.  *
  55.  */
  56. char *menu_names[] = {
  57. "Insert_at_begining",
  58. "Insert_at_index",
  59. "Insert_at_end",
  60. "Remove_from_start",
  61. "Remove_from_end",
  62. "Remove_from_index",
  63. "Exit"
  64. };
  65.  
  66. /**
  67.  *
  68.  * @breif This enum will replace the numbers with the names for better code understanding
  69.  *
  70.  */
  71. enum menu_options{
  72. INSERT_BEGIN = 1,
  73. INSERT_INDEX,
  74. INSERT_END,
  75. REMOVE_START,
  76. REMOVE_END,
  77. REMOVE_INDEX,
  78. EXIT
  79. };
  80.  
  81. /**
  82.  *
  83.  * @breif This function will display all the menus that a user can use
  84.  *
  85.  */
  86. void print_menu(void){
  87. for(int i=0; i<MAX_MENU_NAMES; i++)
  88. {
  89. printf("%d. %s\n",i+1,menu_names[i]);
  90. }
  91. }
  92.  
  93. /**
  94.  * @breif This is a structure function which will insert a node at the begining of the linkedlist
  95.  *
  96.  * @param head_ref This will give the refrence of the current head of the linkedlist
  97.  * @param new_data This will be storing the new data that is being entered by the user
  98.  *
  99.  * @return new_node This will be returned from the function as the result and a new element will be added to the linkedlist.
  100.  *
  101.  */
  102. struct node * insert_at_begining(struct node* head_ref,int new_data){
  103. struct node * new_node = (struct node*)malloc(sizeof(struct node));
  104. if(NULL == new_node)
  105. {
  106. printf("Memory allocation unsuccessfull for node of insert at begining");
  107. exit(1);
  108. }
  109. new_node->next = head_ref;
  110. new_node->data = new_data;
  111. head_ref = new_node;
  112. return head_ref;
  113. }
  114.  
  115. /**
  116.  * @breif This is a structure function which will insert a node at the specific index of the linkedlist
  117.  *
  118.  * @param head_ref This will give the refrence of the current head of the linkedlist.
  119.  * @param index It will store the index data at which the node you want to insert data at.
  120.  * @param new_data This will be storing the new data that is being entered by the user.
  121.  *
  122.  * @return head_ref This will be returned from the function as the result and a new element will be added to the linkedlist.
  123.  *
  124.  */
  125. struct node *insert_at_index(struct node* head_ref,int new_data,int index){
  126. struct node * new_node = (struct node*) malloc(sizeof(struct node));
  127. if(NULL == new_node)
  128. {
  129. printf("Memory allocation unsuccessfull for node of insert at index");
  130. exit(1);
  131. }
  132. new_node->data = new_data;
  133. struct node *ptr_for_traversing = head_ref;
  134. int i=0;
  135.  
  136. while(i != index-1)
  137. {
  138. ptr_for_traversing = ptr_for_traversing->next;
  139. i++;
  140. }
  141. new_node->next = ptr_for_traversing->next;
  142. ptr_for_traversing->next = new_node;
  143. return head_ref;
  144. }
  145.  
  146. /**
  147.  * @breif This is a structure function which will insert a node at the end of the linkedlist
  148.  *
  149.  * @param head_ref This will give the refrence of the current head of the linkedlist
  150.  * @param new_data This will be storing the new data that is being entered by the user
  151.  *
  152.  * @return head_ref This will be returned from the function as the result and a new element will be added to the linkedlist.
  153.  *
  154.  */
  155. struct node *insert_at_end(struct node* head_ref,int new_data){
  156. struct node * new_node = (struct node*) malloc(sizeof(struct node));
  157. if(NULL == new_node)
  158. {
  159. printf("Memory allocation unsuccessfull for node of insert at end");
  160. exit(1);
  161. }
  162. new_node->data = new_data;
  163. struct node *ptr_for_traversing = head_ref;
  164.  
  165. while (ptr_for_traversing->next != NULL)
  166. {
  167. ptr_for_traversing = ptr_for_traversing->next;
  168. }
  169. ptr_for_traversing->next = new_node;
  170. new_node->next = NULL;
  171. return head_ref;
  172. }
  173.  
  174. /**
  175.  * @breif This is a structure function which will remove a node from the start of the linkedlist
  176.  *
  177.  * @param head_ref This will give the refrence of the current head of the linkedlist
  178.  *
  179.  * @return head_ref This will be returned from the function as the result and a element will be deleted from the linkedlist.
  180.  *
  181.  */
  182. struct node * remove_first_node(struct node* head_ref){
  183. struct node *ptr_for_traversing = head_ref;
  184. head_ref = head_ref->next;
  185.  
  186. free(ptr_for_traversing);
  187. return head_ref;
  188. }
  189.  
  190. /**
  191.  * @breif This is a structure function which will remove a node as per the index entered by the user.
  192.  *
  193.  * @param head_ref This will give the refrence of the current head of the linkedlist.
  194.  * @param index This will be storing index entered by the user which evere node he wants to remove.
  195.  *
  196.  * @return head_ref This will be returned from the function as the result and a node as per the index will be removed.
  197.  *
  198.  */
  199. struct node * remove_at_index(struct node* head_ref, int index){
  200. struct node *ptr_for_traversing = head_ref;
  201. struct node *ptr_to_remove_node = head_ref->next;
  202.  
  203. for(int i=0; i<index-1;i++){
  204. ptr_for_traversing = ptr_for_traversing->next;
  205. ptr_to_remove_node = ptr_to_remove_node->next;
  206. }
  207.  
  208. ptr_for_traversing->next = ptr_to_remove_node->next;
  209. free(ptr_to_remove_node);
  210. return head_ref;
  211. }
  212.  
  213. /**
  214.  * @breif This is a structure function which will remove a node from the end of the linkedlist
  215.  *
  216.  * @param head_ref This will give the refrence of the current head of the linkedlist
  217.  *
  218.  * @return head_ref This will be returned from the function as the result and a element will be deleted from the linkedlist.
  219.  *
  220.  */
  221. struct node * remove_at_end(struct node * head_ref){
  222. struct node * ptr_for_traversing = head_ref;
  223. struct node * ptr_to_remove_node = head_ref->next;
  224.  
  225. while(ptr_to_remove_node->next != NULL){
  226. ptr_for_traversing = ptr_for_traversing->next;
  227. ptr_to_remove_node = ptr_to_remove_node->next;
  228. }
  229.  
  230. ptr_for_traversing->next = NULL;
  231. free(ptr_to_remove_node);
  232. return head_ref;
  233. }
  234.  
  235. /**
  236.  * @breif This is a structure function which will insert a node at the end of the linkedlist
  237.  *
  238.  * @param head_ref This will give the refrence of the current head of the linkedlist
  239.  * @param new_data This will be storing the new data that is being entered by the user
  240.  *
  241.  * @return head_ref This will be returned from the function as the result and a new element will be added to the linkedlist.
  242.  *
  243.  */
  244. struct node * remove_after_node(struct node * head_ref,struct node * prev_node){
  245. struct node * ptr_to_remove_node = prev_node->next;
  246.  
  247. prev_node->next = ptr_to_remove_node->next;
  248. free(ptr_to_remove_node);
  249. return head_ref;
  250. }
  251.  
  252. /**
  253.  * @breif This function will print the entire linked_list
  254.  *
  255.  * @param head_ref This will give the refrence of the current head of the linkedlist
  256.  *
  257.  */
  258. int Displaylist(struct node* node_ptr){
  259. int i=0;
  260. while(node_ptr != NULL){
  261. printf("The value at element[%d] = %d\n",i,node_ptr->data);
  262. node_ptr = node_ptr->next;
  263. i++;
  264. }
  265. return 0;
  266. }
  267.  
  268. void calculate_list_size(struct node* head_ref){
  269. if(head_ref == NULL)
  270. {
  271. printf("No link list available");
  272. }
  273. else
  274. {
  275. int count = 0;
  276. while(head_ref != NULL)
  277. {
  278. head_ref = head_ref->next;
  279. count++;
  280. }
  281. printf("The size of the linked list is : %d\n",count);
  282. }
  283. }
  284.  
  285. void delete_linklist(struct node* head_ref){
  286. if(head_ref != NULL)
  287. {
  288. struct node* ptr_for_traversing;
  289. struct node* ptr_to_remove_node = head_ref;
  290.  
  291. while(ptr_for_traversing != NULL)
  292. {
  293. ptr_for_traversing = ptr_to_remove_node->next;
  294. free(ptr_to_remove_node);
  295. ptr_to_remove_node = ptr_for_traversing;
  296. }
  297. printf("Linked list deleted successfully");
  298. }
  299. else
  300. {
  301. printf("No list available to delete");
  302. }
  303. }
  304.  
  305. int main(){
  306. struct node *head = NULL;
  307.  
  308. int no_of_nodes = 0;
  309. int data = 0;
  310. system("clear");
  311. print_header();
  312. printf("Please enter the number of nodes you want to add in linklist (MAX 10 at a time):");
  313. scanf("%d", &no_of_nodes);
  314. while(0 > no_of_nodes || MAX_LIST_LENGTH < no_of_nodes)
  315. {
  316. printf("Invalid Input!!\nplease enter a valid input :");
  317. scanf("%d",&no_of_nodes);
  318. }
  319.  
  320. for(int i = 0;i<no_of_nodes;i++)
  321. {
  322. printf("Enter the data for element[%d]:",i);
  323. scanf("%d",&data);
  324. if(NULL == head)
  325. {
  326. head = (struct node*)malloc(sizeof(struct node));
  327. if(NULL == head)
  328. {
  329. printf("Memory allocation unsuccessfull");
  330. exit(1);
  331. }
  332. head->data = data;
  333. head->next = NULL;
  334. }
  335. else
  336. {
  337. head = insert_at_end(head,data);
  338. }
  339. }
  340. printf("\n");
  341. printf("The linked list is as follows : \n\n");
  342. Displaylist(head);
  343. printf("Press Enter to continue");
  344. int index;
  345. int choice;
  346. do{
  347. system("clear");
  348. print_header();
  349. print_menu();
  350. printf("Please select an operation that you want to perform for link list : ");
  351. scanf("%d", &choice);
  352.  
  353. switch(choice)
  354. {
  355. case INSERT_BEGIN:
  356. printf("enter the data you want to give to node :");
  357. scanf("%d", &data);
  358. head = insert_at_begining(head,data);
  359. printf("The linked list is as follows : \n\n");
  360. Displaylist(head);
  361. printf("Press Enter to continue");
  362. break;
  363.  
  364. case INSERT_INDEX:
  365. printf("enter the data you want to give to node :");
  366. scanf("%d", &data);
  367. printf("enter the index you want the node after node :");
  368. scanf("%d", &index);
  369. head = insert_at_index(head,data,index);
  370. printf("The linked list is as follows : \n\n");
  371. Displaylist(head);
  372. printf("Press Enter to continue");
  373. break;
  374.  
  375. case INSERT_END:
  376. printf("enter the data you want to give to node :");
  377. scanf("%d", &data);
  378. head = insert_at_end(head,data);
  379. printf("The linked list is as follows : \n\n");
  380. Displaylist(head);
  381. printf("Press Enter to continue");
  382. break;
  383.  
  384. case REMOVE_START:
  385. head = remove_first_node(head);
  386. printf("The linked list is as follows : \n\n");
  387. Displaylist(head);
  388. printf("Press Enter to continue");
  389. break;
  390.  
  391. case REMOVE_END:
  392. head = remove_at_end(head);
  393. printf("The linked list is as follows : \n\n");
  394. Displaylist(head);
  395. printf("Press Enter to continue");
  396. break;
  397.  
  398. case REMOVE_INDEX:
  399. printf("enter the index you want to remove :");
  400. scanf("%d", &index);
  401. head = remove_at_index(head,index);
  402. printf("The linked list is as follows : \n\n");
  403. Displaylist(head);
  404. printf("Press Enter to continue");
  405. break;
  406.  
  407. case EXIT:
  408. break;
  409. }
  410. }while(choice != EXIT);
  411. calculate_list_size(head);
  412. delete_linklist(head);
  413. return 0;
  414. }
  415.  
Success #stdin #stdout #stderr 0.01s 5304KB
stdin
2
1
2

1
5

7

stdout
******************** Singly Link List ********************

Please enter the number of nodes you want to add in linklist (MAX 10 at a time):Enter the data for element[0]:Enter the data for element[1]:
The linked list is as follows : 

The value at element[0] = 1
The value at element[1] = 2
Press Enter to continue******************** Singly Link List ********************

1. Insert_at_begining
2. Insert_at_index
3. Insert_at_end
4. Remove_from_start
5. Remove_from_end
6. Remove_from_index
7. Exit
Please select an operation that you want to perform for link list : enter the data you want to give to node :The linked list is as follows : 

The value at element[0] = 5
The value at element[1] = 1
The value at element[2] = 2
Press Enter to continue******************** Singly Link List ********************

1. Insert_at_begining
2. Insert_at_index
3. Insert_at_end
4. Remove_from_start
5. Remove_from_end
6. Remove_from_index
7. Exit
Please select an operation that you want to perform for link list : The size of the linked list is : 3
Linked list deleted successfully
stderr
TERM environment variable not set.
TERM environment variable not set.
TERM environment variable not set.