1. Write a program that uses functions to perform the following operations on singly linkedlist.
:
i) Creation ii) Insertion iii) Deletion iv) Traversal
Ans –
#include<stdio.h>
#include<stdlib.h>
struct node{
int data;
struct node*next;
}*head=NULL;
int count()
{
struct node *temp;
int i=1;
temp=head;
while(temp->next!=NULL)
{
temp=temp->next;
i++;
}
return(i);
}
struct node *create(int value)
{
struct node *temp;
temp=(struct node*)malloc(sizeof(struct node));
temp->data=value;
temp->next=NULL;
return temp;
}
void insert_begin(int value)
{
struct node *newnode;
newnode=create(value);
if(head==NULL)
{
head=newnode;
}
else
{
newnode->next=head;
head=newnode;
}
}
void insert_end(int value)
{
struct node *newnode, *temp;
newnode=create(value);
if(head==NULL)
{
head=newnode;
}
else
{
temp=head;
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=newnode;
}
}
void insert_pos(int value,int pos)
{
struct node *newnode, *temp1,*temp2;
int i,c=1;
newnode=create(value);
i=count();
if(pos==1)
insert_begin(value);
else if(pos>i+1)
{
printf("insertion is not posible");
return;
}
else
{
temp1=head;
while(c<=pos-1 && temp1!=NULL)
{
temp2=temp1;
temp1=temp1->next;
c++;
}
newnode->next=temp2->next;
temp2->next=newnode;
}
}
void delete_begin()
{
struct node *temp;
if(head==NULL)
{
printf("deletion is not possible");
}
else
{
temp=head;
head=head->next;
free(temp);
}
}
void delete_end()
{
struct node *temp1,*temp2;
if(head==NULL)
{
printf("deletion is not possible");
}
else
{
temp1=head;
while(temp1->next!=NULL)
{
temp2=temp1;
temp1=temp1->next;
}
temp2->next=NULL;
free(temp1);
}
}
void delete_pos(int pos)
{
struct node *temp1,*temp2;
int i,c=1;
i=count();
if(pos==1)
delete_begin();
else if(pos>i)
{
printf("Deletion is not posible");
return;
}
else
{
temp1=head;
while(c<=pos && temp1->next!=NULL)
{
temp2=temp1;
temp1=temp1->next;
c++;
}
temp2->next=temp1->next;
free(temp1);
}
}
void display()
{
struct node *temp;
if(head==NULL)
{
printf("list is empty");
}
else
{
temp=head;
while(temp->next!=NULL)
{
printf("%d-> ",temp->data);
temp=temp->next;
}
printf("%d",temp->data);
}
}
void main()
{
int ch,pos,value;
do
{
printf("\n1.Insert Begin\n2.Insert End\n3.Insert Position\n4.Delete Beg
in\n5.Delete End\n6.Delete Position\n7.Display\n8.Exit\n");
printf("enter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("enter the value:");
scanf("%d",&value);
insert_begin(value);
break;
case 2: printf("enter value:");
scanf("%d",&value);
insert_end(value);
break;
case 3: printf("enter value:");
scanf("%d",&value);
printf("enter position you want to insert: ");
scanf("%d",&pos);
insert_pos(value,pos);
break;
case 4: delete_begin();
break;
case 5: delete_end();
break;
case 6: printf("enter position you want to delete: ");
scanf("%d",&pos);
delete_pos(pos);
break;
case 7: display();
break;
case 8:break;
default: printf("\nyour choice is wrong!.. ");
}
}while(ch!=8);
}
Output
1.Insert Begin
2.Insert End
3.Insert Position
4.Delete Begin
5.Delete End
6.Delete Position
7.Display
8.Exit
enter your choice:1
enter the value:10
1.Insert Begin
2.Insert End
3.Insert Position
4.Delete Begin
5.Delete End
6.Delete Position
7.Display
8.Exit
enter your choice:2
enter value:30
1.Insert Begin
2.Insert End
3.Insert Position
4.Delete Begin
5.Delete End
6.Delete Position
7.Display
8.Exit
enter your choice:3
enter value:20
enter position you want to insert: 2
1.Insert Begin
2.Insert End
3.Insert Position
4.Delete Begin
5.Delete End
6.Delete Position
7.Display
8.Exit
enter your choice:7
10-> 20-> 30
1.Insert Begin
2.Insert End
3.Insert Position
4.Delete Begin
5.Delete End
6.Delete Position
7.Display
8.Exit
enter your choice:3
enter value:40
enter position you want to insert: 4
1.Insert Begin
2.Insert End
3.Insert Position
4.Delete Begin
5.Delete End
6.Delete Position
7.Display
8.Exit
enter your choice:7
10-> 20-> 30-> 40
1.Insert Begin
2.Insert End
3.Insert Position
4.Delete Begin
5.Delete End
6.Delete Position
7.Display
8.Exit
enter your choice:4
1.Insert Begin
2.Insert End
3.Insert Position
4.Delete Begin
5.Delete End
6.Delete Position
7.Display
8.Exit
enter your choice:5
1.Insert Begin
2.Insert End
3.Insert Position
4.Delete Begin
5.Delete End
6.Delete Position
7.Display
8.Exit
enter your choice:7
20-> 30
1.Insert Begin
2.Insert End
3.Insert Position
4.Delete Begin
5.Delete End
6.Delete Position
7.Display
8.Exit
enter your choice:6
enter position you want to delete: 2
1.Insert Begin
2.Insert End
3.Insert Position
4.Delete Begin
5.Delete End
6.Delete Position
7.Display
8.Exit
enter your choice:7
20
1.Insert Begin
2.Insert End
3.Insert Position
4.Delete Begin
5.Delete End
6.Delete Position
7.Display
8.Exit
enter your choice:8
2. Write a program that implement Stack (its operations) using Array
Ans
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define MAX_SIZE 5
int stack[MAX_SIZE],top=-1;
// Function to check if the stack is empty
bool isEmpty() {
return top == -1;
}
// Function to add an item to the stack
void push(int item) {
if (top == MAX_SIZE - 1) {
printf("Stack Overflow\n");
} else {
stack[++top] = item;
}
}
// Function to remove an item from the stack
int pop() {
if (isEmpty()) {
printf("Stack Underflow\n");
return -1; // Indicating underflow
} else {
return stack[top--];
}
}
// Function to get the top item of the stack
int peek() {
if (isEmpty()) {
printf("Stack is Empty\n");
return -1; // Indicating empty stack
} else {
return stack[top];
}
}
// Function to show all the items from stack
void show()
{
int i;
if (isEmpty())
printf("Stack is Empty\n");
else{
for(i=top;i>-1;i--)
printf("%d\n",stack[i]);
}
}
// Main function
int main() {
int ch,data;
do{
printf("\n1. Push\n2. Pop\n3. Peek\n4. Show\n5. Exit");
printf("\nEnter your choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("Enter data to push: ");
scanf("%d",&data);
push(data);
break;
case 2: printf("Popped: %d\n", pop());
break;
case 3: printf("Top element: %d\n", peek());
break;
case 4: show();
break;
case 5: break;
default: printf("Enter valid choice");
}
}while(ch!=5);
return 0;
}
Output
1. Push
2. Pop
3. Peek
4. Show
5. Exit
Enter your choice: 1
Enter data to push: 10
1. Push
2. Pop
3. Peek
4. Show
5. Exit
Enter your choice: 1
Enter data to push: 20
1. Push
2. Pop
3. Peek
4. Show
5. Exit
Enter your choice: 1
Enter data to push: 30
1. Push
2. Pop
3. Peek
4. Show
5. Exit
Enter your choice: 1
Enter data to push: 40
1. Push
2. Pop
3. Peek
4. Show
5. Exit
Enter your choice: 3
Top element: 40
1. Push
2. Pop
3. Peek
4. Show
5. Exit
Enter your choice: 4
40
30
20
10
1. Push
2. Pop
3. Peek
4. Show
5. Exit
Enter your choice: 1
Enter data to push: 50
1. Push
2. Pop
3. Peek
4. Show
5. Exit
Enter your choice: 1
Enter data to push: 60
Stack Overflow
1. Push
2. Pop
3. Peek
4. Show
5. Exit
Enter your choice: 4
50
40
30
20
10
1. Push
2. Pop
3. Peek
4. Show
5. Exit
Enter your choice: 2
Popped: 50
1. Push
2. Pop
3. Peek
4. Show
5. Exit
Enter your choice: 2
Popped: 40
1. Push
2. Pop
3. Peek
4. Show
5. Exit
Enter your choice: 2
Popped: 30
1. Push
2. Pop
3. Peek
4. Show
5. Exit
Enter your choice: 2
Popped: 20
1. Push
2. Pop
3. Peek
4. Show
5. Exit
Enter your choice: 2
Popped: 10
1. Push
2. Pop
3. Peek
4. Show
5. Exit
Enter your choice: 2
Stack Underflow
Popped: -1
1. Push
2. Pop
3. Peek
4. Show
5. Exit
Enter your choice: 4
Stack is Empty
1. Push
2. Pop
3. Peek
4. Show
5. Exit
Enter your choice: 5
3. Write a program that implements Quick sort sorting methods to sort a given
list of integers in ascending order
Ans
#include <stdio.h>
// Function to swap two elements
void swap(int *a, int *b) {
int t = *a;
*a = *b;
*b = t;
}
void quicksort(int number[25],int first,int last)
{
int i, j, pivot, temp;
if(first<last)
{
pivot=first; // Choose the first element as pivot
i=first;
j=last;
while(i<j)
{
while(number[i]<=number[pivot]&&i<last)
i++;
while(number[j]>number[pivot])
j--;
if(i<j) // swap two elements
{
swap(&number[i], &number[j]);
}
}
// Swap the pivot element with the element at i+1 position
swap(&number[pivot], &number[j]);
// Recursive call on the left of pivot
quicksort(number,first,j-1);
// Recursive call on the right of pivot
quicksort(number,j+1,last);
}
}
int main()
{
int i, count, number[25];
printf("How many elements are u going to enter?: ");
scanf("%d",&count);
for(i=0;i<count;i++)
{
printf("\nEnter %d element: ", i+1);
scanf("%d",&number[i]);
}
quicksort(number,0,count-1);
printf("Order of Sorted elements: ");
for(i=0;i<count;i++)
printf(" %d",number[i]);
return 0;
}
Output
How many elements are u going to enter?: 10
Enter 1 element: 3
Enter 2 element: 6
Enter 3 element: 9
Enter 4 element: 8
Enter 5 element: 5
Enter 6 element: 2
Enter 7 element: 1
Enter 8 element: 4
Enter 9 element: 7
Enter 10 element: 10
Order of Sorted elements: 1 2 3 4 5 6 7 8 9 10
4. Write a program to implement the tree traversal methods using Recursive
Ans
#include <stdio.h>
#include <stdlib.h>
// Definition of a node in a binary tree
struct Node {
int data;
struct Node *left;
struct Node *right;
}*root=NULL;
// Function to create a new node
struct Node* createNode(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
}
void preOrder(struct Node* root)
{
if(root != NULL) {
printf("%d ",root->data);
preOrder(root->left);
preOrder(root->right);
}
}
void inOrder(struct Node* root)
{
if(root != NULL) {
inOrder(root->left);
printf("%d ",root->data);
inOrder(root->right);
}
}
void postOrder(struct Node* root)
{
if(root != NULL) {
postOrder(root->left);
postOrder(root->right);
printf("%d ", root->data);
}
}
int main() {
// Constructing a binary tree
// 1
// / \
// 2 3
// / \
// 4 5
root = createNode(1);
root->left = createNode(2);
root->right = createNode(3);
root->left->left = createNode(4);
root->left->right = createNode(5);
/* Traversals
printf("Pre-order traversal: ");
preOrder(root);
printf("\n"); */
printf("In-order traversal: ");
inOrder(root);
printf("\n");
/* printf("Post-order traversal: ");
postOrder(root);
printf("\n"); */
return 0;
}
Output
Pre-order traversal: 1 2 4 5 3
In-order traversal: 4 2 5 1 3
Post-order traversal: 4 5 2 3 1
5. Write a program to implement the graph traversal methods (Breadth First
Search)
Ans
#include<stdio.h>
// creating queue data structure using arrays
int queue[10];
// defining pointers of the queue to perform pop and push
int front=0,back=0;
// defining push operation on the queue
void push(int var)
{
queue[back] = var;
back++;
}
// defining pop operation on queue
void pop()
{
queue[front] = 0;
front++;
}
// creating a visited array to keep the track of visited nodes
int visited[7] = {0};
int main()
{
int v,n,i,j;
// adjacenty matrix representing graph
int graph[10][10];
printf("Enter the number of vertices: ");
scanf("%d", &n);
printf("Enter graph data in matrix form: \n");
for (i = 0; i < n; i++)
for (j = 0; j < n; j++)
scanf("%d", &graph[i][j]);
// adding a starting node in the list
printf("Enter the starting vertex: ");
scanf("%d", &v);
push(v);
while(front != back)
{
int current = queue[front];
// printing current element
printf("%d ", current);
// popping the front element from the queue
pop();
for(int i=0;i < 6;i++)
{
// adding non-visited connected nodes of the current node to the qu
eue
if((graph[current-1][i] == 1) && (visited[i] == 0))
{
visited[i] = 1; // marking visisted
push(i+1);
}
}
}
return 0;
}
Output
Enter the number of vertices: 6
Enter graph data in matrix form:
0 1 1 0 0 0
1 0 1 0 0 0
1 1 0 1 1 0
0 0 1 0 0 0
0 0 1 0 0 1
0 0 0 0 1 0
Enter the starting vertex: 2
2 1 3 2 4 5 6