/*
* C Program to Implement Singly Linked List using Dynamic Memory
Allocation
*/
#include <stdio.h>
#include <malloc.h>
#define ISEMPTY printf("\nEMPTY LIST:");
/*
* Node Declaration
*/
struct node
{
int value;
struct node *next;
};
snode* create_node(int);
void insert_node_first();
void insert_node_last();
void insert_node_pos();
void delete_pos();
void search();
void update_val();
void display();
void rev_display(snode *);
typedef struct node snode;
snode *newnode, *ptr, *prev, *temp;
snode *first = NULL, *last = NULL;
/*
* Main :contains menu
*/
int main()
{
int ch;
char ans = 'Y';
while (ans == 'Y'||ans == 'y')
{
printf("\n---------------------------------\n");
printf("\nOperations on singly linked list\n");
printf("\n---------------------------------\n");
printf("\n1.Insert node at first");
printf("\n2.Insert node at last");
printf("\n3.Insert node at position");
printf("\n4.Delete Node from any Position");
printf("\n5.Update Node Value");
printf("\n6.Search Element in the linked list");
printf("\n7.Display List from Beginning to end");
printf("\n8.Display List from end using Recursion");
printf("\n9.Exit\n");
printf("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
printf("\nEnter your choice");
scanf("%d", &ch);
switch (ch)
{
case 1:
printf("\n...Inserting node at first...\n");
insert_node_first();
break;
case 2:
printf("\n...Inserting node at last...\n");
insert_node_last();
break;
case 3:
printf("\n...Inserting node at position...\n");
insert_node_pos();
break;
case 4:
printf("\n...Deleting Node from any Position...\n");
delete_pos();
break;
case 5:
printf("\n...Updating Node Value...\n");
update_val();
break;
case 6:
printf("\n...Searching Element in the List...\n");
search();
break;
case 7:
printf("\n...Displaying List From Beginning to End...\n");
display();
break;
case 8:
printf("\n...Displaying List From End using Recursion...\n");
rev_display(first);
break;
case 9:
printf("\n...Exiting...\n");
return 0;
break;
default:
printf("\n...Invalid Choice...\n");
break;
}
printf("\nYOU WANT TO CONTINUE (Y/N)");
scanf(" %c", &ans);
}
return 0;
}
/*
* Creating Node
*/
snode* create_node(int val)
{
newnode = (snode *)malloc(sizeof(snode));
if (newnode == NULL)
{
printf("\nMemory was not allocated");
return 0;
}
else
{
newnode->value = val;
newnode->next = NULL;
return newnode;
}
}
/*
* Inserting Node at First
*/
void insert_node_first()
{
int val;
printf("\nEnter the value for the node:");
scanf("%d", &val);
newnode = create_node(val);
if (first == last && first == NULL)
{
first = last = newnode;
first->next = NULL;
last->next = NULL;
}
else
{
temp = first;
first = newnode;
first->next = temp;
}
printf("\n----INSERTED----");
}
/*
* Inserting Node at Last
*/
void insert_node_last()
{
int val;
printf("\nEnter the value for the Node:");
scanf("%d", &val);
newnode = create_node(val);
if (first == last && last == NULL)
{
first = last = newnode;
first->next = NULL;
last->next = NULL;
}
else
{
last->next = newnode;
last = newnode;
last->next = NULL;
}
printf("\n----INSERTED----");
}
/*
* Inserting Node at position
*/
void insert_node_pos()
{
int pos, val, cnt = 0, i;
printf("\nEnter the value for the Node:");
scanf("%d", &val);
newnode = create_node(val);
printf("\nEnter the position ");
scanf("%d", &pos);
ptr = first;
while (ptr != NULL)
{
ptr = ptr->next;
cnt++;
}
if (pos == 1)
{
if (first == last && first == NULL)
{
first = last = newnode;
first->next = NULL;
last->next = NULL;
}
else
{
temp = first;
first = newnode;
first->next = temp;
}
printf("\nInserted");
}
else if (pos>1 && pos<=cnt)
{
ptr = first;
for (i = 1;i < pos;i++)
{
prev = ptr;
ptr = ptr->next;
}
prev->next = newnode;
newnode->next = ptr;
printf("\n----INSERTED----");
}
else
{
printf("Position is out of range");
}
}
/*
* Delete Node from specified position in a non-empty list
*/
void delete_pos()
{
int pos, cnt = 0, i;
if (first == NULL)
{
ISEMPTY;
printf(":No node to delete\n");
}
else
{
printf("\nEnter the position of value to be deleted:");
scanf(" %d", &pos);
ptr = first;
if (pos == 1)
{
first = ptr->next;
printf("\nElement deleted");
}
else
{
while (ptr != NULL)
{
ptr = ptr->next;
cnt = cnt + 1;
}
if (pos > 0 && pos <= cnt)
{
ptr = first;
for (i = 1;i < pos;i++)
{
prev = ptr;
ptr = ptr->next;
}
prev->next = ptr->next;
}
else
{
printf("Position is out of range");
}
free(ptr);
printf("\nElement deleted");
}
}
}
/*
* Updating Node value in a non-empty list
*/
void update_val()
{
int oldval, newval, flag = 0;
if (first == NULL)
{
ISEMPTY;
printf(":No nodes in the list to update\n");
}
else
{
printf("\nEnter the value to be updated:");
scanf("%d", &oldval);
printf("\nEnter the newvalue:");
scanf("%d", &newval);
for (ptr = first;ptr != NULL;ptr = ptr->next)
{
if (ptr->value == oldval)
{
ptr->value = newval;
flag = 1;
break;
}
}
if (flag == 1)
{
printf("\nUpdated Successfully");
}
else
{
printf("\nValue not found in List");
}
}
}
/*
* searching an element in a non-empty list
*/
void search()
{
int flag = 0, key, pos = 0;
if (first == NULL)
{
ISEMPTY;
printf(":No nodes in the list\n");
}
else
{
printf("\nEnter the value to search");
scanf("%d", &key);
for (ptr = first;ptr != NULL;ptr = ptr->next)
{
pos = pos + 1;
if (ptr->value == key)
{
flag = 1;
break;
}
}
if (flag == 1)
{
printf("\nElement %d found at %d position\n", key, pos);
}
else
{
printf("\nElement %d not found in list\n", key);
}
}
}
/*
* Displays non-empty List from Beginning to End
*/
void display()
{
if (first == NULL)
{
ISEMPTY;
printf(":No nodes in the list to display\n");
}
else
{
for (ptr = first;ptr != NULL;ptr = ptr->next)
{
printf("%d\t", ptr->value);
}
}
}
/*
* Display non-empty list in Reverse Order
*/
void rev_display(snode *ptr)
{
int val;
if (ptr == NULL)
{
ISEMPTY;
printf(":No nodes to display\n");
}
else
{
if (ptr != NULL)
{
val = ptr->value;
rev_display(ptr->next);
printf("%d\t", val);
}
}
}
OUTPUT:
$gcc linkedlist.c
a.out
---------------------------------
Operations on singly linked list
---------------------------------
1.Insert node at first
2.Insert node at last
3.Insert node at position
4.Delete Node from any Position
5.Update Node Value
6.Search Element in the linked list
7.Display List from Beginning to end
8.Display List from end using Recursion
9.Exit
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Enter your choice7
...Displaying List From Beginning to End...
EMPTY LIST::No nodes in the list to display
YOU WANT TO CONTINUE (Y/N)y
---------------------------------
Operations on singly linked list
---------------------------------
1.Insert node at first
2.Insert node at last
3.Insert node at position
4.Delete Node from any Position
5.Update Node Value
6.Search Element in the linked list
7.Display List from Beginning to end
8.Display List from end using Recursion
9.Exit
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Enter your choice4
...Deleting Node from any Position...
EMPTY LIST::No node to delete
YOU WANT TO CONTINUE (Y/N)
y
---------------------------------
Operations on singly linked list
---------------------------------
1.Insert node at first
2.Insert node at last
3.Insert node at position
4.Delete Node from any Position
5.Update Node Value
6.Search Element in the linked list
7.Display List from Beginning to end
8.Display List from end using Recursion
9.Exit
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Enter your choice5
...Updating Node Value...
EMPTY LIST::No nodes in the list to update
YOU WANT TO CONTINUE (Y/N)y
---------------------------------
Operations on singly linked list
---------------------------------
1.Insert node at first
2.Insert node at last
3.Insert node at position
4.Delete Node from any Position
5.Update Node Value
6.Search Element in the linked list
7.Display List from Beginning to end
8.Display List from end using Recursion
9.Exit
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Enter your choice
6
...Searching Element in the List...
EMPTY LIST::No nodes in the list
YOU WANT TO CONTINUE (Y/N)y
---------------------------------
Operations on singly linked list
---------------------------------
1.Insert node at first
2.Insert node at last
3.Insert node at position
4.Delete Node from any Position
5.Update Node Value
6.Search Element in the linked list
7.Display List from Beginning to end
8.Display List from end using Recursion
9.Exit
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Enter your choice
3
...Inserting node at position...
Enter the value for the Node:1010
Enter the position 5
Position is out of range
YOU WANT TO CONTINUE (Y/N)
---------------------------------
Operations on singly linked list
---------------------------------
1.Insert node at first
2.Insert node at last
3.Insert node at position
4.Delete Node from any Position
5.Update Node Value
6.Search Element in the linked list
7.Display List from Beginning to end
8.Display List from end using Recursion
9.Exit
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Enter your choice1
...Inserting node at first...
Enter the value for the node:100
----INSERTED----
YOU WANT TO CONTINUE (Y/N)y
---------------------------------
Operations on singly linked list
---------------------------------
1.Insert node at first
2.Insert node at last
3.Insert node at position
4.Delete Node from any Position
5.Update Node Value
6.Search Element in the linked list
7.Display List from Beginning to end
8.Display List from end using Recursion
9.Exit
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Enter your choice1
...Inserting node at first...
Enter the value for the node:200
----INSERTED----
YOU WANT TO CONTINUE (Y/N)y
---------------------------------
Operations on singly linked list
---------------------------------
1.Insert node at first
2.Insert node at last
3.Insert node at position
4.Delete Node from any Position
5.Update Node Value
6.Search Element in the linked list
7.Display List from Beginning to end
8.Display List from end using Recursion
9.Exit
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Enter your choice7
...Displaying List From Beginning to End...
200 100
YOU WANT TO CONTINUE (Y/N)y
---------------------------------
Operations on singly linked list
---------------------------------
1.Insert node at first
2.Insert node at last
3.Insert node at position
4.Delete Node from any Position
5.Update Node Value
6.Search Element in the linked list
7.Display List from Beginning to end
8.Display List from end using Recursion
9.Exit
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Enter your choice2
...Inserting node at last...
Enter the value for the Node:50
----INSERTED----
YOU WANT TO CONTINUE (Y/N)y
---------------------------------
Operations on singly linked list
---------------------------------
1.Insert node at first
2.Insert node at last
3.Insert node at position
4.Delete Node from any Position
5.Update Node Value
6.Search Element in the linked list
7.Display List from Beginning to end
8.Display List from end using Recursion
9.Exit
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Enter your choice2
...Inserting node at last...
Enter the value for the Node:150
----INSERTED----
YOU WANT TO CONTINUE (Y/N)y
---------------------------------
Operations on singly linked list
---------------------------------
1.Insert node at first
2.Insert node at last
3.Insert node at position
4.Delete Node from any Position
5.Update Node Value
6.Search Element in the linked list
7.Display List from Beginning to end
8.Display List from end using Recursion
9.Exit
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Enter your choice8
...Displaying List From Beginning to End...
200 100 50 150
YOU WANT TO CONTINUE (Y/N)
---------------------------------
Operations on singly linked list
---------------------------------
1.Insert node at first
2.Insert node at last
3.Insert node at position
4.Delete Node from any Position
5.Update Node Value
6.Search Element in the linked list
7.Display List from Beginning to end
8.Display List from end using Recursion
9.Exit
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Enter your choice3
...Inserting node at position...
Enter the value for the Node:1111
Enter the position 4
----INSERTED----
YOU WANT TO CONTINUE (Y/N)Y
---------------------------------
Operations on singly linked list
---------------------------------
1.Insert node at first
2.Insert node at last
3.Insert node at position
4.Delete Node from any Position
5.Update Node Value
6.Search Element in the linked list
7.Display List from Beginning to end
8.Display List from end using Recursion
9.Exit
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Enter your choice9
...Exiting...
Arrays Arravs Introduction Your ninth programming assignment will consist of two C++programs. Your programs should compile...
C++ code The assignment has two input files: • LSStandard.txt • LSTest.txt The LSStandard.txt file contains integer values against which we are searching. There will be no more than 100 of these. The LSTest.txt file contains a set of numbers that we are trying to locate within the standard data set. There will be no more than 50 of these. Read both files into two separate arrays. Your program should then close both input files. All subsequent processing will be...
The purpose of this assignment is to familiarize you with sort algorithms. Problem Description is as follows: 8. Search Benchmarks Write a program that has at least 20 250 integers stored in an array in ascending order. It should call a function that uses the linear search algorithm to locate one of the values. The function should keep a count of the number of comparisons it makes until it finds the value. The program then should call a function that...
Benchmark Searching and Sorting Write a program that has an array of at least 20 strings that you will have your user enter. It should call a function that uses the linear search algorithm to locate one of the values. The function should keep a count of the number of comparisons it makes until it finds the value. The program then should call a function that uses the binary search algorithm to locate the same value. It should also keep...
Using Arrays with Sorting and Searching Algorithms 1) This program has six required outputs and involves searching and sorting an array of integers. Write a java application that initializes an array with the following numbers, in this order: 23, 17, 5, 90, 12, 44, 38, 84, 77, 3, 66, 55, 1, 19, 37, 88, 8, 97, 25, 50, 75, 61, and 49. Then display the unsorted values. This is required output #1 of 6 for this program. 2) Using a...
This lab is to give you more experience with C++ Searching and Sorting Arrays Given a file with data for names and marks you will read them into two arrays You will then display the data, do a linear search and report if found, sort the data, do a binary search. Be sure to test for found and not found in your main program. Read Data Write a function that reads in data from a file using the prototype below....
C Programming Language 2(a) Define a struct with 1 int array named i, 1 float array named f, and one double array named d, each of size M. (b)Declare array x with N of those structs. (c)Write a void function to traverse array x (using a pointer) assigning to each element in each array d (in each struct in array x) the sum of the corresponding elements in arrays i and f (in the same struct). Use 3 pointers (of...
C++ please read all the question Create 3 large arrays to search, where the size will be variable up until it reaches your machines limit. They should contain the same information since we are going to compare 3 different algorithms.You are going to do operational studies on them to determine their order. O(N), O(log(N)), and O(1)? Search algorithms.Linear, Binary, Hash You should know which algorithms are what order,now you are going to prove it. Fill the 3 arrays with the...
The purpose of this assignment is to familiarize you with sort algorithms. Problem Description is as follows: 8. Search Benchmarks Write a program that has at least 20 250 integers stored in an array in ascending order. It should call a function that uses the linear search algorithm to locate one of the values. The function should keep a count of the number of comparisons it makes until it finds the value. The program then should call a function that...
C programming (you don't need to write program) Problem 1 [Linear Search with Early Stop] Below you will find a linear search function with early stop. A linear search is just a naive search - you go through each of the elements of a list one by one. Early stop works only on sorted list. Early stop means, instead of going through whole list, we will stop when your number to search can no longer be possibly found in the...
C++ please read all the question Create 3 large arrays to search, where the size will be variable up until it reaches your machines limit. They should contain the same information since we are going to compare 3 different algorithms.You are going to do operational studies on them to determine their order. O(N), O(log(N)), and O(1)? Search algorithms.Linear, Binary, Hash You should know which algorithms are what order,now you are going to prove it. Fill the 3 arrays with the...