[go: up one dir, main page]

0% found this document useful (0 votes)
82 views49 pages

DSUC File - 2 - 2

The document contains an index of 22 programming problems involving arrays, structures, functions, recursion, sorting, and searching algorithms. Example programs are provided for some of the problems, including: 1) A menu-driven program using switch-case statements. 2) Swapping two numbers using call by value and call by reference functions. 3) Generating a Fibonacci series using recursion. 4) Calculating a factorial using recursion. 5) Reading and displaying employee data using a structure. The programs provided demonstrate basic programming concepts like arrays, structures, functions, recursion, and searching/sorting algorithms.

Uploaded by

chetan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
82 views49 pages

DSUC File - 2 - 2

The document contains an index of 22 programming problems involving arrays, structures, functions, recursion, sorting, and searching algorithms. Example programs are provided for some of the problems, including: 1) A menu-driven program using switch-case statements. 2) Swapping two numbers using call by value and call by reference functions. 3) Generating a Fibonacci series using recursion. 4) Calculating a factorial using recursion. 5) Reading and displaying employee data using a structure. The programs provided demonstrate basic programming concepts like arrays, structures, functions, recursion, and searching/sorting algorithms.

Uploaded by

chetan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 49

INDEX

Sr. Name of the Experiment Remarks


No.
1. WAP to create a Menu-driven program using Switch-
Case statement.

2. WAP for swapping of two numbers using Functions with


call by value and call by reference strategies.

3. Generate Fibonacci Series using Recursion.

4. Factorial of a number using Recursion.

5. Program to read and display the information of Employee


using Structure.

6. WAP to read and display array elements with their


addresses.

7. WAP to insert an element at specified position in a


linear array
8. WAP to delete an element at specified position in a
linear array.

9. WAP to find largest of three numbers using Structure.


10. WAP to search an element using Linear Search in a linear
array.

11. WAP to search an element using Binary Search in a linear


array.
12. Write implement the various operations on string such as
length of string concatenation, reverse of a string &
copy of a string to another.

13. WAP to sort a linear array in ascending order using


Bubble Sort.
14. WAP to sort a linear array in ascending order using
Insertion Sort.
15. WAP to sort a linear array in ascending order using
Selection Sort.
16. WAP to sort a linear array in ascending order using Merge
sort.
17. WAPto Perform Push, Pop, operations on the stack using
array.

18. WAP to Convert Infix Expression to postfix form using


Stack.

19. WAP to reverse the String using Stack.


20. WAP to perform different operations with Queue such as
Insert, Delete, Display of elements using array.
22. Program to perform different operations with Queue such
as Insert, Delete, Display of elements using Circular
Queue
22 Program to implement Linear Link List with operations:
Insertion (Beginning, Between, End) Deletion (Beginning,
Between, End),Traverse
PROGRAM :1
 WAP to create a Menu-driven program using Switch-Case
statement.
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>

void main()
{
int ch;
float a,b,res;
clrscr();
printf( Enter two numbers: );
scanf( %f%f ,&a,&b);
printf( nMenun1.Additionn2.Subtractionn3.Multiplicationn4.Division );
printf( nEnter your choice: );
scanf( %d ,&ch);

switch(ch)
{
case 1: res=a+b;
break;
case 2: res=a-b;
break;
case 3: res=a*b;
break;
case 4: res=a/b;
break;
default: printf( Wrong choice!!nPress any key… );
getch();
exit(0);
}printf( nResult=%f ,res);
getch();
}
PROGRAM :2
 WAP for swapping of two numbers using Functions with call by
value and call by reference strategies.

