# Write a C program that uses functions to perform the following operations: Create a `singly linked list` of integer elements. Traverse the above list and display the elements.

#### Algorithm:

```Step 1:  Start
Step 2:  Declare a structure named linked-list
Step 3:  Declare the pointers next, first, fresh, ptr
Step 6:  switch(choice)
Step 7:  if(choice == 1)
7.1: Assign fresh = malloc(size of (node))
7.2: Read the element fresh -> data
7.3: Read the choice where to insert
7.4: switch(choice)
7.4.1: if choice == 1
7.4.2: call the function IBegin()
7.4.3: if choice == 2
7.4.4: call the function Iend()
7.4.5: if choice == 3
7.4.6: call the function Imiddle()
Step 8: if(choice == 2)
8.1: Read the position to delete
8.2: switch(choice)
8.2.1: if choice == 1
8.2.2: call the function DBegin()
8.2.3: if choice == 2
8.2.4: call the function Dend()
8.2.5: if choice == 3
8.2.6: call the function Dmiddle()
Step 9:  if choice == 3
9.1 Call function view
Step 10: if choice == 4
10.1 exit()
Step 11: Start insert function
Step 12: if(first == null)
Step 13: first -> data = e
Step 14: first -> next = null
Step 15: else declare new node
Step 16: fresh -> data = e
Step 17: if choice = 1
Step 18: first -> next = first
Step 19: first = fresh
Step 20: if choice = 2
Step 21: ptr = first
Step 22: ptr -> next = fresh
Step 23: fresh -> next = full
Step 24: if choice = 3
Step 25: Enter the position
Step 26: at p - 1 node
Step 27: fresh -> next = ptr -> next
Step 28: ptr -> next = fresh
Step 29: for delete function
Step 30: if first != null
Step 31: Enter the position to delete
Step 32: if choice = 1
Step 33: d = first -> data
Step 34: first = first -> next
Step 35: if choice = 2
Step 36: ptr = first
Step 37: Traverse to last node
Step 38: d = ptr -> next -> data
Step 39: ptr -> next = ptr -> next -> next
Step 40: Print d value
Step 41: for function view
Step 42: for ptr = first and ptr != null and ptr = ptr -> next
Step 43: print ptr -> data
Step 44: End```

#### Flowchart: #### Program:

```#include<stdio.h>
#include<conio.h>
#include<alloc.h>
#include<stdlib.h>
void create();
void insert();
void del();
void display();
struct node
{
int data;
};
struct node *first = null, *last = null ,*next, *curr, *prev;
int ch;
void main()
{
clrscr();
do
{
printf(“\n 1.create  \n 2.insert \n 3.delete \n 4.exit \n ”);
scanf(“%d”, &ch);
switch(ch)
{
case 1: create();
display();
break;
case 2: insert();
display();
break;
case 3: del();
display();
break;
case 4: exit(0);
}
}
while(ch<=3);
}
void create()
{
curr = (struct node *) malloc(sizeof(struct node));
printf(“Enter the data: ”);
scanf(“%d”, &curr -> data);
first = curr;
last = curr;
}
void insert()
{
int pos, c = 1;
curr=(struct node *)malloc(sizeof(struct node));
printf(“Enter the data:”);
scanf(“%d”, &curr -> data);
printf(“Enter the position:”);
scanf(“%d”, &pos);
if((pos == 1) && (first != null))
{
first = curr;
}
else
{
next = first;
while(c < pos)
{
prev = next;
c++;
}
if(prev == null)
{
printf(“\n Invalid position”);
}
else
{
{
last = curr;
}
}
}
void del()
{
int pos, c = 1;
printf(“Enter the position”);
scanf(“%d”, &pos);
if(first = null)
{
printf(“\n list is empty”);
}
else if(pos == 1) && (first -> link == null)
{
printf(“\n Deleted element is %d \n”, curr -> data);
free(curr);
}
else
{
next = first;
while(c < pos)
{
prev = next;
c++;
}
if(next = null)
{
printf(“\n Invalid position”);
}
else
{
printf(“\n Deleted element is:%d\n”, next -> data);
free(next);
{
last = prev;
}
}
}
}
void display()
{
curr = first;
while(curr != null)
{
printf(“\n %d”, curr -> data);
}
}```

#### Input & Output:

```Singly linked list
1.create
2.insert
3.del
4.exit
Enter the data:2
1.create
2.insert
3.del
4.exit
Enter the data: 4
Enter the position: 2
2
4
1.create
2.insert
3.del
4.exit