Problem 1
Problem 1
#include<stdio.h>
int main()
int a[num];
scanf("%d",&num);
for(i=0;i<num;i++)
scanf("%d",&a[i]);
scanf("%d",&item);
for(i=0;i<num;i++)
if(item == a[i])
flag=1;
break;
}
}
if(flag==1)
else
OUTPUT: 1)
OUTPUT 2):
4
enter the elements to be searched
#include<stdio.h>
int main()
scanf("%d", &n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
scanf("%d",&search);
first =0;
last= n-1;
while(first<=last)
if(a[mid]<search)
break;
else
last = mid-1;
if(first>last)
return 0;
OUTPUT (1):
//wap to create a linked list and serach an element in the linked list.
#include<stdio.h>
#include<stdlib.h>
struct node
int num;
};
int main()
create(&p);
display(p);
scanf("%d", &item);
result = search(p,item);
if(result)
else
return 0;
while(head!=NULL)
if(head->num== item)
return 1;
head = head->next;
}
return 0;
int c, ch;
do
printf("enter number");
scanf("%d", &c);
temp->num = c;
temp->next = NULL;
if(head == NULL)
head = temp;
else
rear->next = temp;
rear = temp;
printf("do you wish to continue [1/0]");
scanf("%d",&ch);
}while(ch!=0);
printf("\n");
return 0;
while(p!=NULL)
printf("%d\t", p->num);
p = p->next;
printf("\n");
OUTPUT:
Enter number 12
Enter number: 4
Enter number 36
12 4 24 36
#include<stdio.h>
#include<stdlib.h>
struct node
int num;
};
int main()
int n;
create(&p);
display(p);
reverse(&p);
display(p);
return 0;
p = q= r = *head;
p = p->next->next;
q = q->next;
r->next = NULL;
q->next = r;
while(p!=NULL)
r = q;
q = p;
p = p->next;
q->next = r;
}
head = q;
int c, ch;
do
{
printf("enter number");
scanf("%d",&c);
temp->num = c;
temp->next = NULL;
if(head == NULL)
head = temp;
else
rear->next = temp;
rear = temp;
scanf(%d", &ch);
}while(ch!=0);
printf("\n");
head = head->next;
while(head!= NULL)
{
free(temp);
temp = head;
head = head->next;
OUTPUT:
enter number: 1
enter number: 2
enter number: 3
enter number: 4
1 2 3 4
4 3 2 1
PROBLEM – 3(a)
//wap to insert node at the begining of singly linked list.
#include<stdio.h>
#include<stdlib.h>
struct node
int data;
} *head;
void display();
int main()
int n, data;
scanf("%d", &n);
createlist(n);
displaylist();
scanf("%d", %data);
insertatbeg(data);
displaylist();
return 0;
void createlist(int n)
int data, i;
if(head == NULL)
else
scanf("%d", &data);
head->data = data;
head->next = NULL;
temp = head;
for(i=2;i<n;i++)
{
if(newnode = NULL)
break;
else
scanf("%d", &data);
newnode->data = data;
newnode->next = NULL;
temp->next = newnode;
temp = temp->next;
{
struct node *newnode;
if(newnode == NULL)
else
newnode->data = data;
newnode->next = head;
head = newnode;
void displaylist()
if(head == NULL)
printf("list is empty");
else
{
temp = head;
while(temp!=NULL)
temp = temp->next;
OUTPUT:
data = 20
data = 30
data = 40
data = 50
data = 10
PROBLEM – 3(b)
//wap to insert element at the end of linked list.
#include <stdio.h>
#include <stdlib.h>
/* Structure of a node */
struct node {
}*head;
void displayList();
int main()
int n, data;
/*
*/
scanf("%d", &n);
createList(n);
printf("\nData in the list \n");
displayList();
/*
*/
scanf("%d", &data);
insertNodeAtEnd(data);
displayList();
return 0;
/*
*/
void createList(int n)
/*
*/
if(head == NULL)
else
/*
*/
scanf("%d", &data);
/*
*/
if(newNode == NULL)
break;
else
scanf("%d", &data);
temp = temp->next;
/*
* Create a new node and inserts at the end of the linked list.
*/
if(newNode == NULL)
{
printf("Unable to allocate memory.");
else
newNode->next = NULL;
temp = head;
while(temp->next != NULL)
temp = temp->next;
/*
void displayList()
/*
*/
if(head == NULL)
printf("List is empty.");
else
temp = head;
while(temp != NULL)
}
OUTPUT:
Data = 10
Data = 20
Data = 30
Data = 10
Data = 20
Data = 30
Data = 40
PROBLEM – 3(c)
//wap to insert an element after a given node in the linked list.
#include<stdio.h>
#include<stdlib.h>
void randominsert(int);
void create(int);
struct node
int data;
};
void main ()
int choice,item,loc;
do
scanf("%d",&item);
if(head == NULL)
create(item);
}
else
randominsert(item);
scanf("%d",&choice);
}while(choice == 0);
if(ptr == NULL)
printf("\nOVERFLOW\n");
else
ptr->data = item;
ptr->next = head;
head = ptr;
printf("\nNode inserted\n");
int i,loc;
if(ptr == NULL)
printf("\nOVERFLOW");
else
scanf("%d",&loc);
ptr->data = item;
temp=head;
for(i=0;i<loc;i++)
temp = temp->next;
if(temp == NULL)
printf("\ncan't insert\n");
return;
printf("\nNode inserted");
OUTPUT:
12
Node inserted
2
PROBLEM – 4(a)
//write a program to deleter an element at the start of the linked list.
#include <stdio.h>
#include <stdlib.h>
/* Structure of a node */
struct node {
}*head;
void deleteFirstNode();
void displayList();
int main()
int n, choice;
/*
*/
scanf("%d", &n);
createList(n);
printf("\nData in the list \n");
displayList();
scanf("%d", &choice);
if(choice == 1)
deleteFirstNode();
displayList();
return 0;
/*
*/
void createList(int n)
{
int data, i;
/*
*/
if(head == NULL)
else
/*
*/
scanf("%d", &data);
temp = head;
/*
*/
if(newNode == NULL)
break;
else
scanf("%d", &data);
newNode->data = data; // Link the data field of newNode with data
temp = temp->next;
/*
*/
void deleteFirstNode()
if(head == NULL)
{
printf("List is already empty.");
else
toDelete = head;
head = head->next;
free(toDelete);
/*
*/
void displayList()
{
struct node *temp;
/*
*/
if(head == NULL)
printf("List is empty.");
else
temp = head;
while(temp != NULL)
OUTPUT:
Data = 10
Data = 20
Data = 30
Data = 40
Data = 50
Data deleted = 10
Data = 20
Data = 30
Data = 40
Data = 50
PROBLEM- 4(b)
#include <stdlib.h>
/* Structure of a node */
struct node {
}*head;
void deleteLastNode();
void displayList();
int main()
int n, choice;
/*
*/
scanf("%d", &n);
createList(n);
printf("\nData in the list \n");
displayList();
scanf("%d", &choice);
if(choice == 1)
deleteLastNode();
displayList();
return 0;
/*
*/
void createList(int n)
{
int data, i;
/*
*/
if(head == NULL)
else
/*
*/
scanf("%d", &data);
temp = head;
/*
*/
if(newNode == NULL)
break;
else
scanf("%d", &data);
newNode->data = data; // Link the data field of newNode with data
temp = temp->next;
/*
*/
void deleteLastNode()
if(head == NULL)
{
printf("List is already empty.");
else
toDelete = head;
secondLastNode = head;
while(toDelete->next != NULL)
secondLastNode = toDelete;
toDelete = toDelete->next;
if(toDelete == head)
head = NULL;
else
secondLastNode->next = NULL;
}
free(toDelete);
/*
*/
void displayList()
/*
*/
if(head == NULL)
{
printf("List is empty.");
else
temp = head;
while(temp != NULL)
OUTPUT:
Data = 20
Data = 30
Data = 40
Data = 50
Data = 10
Data = 20
Data = 30
Data = 40
PROBLEM - 4(C)
#include <stdlib.h>
/* Structure of a node */
struct node {
} *head;
void displayList();
int main()
int n, position;
/*
*/
createList(n);
displayList();
scanf("%d", &position);
deleteMiddleNode(position);
displayList();
return 0;
/*
*/
void createList(int n)
{
struct node *newNode, *temp;
int data, i;
/*
*/
if(head == NULL)
else
/*
*/
scanf("%d", &data);
/*
*/
if(newNode == NULL)
break;
else
scanf("%d", &data);
temp = temp->next;
/*
*/
int i;
if(head == NULL)
}
else
toDelete = head;
prevNode = head;
prevNode = toDelete;
toDelete = toDelete->next;
if(toDelete == NULL)
break;
if(toDelete != NULL)
if(toDelete == head)
head = head->next;
prevNode->next = toDelete->next;
toDelete->next = NULL;
/* Delete nth node */
free(toDelete);
else
/*
*/
void displayList()
/*
if(head == NULL)
printf("List is empty.");
else
temp = head;
while(temp != NULL)
OUTPUT:
Data = 10
Data = 20
Data = 30
Data = 40
Data = 50
Data = 10
Data = 20
Data = 30
Data = 50
PROBLEM-5
//wap to create a stack using linked list and perform the following
operations
a) PUSH
b) POP
C) PEEK (DISPLAY)
#include <iostream>
struct Node {
int data;
};
newnode->data = val;
newnode->next = top;
top = newnode;
void pop() {
if(top==NULL)
cout<<"Stack Underflow"<<endl;
else {
top = top->next;
void display() {
if(top==NULL)
cout<<"stack is empty";
else {
ptr = top;
ptr = ptr->next;
cout<<endl;
int main() {
cout<<"4) Exit"<<endl;
do {
cin>>ch;
switch(ch) {
case 1: {
cin>>val;
push(val);
break;
case 2: {
pop();
break;
case 3: {
display();
break;
case 4: {
cout<<"Exit"<<endl;
break;
default: {
cout<<"Invalid Choice"<<endl;
}while(ch!=4);
return 0;
OUTPUT:
1) Push in stack
3) Display stack
4) Exit
Enter choice: 1
Enter choice: 1
Enter choice: 1
Enter value to be pushed: 8
Enter choice: 1
Enter choice: 2
Enter choice: 3
Enter choice: 5
Invalid Choice
Enter choice: 4
Exit
PROBLEM -6(a)
//wap to implement the merge sort algorithm,
#include <stdio.h>
#define max 10
int a[11] = { 10, 14, 19, 26, 27, 31, 33, 35, 42, 44, 0 };
int b[10];
for(l1 = low, l2 = mid + 1, i = low; l1 <= mid && l2 <= high; i++) {
b[i] = a[l1++];
else
b[i] = a[l2++];
b[i++] = a[l1++];
b[i++] = a[l2++];
for(i = low; i <= high; i++)
a[i] = b[i];
int mid;
sort(low, mid);
sort(mid+1, high);
} else {
return;
int main() {
int i;
sort(0, max);
OUTPUT:
10 14 19 26 27 31 33 35 42 44 0
0 10 14 19 26 27 31 33 35 42 44
PROBLEM -6(b)
//wap to implement the quick sort algorithm,
#include <stdio.h>
int main()
int list[50];
int size, i;
scanf("%d", &size);
scanf("%d", &list[i]);
printf("\n");
return 0;
pivot = low;
i = low;
j = high;
while (i < j)
i++;
j--;
if (i < j)
{
temp = list[i];
list[i] = list[j];
list[j] = temp;
temp = list[j];
list[j] = list[pivot];
list[pivot] = temp;
quicksort(list, j + 1, high);
OUTPUT:
67
45
24
98
12
38
12 24 38 45 67 98
PROBLEM - 7(a)
//wap to implement insertion sort algorithm.
#include <stdio.h>
int main()
int n, i, j, temp;
int arr[64];
scanf("%d", &n);
scanf("%d", &arr[i]);
j = i;
temp = arr[j];
arr[j] = arr[j-1];
arr[j-1] = temp;
j--;
printf("%d\n", arr[i]);
return 0;
OUTPUT:
Enter 6 integers
461253
4
5
6
PROBLEM - 7(b)
//wap to implement bubble sort algorithm.
#include <stdio.h>
/*
* Main Function
*/
int main()
int n, j, i, swap;
scanf("%d", &n);
int array[n];
scanf("%d", &array[i]);
swap = array[j];
array[j] = array[j+1];
array[j+1] = swap;
printf("%d\n", array[i]);
return 0;
OUTPUT:
Enter 6 integers
2
5
Enter 3 integers
-3
31
66
-3
31
66
Enter 5 integers
9
PROBLEM - 7(c)
//wap to implement selection sort algorithm
#include <stdio.h>
/*
*/
int i, j;
swap(&arr[i], &arr[j]);
int temp;
temp = *a;
*a = *b;
*b = temp;
/*
* Main Function
*/
int main()
scanf("%d", &size);
printf("\n");
scanf("%d", &array[i]);
selectionSort(array, size);
OUTPUT:
Enter 6 numbers
Sorted array is 1 2 3 4 5 6
Enter 3 numbers
-3
31
66
Sorted array is -3 31 66
3. How many numbers you want to sort: 5
Enter 5 numbers
Sorted array is 1 3 6 8 9
PROBLEM - 8
//wap a c program that uses a stack operations to convert a given infix
expression into its postfix equivalent.
#include<stdio.h>
char stack[20];
void push(char x)
stack[++top] = x;
char pop()
if(top == -1)
return -1;
else
return stack[top--];
int priority(char x)
if(x == '(')
return 0;
return 1;
return 2;
main()
char exp[20];
char *e, x;
scanf("%s",exp);
e = exp;
while(*e != '\0')
if(isalnum(*e))
printf("%c",*e);
push(*e);
{
while((x = pop()) != '(')
printf("%c", x);
else
printf("%c",pop());
push(*e);
e++;
while(top != -1)
printf("%c",pop());
OUTPUT:
ab+c*da-+