#include <stdio.h>
#include<conio.h>
void swap(int *n1, int *n2);
int main()
{
int num1 = 5, num2 = 10;
clrscr();
interchange(num1,num2);
// swaping using call by value
printf("\n Call by value result");
printf("\n Number 1 : %d",num1);
printf("\n Number 2 : %d",num2);
swap( &num1, &num2); // address of num1 and num2 is passed to
the swap function
printf("\n Call by reference result");
printf("\n Number1 = %d", num1);
printf("\n Number2 = %d", num2);
getch();
return 0;
}
void swap(int * n1, int * n2)
{
int temp; // pointer n1 and n2 points to the address of num1 and
num2 respectively
temp = *n1;
*n1 = *n2;
*n2 = temp;
}

int interchange(int number1,int number2)


{
int temp;
temp = number1;
number1 = number2;
number2 = temp;
return 0;
}
PROGRAM :3
 Generate Fibonacci Series using Recursion.

#include<stdio.h>
#include<conio.h>
int Fibonacci(int);

main()
{
int n, i = 0, c;

scanf("%d",&n);

printf("Fibonacci series\n");

for ( c = 1 ; c <= n ; c++ )


{
printf("%d\n", Fibonacci(i));
i++;
}
getch();
return 0;
}

int Fibonacci(int n)
{
if ( n == 0 )
return 0;
else if ( n == 1 )
return 1;
else
return ( Fibonacci(n-1) + Fibonacci(n-2) );
}
PROGRAM : 4
 Factorial of a number using Recursion
#include<stdio.h>
#include<conio.h>
long factorial(int);

int main()
{
int n;
long f;

printf("Enter an integer to find factorial\n");


scanf("%d", &n);

if (n < 0)
printf("Negative integers are not allowed.\n");
else
{
f = factorial(n);
printf("%d! = %ld\n", n, f);
}
getch();
return 0;
}

long factorial(int n)
{
if (n == 0)
return 1;
else
return(n * factorial(n-1));
}
PROGRAM :5
 Program to read and display the information of Employee
using Structure.

struct employee /*structure declaration*/


{ char name[30];
int empId;
float salary;
};
int main()
{ struct employee emp; /*declare structure
variable*/
printf("\nEnter details :\n"); /*read employee
details*/
printf("Name ?:"); gets(emp.name);
printf("ID ?:"); scanf("%d",&emp.empId);
printf("Salary ?:"); scanf("%f",&emp.salary);
printf("\nEntered detail is:"); /*print employee details*/
printf("Name: %s" ,emp.name);
printf("Id: %d" ,emp.empId);
printf("Salary: %f\n",emp.salary);
return 0;
}
PROGRAM : 6
 WAP to read and display array elements with their
addresses.
#include <stdio.h>

#include <conio.h>

int main()
{
int arr[10]; //declare integer array
int *pa; //declare an integer pointer
int i;
clrscr();
pa=&arr[0]; //assign base address of array
printf("Enter array elements:\n");
for(i=0;i < 5; i++){
printf("Enter element %02d: ",i+1);
scanf("%d",pa+i); //reading through pointer
}
printf("\nEntered array elements are:");
printf("\nAddress\t\tValue\n");
for(i=0;i<5;i++){
printf("%d\t\t%d \n",(pa+i),*(pa+i));
} getch();
Return 0;

}
PROGRAM : 7
 WAP to insert an element at specified position in a
linear array
ALGORITHM:

Let LA be a Linear Array (unordered) with N elements and


K is a positive integer such that K<=N. Following is the
algorithm where ITEM is inserted into the Kth position
of LA

1. Start
2. Set J = N
3. Set N = N+1
4. Repeat steps 5 and 6 while J >= K
5. Set LA[J+1] = LA[J]
6. Set J = J-1
7. Set LA[K] = ITEM
8. Stop
PROGRAM:
#include <stdio.h>
#include<conio.h>

int main()
{
int array[100], position, c, n, value;

printf("Enter number of elements in array\n");


scanf("%d", &n);
printf("Enter %d elements\n", n);

for (c = 0; c < n; c++)


scanf("%d", &array[c]);
printf("Enter the location where you wish to insert an
element\n");
scanf("%d", &position);
printf("Enter the value to insert\n");
scanf("%d", &value);

for (c = n - 1; c >= position - 1; c--)


array[c+1] = array[c];
array[position-1] = value;
printf("Resultant array is\n");
for (c = 0; c <= n; c++)
printf("%d\n", array[c]);

return 0;
}
PROGRAM: 8
 WAP to delete an element at specified position in a
linear array.
ALGORITHM:

Consider LA is a linear array with N elements and K is a


positive integer such that K<=N. Following is the
algorithm to delete an element available at the Kth
position of LA.

1. Start
2. Set J = K
3. Repeat steps 4 and 5 while J < N
4. Set LA[J] = LA[J + 1]
5. Set J = J+1
6. Set N = N-1
7. Stop
PROGRAM:
#include <stdio.h>
#include<conio.h>

int main()
{ int array[100], position, c, n;

printf("Enter number of elements in array\n");


scanf("%d", &n);
printf("Enter %d elements\n", n);

for ( c = 0 ; c < n ; c++ )


{ scanf("%d", &array[c]);
printf("Enter the location where you wish to delete
element\n");
scanf("%d", &position);
}
if ( position >= n+1 )
printf("Deletion not possible.\n");
else
{ for ( c = position - 1 ; c < n - 1 ; c++ )
array[c] = array[c+1];
printf("Resultant array is\n");
for( c = 0 ; c < n - 1 ; c++ )
printf("%d\n", array[c]);
}
getch();
return 0;

PROGRAM :9
 WAP to find largest of three numbers using
Structure.
#include <stdio.h>
#include<conio.h>
struct Num
{
int num1, num2, num3;
};
void main()
{
struct Num NL;
clrscr();
printf("Enter the values of num1, num2 and num3\n");
scanf("%d %d %d", &NL.num1, &NL.num2, &NL.num3);
printf("num1 = %d\tnum2 = %d\tnum3 = %d\n", NL.num1, NL.num2,
NL.num3);
if (NL.num1 > NL.num2)
{
if (NL.num1 > NL.num3)
{
printf("num1 is the greatest among three \n");
}
else
{
printf("num3 is the greatest among three \n");
}
}

else if (NL.num2 > NL.num3)


printf("num2 is the greatest among three \n");
else
printf("num3 is the greatest among three \n");
getch();
}
PROGRAM :10
 WAP to search an element using Linear Search in a
linear array.

ALGORITHM:
Linear Search ( Array A, Value x)

Step 1: Set i to 1
Step 2: if i > n then go to step 7
Step 3: if A[i] = x then go to step 6
Step 4: Set i to i + 1
Step 5: Go to Step 2
Step 6: Print Element x Found at index i and go to step
8
Step 7: Print element not found
Step 8: Exit
PROGRAM:
#include <stdio.h>
#include<conio.h>

int main()
{
int array[100], search, c, n;

printf("Enter the number of elements in array\n");


scanf("%d",&n);

printf("Enter %d integer(s)\n", n);

for (c = 0; c < n; c++)


scanf("%d", &array[c]);

printf("Enter the number to search\n");


scanf("%d", &search);

for (c = 0; c < n; c++)


{
if (array[c] == search) /* if required element found */
{
printf("%d is present at location %d.\n", search, c+1);
break;
}
}
if (c == n)
printf("%d is not present in array.\n", search);
getch();
return 0;
}
PROGRAM : 11
 WAP to search an element using Binary Search in a
linear array.

ALGORITHM:

Binary_search(A, target):

1. lo = 1, hi = size(A)
2. while lo <= hi repeat 3 to 6
3. mid = lo + (hi-lo)/2
4. if A[mid] == target:
return mid
5. else if A[mid] < target:
lo = mid+1
6. else:
hi = mid-1

7. End
PROGRAM:
#include <stdio.h>
#include<conio.h>
int main()
{ int c, first, last, middle, n, search, array[100];
printf("Enter number of elements\n");
scanf("%d",&n);
printf("Enter %d integers\n", n);
for (c = 0; c < n; c++)
scanf("%d",&array[c]);
printf("Enter value to find\n");
scanf("%d", &search);

first = 0;
last = n - 1;
middle = (first+last)/2;

while (first <= last) {


if (array[middle] < search)
first = middle + 1;
else if (array[middle] == search) {
printf("%d found at location %d.\n", search, middle+1);
break;
}
else
last = middle - 1;

middle = (first + last)/2;


}
if (first > last)
printf("Not found! %d is not present in the list.\n",
search);
getch();
return 0;
}
PROGRAM: 12
 Write implement the various operations on string such as
length of string concatenation, reverse of a string &
copy of a string to another.

#include < stdio.h >


#include < stdlib.h >
#include< conio.h >
int find_length(char string[])
{
int len = 0, i;
for (i = 0; string[i] != '\0'; i++) {
len++;
}
return len;
}
void join_strings(char string1[], char string2[])
{
int i, len1, len2;
len1 = find_length(string1);
len2 = find_length(string2);
for (i = len1; i < len1 + len2; i++) {
string1[i] = string2[i - len1];
}
string1[i] = '\0'; //adding null character at the end of input
}

/*returns 0 if thery are same otherwise returns 1*/


int compare_strings(char string1[], char string2[]) {
int len1, len2, i, count = 0;
len1 = find_length(string1);
len2 = find_length(string2);
if (len1 != len2)
return 1;
for (i = 0; i < len1; i++) {
if (string1[i] == string2[i])
count++;
}
if (count == len1)
return 0;
return 1;
}
void copy_string(char destination[], char source[])
{
int len, i;
len = find_length(source);
for (i = 0; i < len; i++) {
destination[i] = source[i];
}
destination[i] = '\0';
}
int main()
{
char string1[20], string2[20];
//string variables declaration with size 20
int choice;
while (1)
{
printf("\n1. Find Length \n2. Concatenate \n3. Compare \n4. Copy
\n5. Exit\n");
printf("Enter your choice: ");
scanf("%d", & choice);
switch (choice)
{
case 1:
printf("Enter the string: ");
scanf("%s", string1);
printf("The length of string is %d", find_length(string1));
break;
case 2:
printf("Enter two strings: ");
scanf("%s%s", string1, string2);
join_strings(string1, string2);
printf("The concatenated string is %s", string1);
break;
case 3:
printf("Enter two strings: ");
scanf("%s%s", string1, string2);
if (compare_strings(string1, string2) == 0) {
printf("They are equal");
} else {
printf("They are not equal");
}
break;

case 4:
printf("Enter a string: ");
scanf("%s", string1);
printf("String1 = %s\n");
printf("After copying string1 to string 2\n");
copy_string(string2, string1);
printf("String2 = %s", string2);
break;
case 5:
exit(0);
}
}
getch();
return 0;
}
PROGRAM: 13
 WAP to sort a linear array in ascending order using
Bubble Sort.

ALGORITHM:

We assume list is an array of n elements. We further assume


that swap function swaps the values of the given array
elements.

BubbleSort(list,n)

1. set i=0
2. repeat step 3 to 5 until i<n
3. set j=0
4. repeat step 5 until ( j <n-i-1 )
5. IF list[j]>list[j+1] then
Set temp = list[j]
Set list[j] = list[j+1]
Set list[j+1] = temp
6. Exit
PROGRAM:

#include <stdio.h>

int main()
{
int array[100], n, c, d, swap;

printf("Enter number of elements\n");


scanf("%d", &n);
printf("Enter %d integers\n", n);

for (c = 0; c < n; c++)


scanf("%d", &array[c]);

for (c = 0 ; c < ( n - 1 ); c++)


{
for (d = 0 ; d < n - c - 1; d++)
{
if (array[d] > array[d+1]) /* For decreasing order use < */
{
swap = array[d];
array[d] = array[d+1];
array[d+1] = swap;
}
}
}
printf("Sorted list in ascending order:\n");
for ( c = 0 ; c < n ; c++ )
printf("%d\n", array[c]);
return 0;
}
PROGRAM :14
 WAP to sort a linear array in ascending order using
Insertion Sort

ALGORITHM:

Insertion(A,n)
1. Set A[0]
2. Repeat step 3 to 5 for k<n
3. Set temp = A[k] and ptr = k -1
4. Repeat while temp < A[ptr]
Set A[ptr +1] = A[ptr]
Set ptr =ptr -1
5. Set A[ptr +1] = temp
6. Exit.
PROGRAM:
#include <stdio.h>

int main()
{
int n, array[1000], c, d, t;
printf("Enter number of elements\n");
scanf("%d", &n);
printf("Enter %d integers\n", n);

for (c = 0; c < n; c++) {


scanf("%d", &array[c]);
}

for (c = 1 ; c <= n - 1; c++) {


d = c;

while ( d > 0 && array[d] < array[d-1]) {


t = array[d];
array[d] = array[d-1];
array[d-1] = t;
d--;
}
}

printf("Sorted list in ascending order:\n");

for (c = 0; c <= n - 1; c++) {


printf("%d\n", array[c]);
}

return 0;
}
PROGRAM : 15
 WAP to sort a linear array in ascending order using
Selection Sort.

ALGORITHM:

SelectionSort(A[],n)
1. Repeat step 2,3 until k < n
2. Call MIN(A,k,n,loc)
3. Set temp = A[k]
A[k]= A[loc]
A[loc] = temp
4. Exit

MIN(A,k,n,loc)
1. Set MIN=A[k] and loc =k
2. Repeat for j =k+1 until j<n
3. If MIN >A[j]
Set MIN = A[j]
Loc = j
4. Return

PROGRAM:
#include <stdio.h>

int main()
{
int array[100], n, c, d, position, swap;

printf("Enter number of elements\n");


scanf("%d", &n);
printf("Enter %d integers\n", n);

for ( c = 0 ; c < n ; c++ )


scanf("%d", &array[c]);

for ( c = 0 ; c < ( n - 1 ) ; c++ )


{
position = c;
for ( d = c + 1 ; d < n ; d++ )
{
if ( array[position] > array[d] )
position = d;
}
if ( position != c )
{
swap = array[c];
array[c] = array[position];
array[position] = swap;
}
}
printf("Sorted list in ascending order:\n");
for ( c = 0 ; c < n ; c++ )
printf("%d\n", array[c]);
return 0;
}
PROGRAM :16
 WAP to sort a linear array in ascending order using
Merge sort.

ALGORITHM:
MergeSort(arr[], l, r)
If r > l
1. Find the middle point to divide the array into two
halves:
middle m = (l+r)/2
2. Call mergeSort for first half:
Call mergeSort(arr, l, m)
3. Call mergeSort for second half:
Call mergeSort(arr, m+1, r)
4. Merge the two halves sorted in step 2 and 3:
Call merge(arr, l, m, r)
PROGRAM:
#include <stdio.h>
#include <conio.h>
#define max 10
int a[11] = { 10, 14, 19, 26, 27, 31, 33, 35, 42, 44, 0 };
int b[10];
void merging(int low, int mid, int high) {
int l1, l2, i;
for(l1 = low, l2 = mid + 1, i = low; l1 <= mid && l2 <= high; i++) {
if(a[l1] <= a[l2])
b[i] = a[l1++];
else
b[i] = a[l2++];
}
while(l1 <= mid)
b[i++] = a[l1++];
while(l2 <= high)
b[i++] = a[l2++];
for(i = low; i <= high; i++)
a[i] = b[i];
}
void sort(int low, int high) {
int mid;
if(low < high) {
mid = (low + high) / 2;
sort(low, mid);
sort(mid+1, high);

merging(low, mid, high);


}
else
{
return;
}
}
int main()
{
int i;
clrscr();
printf("List before sorting\n");
for(i = 0; i <= max; i++)
printf("%d ", a[i]);
sort(0, max);
printf("\nList after sorting\n");
for(i = 0; i <= max; i++)
printf("%d ", a[i]);
getch();
}
PROGRAM : 17
 WAP to Perform Push, Pop, operations on the stack using
array.

ALGORITHM:
PUSH()
Push operation involves a series of steps −
 Step 1 − Checks if the stack is full.
 Step 2 − If the stack is full, produces an error and exit.
 Step 3 − If the stack is not full, increments top to point next empty
space.
 Step 4 − Adds data element to the stack location, where top is pointing.
 Step 5 − Returns success.
POP():
A Pop operation may involve the following steps −
 Step 1 − Checks if the stack is empty.
 Step 2 − If the stack is empty, produces an error and exit.
 Step 3 − If the stack is not empty, accesses the data element at
which top is pointing.
 Step 4 − Decreases the value of top by 1.
 Step 5 − Returns success.
PROGRAM:
#include<conio.h>
#include<stdio.h>
#define max 50
void push();
void pop();
void display();
int menu();
int stack[max], top=0;

void main()
{
int ch;
clrscr();
do{
ch=menu();
switch(ch)
{
case 1: push();
break;
case 2: pop();
break;
case 3: display();
break;
case 4: exit();
default: printf("\nEnter a valid choice!!");
}
}while(1);
}
int menu()
{
int ch;
printf("\nStack");
printf("\n1.Push\n2.Pop\n3.Display\n4.Exit");
printf("\nEnter your Choice:");
scanf("%d",&ch);
return ch;
}
void push()
{
if(top==max)
printf("\nOverflow");
else
{
int element;
printf("\nEnter Element:");
scanf("%d",&element);
printf("\nElement(%d) has been pushed at %d", element, top);
stack[top++]=element;
}
}
void pop()
{
if(top==-1)
printf("\nUnderflow");
else
{
top--;
printf("\nELement has been popped out!");
}
}
void display()
{
if(top==0)
printf("\nStack is Empty!!");
else
{
int i;
for(i=0;i<max;i++)
printf("%d",stack[i]);
}
}
PROGRAM : 18
 WAP to Convert Infix Expression to postfix form using
Stack.
ALGORITM:

Let, X is an arithmetic expression written in infix


notation. This algorithm finds the equivalent postfix
expression Y.

1. Push ( onto Stack, and add ) to the end of X.


2. Scan X from left to right and repeat Step 3 to 6 for
each element of X until the Stack is empty.
3. If an operand is encountered, add it to Y.
4. If a left parenthesis is encountered, push it onto
Stack.
5. If an operator is encountered ,then:
1. Repeatedly pop from Stack and add to Y each
operator (on the top of Stack) which has the same
precedence as or higher precedence than operator.
2. Add operator to Stack.
[End of If]
6. If a right parenthesis is encountered ,then:
1. Repeatedly pop from Stack and add to Y each
operator (on the top of Stack) until a left
parenthesis is encountered.
2. Remove the left Parenthesis.
[End of If]
[End of If]
7. END.
PROGRAM:
#include<stdio.h>
#include<conio.h>
char stack[20];
int top = -1;
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;
if(x == '+' || x == '-')
return 1;
If(x == '*' || x == '/')
return 2;
}
void main()
{ char exp[20];
char *e, x;
clrscr();
printf("Enter the expression :: ");
scanf("%s",exp);
e = exp;
while(*e != '\0')
{ if(isalnum(*e))
printf("%c",*e);
else if(*e == '(')
push(*e);
else if(*e == ')')
{
while((x = pop()) != '(')
printf("%c", x);
}
else
{
while(priority(stack[top]) >=
priority(*e))
printf("%c",pop());
push(*e);}
e++;
}
while(top != -1)
{ printf("%c",pop());
}getch();
}
PROGRAM : 19
 WAP to reverse the String using Stack.
#include <stdio.h>
#include <string.h>
#include<conio.h>
#define max 100
int top,stack[max];
void push(char x) // Push(Inserting Element in stack) operation
{ if(top == max-1){
printf("stack overflow");
}
else {
stack[++top]=x;
}
}
void pop() // Pop (Removing element from stack)
{
printf("%c",stack[top--]);
}

void main()
{
char str[]="KIIT COLLEGE OF ENGINEERING";
int len = strlen(str);
int i;
clrscr();
for(i=0;i<len;i++)
push(str[i]);

for(i=0;i<len;i++)
pop();
getch();
}
PROGRAM : 20
 WAP to perform different operations with Queue such as
Insert, Delete, Display of elements using array.
ALGORITHM:
Insert():
Queues maintain two data pointers, front and rear. Therefore, its
operations are comparatively difficult to implement than that of
stacks.
The following steps should be taken to enqueue (insert) data into a
queue −
 Step 1 − Check if the queue is full.
 Step 2 − If the queue is full, produce overflow error and exit.
 Step 3 − If the queue is not full, increment rear pointer to point the
next empty space.
 Step 4 − Add data element to the queue location, where the rear is
pointing.
 Step 5 − return success.

Delete():
Accessing data from the queue is a process of two tasks − access the
data where front is pointing and remove the data after access. The
following steps are taken to perform dequeue operation −
 Step 1 − Check if the queue is empty.
 Step 2 − If the queue is empty, produce underflow error and exit.
 Step 3 − If the queue is not empty, access the data where front is
pointing.
 Step 4 − Increment front pointer to point to the next available data
element.
 Step 5 − Return success.

PROGRAM:
#include <stdio.h>
#include<conio.h>
#define MAX 50
int queue_array[MAX];
int rear = - 1;
int front = - 1;
main()
{int choice;
clrscr();
while (1)
{ printf("1.Insert element to queue \n");
printf("2.Delete element from queue \n");
printf("3.Display all elements of queue \n");
printf("4.Quit \n");
printf("Enter your choice : ");
scanf("%d", &choice);
switch (choice)
{ case 1:
insert();
break;
case 2:
delete();
break;
case 3:
display();
break;
case 4:
exit(1);
default:
printf("Wrong choice \n");
} /*End of switch*/
} /*End of while*/
getch(); } /*End of main()*/
insert()
{ int add_item;
if (rear == MAX - 1)
printf("Queue Overflow \n");
else
{ if (front == - 1) /*If queue is initially empty */
front = 0;
printf("Inset the element in queue : ");
scanf("%d", &add_item);
rear = rear + 1;
queue_array[rear] = add_item;
} } /*End of insert()*/
delete()
{ if (front == - 1 || front > rear)
{ printf("Queue Underflow \n");
return ;}
else
{ printf("Element deleted from queue is : %d\n",
queue_array[front]);
front = front + 1;
} } /*End of delete() */
display()
{ int i;
if (front == - 1)
printf("Queue is empty \n");
else
{printf("Queue is : \n");
for (i = front; i <= rear; i++)
printf("%d ", queue_array[i]);
printf("\n");
} }
PROGRAM :21
 Program to perform different operations with Queue such
as Insert, Delete, Display of elements using Circular
Queue
#include <stdio.h>
#define size 5
void insertq(int[], int);
void deleteq(int[]);
void display(int[]);
int front = - 1;
int rear = - 1;
int main()
{ int n, ch;
int queue[size];
do
{ printf("\n\n Circular Queue:\n1. Insert \n2. Delete\n3.
Display\n0. Exit");
printf("\nEnter Choice 0-3? : ");
scanf("%d", &ch);
switch (ch)
{ case 1:
printf("\nEnter number: ");
scanf("%d", &n);
insertq(queue, n);
break;

case 2:
deleteq(queue);
break;
case 3:
display(queue);
break;
}
}while (ch != 0);
}
void insertq(int queue[], int item)
{ if ((front == 0 && rear == size - 1) || (front == rear + 1))
{ printf("queue is full");
return; }
else if (rear == - 1)
{ rear++;
front++; }
else if (rear == size - 1 && front > 0)
{ rear = 0; }
else
{ rear++; }
queue[rear] = item;
}
void display(int queue[])
{ int i;
printf("\n");
if (front > rear)
{ for (i = front; i < size; i++)
{ printf("%d ", queue[i]); }
for (i = 0; i <= rear; i++)
printf("%d ", queue[i]); }
else
{ for (i = front; i <= rear; i++)
printf("%d ", queue[i]);
} }
void deleteq(int queue[])
{ if (front == - 1)
{
printf("Queue is empty ");
}
else if (front == rear)
{
printf("\n %d deleted", queue[front]);
front = - 1;
rear = - 1;
}
else
{
printf("\n %d deleted", queue[front]);
front++;
}
}
PROGRAM :22
 Program to implement Linear Link List with operations:
Insertion (Beginning, Between, End) Deletion (Beginning,
Between, End),Traverse
#include <stdio.h>
#include <stdlib.h>

struct node {
int data;
struct node *next;
};

struct node *start = NULL;


void insert_at_begin(int);
void insert_at_end(int);
void traverse();
void delete_from_begin();
void delete_from_end();
int count = 0;

int main () {
int input, data;

for (;;) {
printf("1. Insert an element at beginning of linked list.\n");
printf("2. Insert an element at end of linked list.\n");
printf("3. Traverse linked list.\n");
printf("4. Delete element from beginning.\n");
printf("5. Delete element from end.\n");
printf("6. Exit\n");

scanf("%d", &input);

if (input == 1) {
printf("Enter value of element\n");
scanf("%d", &data);
insert_at_begin(data);
}
else if (input == 2) {
printf("Enter value of element\n");
scanf("%d", &data);
insert_at_end(data);
}
else if (input == 3)
traverse();
else if (input == 4)
delete_from_begin();
else if (input == 5)
delete_from_end();
else if (input == 6)
break;
else
printf("Please enter valid input.\n");
}

return 0;
}

void insert_at_begin(int x) {
struct node *t;

t = (struct node*)malloc(sizeof(struct node));


count++;

if (start == NULL) {
start = t;
start->data = x;
start->next = NULL;
return;
}

t->data = x;
t->next = start;
start = t;
}

void insert_at_end(int x) {
struct node *t, *temp;

t = (struct node*)malloc(sizeof(struct node));


count++;

if (start == NULL) {
start = t;
start->data = x;
start->next = NULL;
return;
}
temp = start;

while (temp->next != NULL)


temp = temp->next;

temp->next = t;
t->data = x;
t->next = NULL;
}

void traverse() {
struct node *t;

t = start;

if (t == NULL) {
printf("Linked list is empty.\n");
return;
}

printf("There are %d elements in linked list.\n", count);

while (t->next != NULL) {


printf("%d\n", t->data);
t = t->next;
}
printf("%d\n", t->data);
}

void delete_from_begin() {
struct node *t;
int n;

if (start == NULL) {
printf("Linked list is already empty.\n");
return;
}

n = start->data;
t = start->next;
free(start);
start = t;
count--;

printf("%d deleted from beginning successfully.\n", n);


}

void delete_from_end() {
struct node *t, *u;
int n;

if (start == NULL) {
printf("Linked list is already empty.\n");
return;
}

count--;

if (start->next == NULL) {
n = start->data;
free(start);
start = NULL;
printf("%d deleted from end successfully.\n", n);
return;
}

t = start;

while (t->next != NULL) {


u = t;
t = t->next;
}

n = t->data;
u->next = NULL;
free(t);

printf("%d deleted from end successfully.\n", n);


}

You might also like