Download as pdf or txt
Download as pdf or txt
You are on page 1of 70

DATA STRUCTURES

By Omkar Deshpande
INDEX
Sr. OBJECTIVES
1. Structures: Programs on
a. Details of Book
b. Details of an employee

2. Arrays: Insert, Delete and Modify Operations


on an array

3. Stack: Push and Pop Operation

4. Stack Application:
a. Infix to Postfix Operation
b. Postfix Evaluation
c. Balancing of Parenthesis

5. Recursion:
a. Fibonacci Series
b. Binary Search using recursion and iteration
both.

6. Tower of Hanoi

7. Queue: En-queue and De-queue Operations.

8. Circular Queue: En-queue and De- queue


Operations.

9. Double Ended Queue: En-queue and De-queue


Operation.

10. Singly Linked List


a. Insert from Beginning, End and
Intermediate.
b. Delete from Beginning, End and
Intermediate.
Sr. OBJECTIVES
11. Circular Linked List
a. Insert from Beginning, End and
Intermediate.
b. Delete from Beginning, End and
Intermediate.

12. Doubly Linked List


a. Insert from Beginning, End and
Intermediate.
b. Delete from Beginning, End and
Intermediate.

13. Linked Lists Application:


a. Addition of two given Polynomials

14. Trees:
a. Pre-Order
b. In-Order
c. Post-Order

15. Graphs: Representations


a. Adjacency Matrix
b. Adjacency List
DATA STRUCTURES

The following file consists of all the important


programs, of the subject Data Structures,
implemented in the Practical Lab at MGM’s
College of Engineering, Nanded.

The following programs are implemented under the


Guidance of Bhagyashri Kapre Mam and Nikita
Pande Mam.
Note:
 All the C-Programs within this file, are stored at
GitHub. GitHub is an online Code Management
Platform.
Visit the following link to access all the C-Programs:
https://github.com/omkar98/SEMESTER-3

 For easy understanding, the explanation of all


the programs (Practical Write-Up) along with
Algorithms and C-Codes, can be accessed
from the following blog:
http://engineersplot.blogspot.in/p/3rd-
semester.html
(Go to “Data Structures” section in the blog, to access all
the Practical Explanations of each and every program.)

Keep visiting the GitHub Directory, to get newly added programs or to


get an updated version of previous programs. Also, the corresponding
explanation (Practical Write-Up) of new programs would be uploaded
on the blog “Engineers Plot” (engineersplot.blogspot.in).

Hence, stay updated by visiting these two websites regularly.


OMKAR DESHPANDE | SE-CSE-1 | 25 | STRUCTURES

AIM: To find the area and perimeter of STRUCTURES


a rectangle using pointers.
AIM: A simple program using structure
(The main aim here is to print the values that reads and displays details of a single
of area and perimeter from the main book.
function)
SYNTAX SYNTAX

#include<stdio.h> #include<stdio.h>
#include<conio.h> #include<conio.h>
void area(int *, int *, int *, int *); void main()
void main() {
{ struct book
int l,b, ar, peri; {
clrscr(); int b_id;
printf("Enter length and breadth of char name[50];
rectangle: "); int price;
scanf("%d %d", &l, &b); } b;
area(&l, &b, &ar, &peri); clrscr();
printf("\nIn Main function: \n Area = printf("Enter Book name: ");
%d; Perimeter = %d", ar, peri); gets(b.name);
getch(); //scanf cannot print the entire book
} name, but only prints one word. Hence
/* We use area and perimeter as *area we use gets.
and *peri, because we want the values of printf("Enter Book ID: ");
area and perimeter to be printed in scanf("%d", &b.b_id);
main function. Hence, we return the printf("Enter Price: ");
addresses to main after calculation in scanf("%d", &b.price);
area function */
printf("\nYou have entered the
void area(int *x, int *y, int *ar, int following book:\nBook ID: %d\nName:
*peri) %s\nPrice: Rs.%d\n",b.b_id, b.name,
{ b.price);
*ar=(*x)*(*y); getch();
*peri=(2*(*x) + 2*(*y)); }
printf("\nIn area function: \nArea =
%d; Perimeter %d", *ar, *peri); ========OUTPUT=======
} Enter Book name: Programming in C
Enter Book ID: 203
==========OUTPUT========= Enter Price: 340
Enter length and breadth of rectangle: 3
4 You have entered the following book:
In area function: Book ID: 203
Area = 12; Perimeter 14 Name: Programming in C
In Main function: Price: Rs.340
Area = 12; Perimeter = 14
OMKAR DESHPANDE | SE-CSE-1 | 25 | STRUCTURES
AIM: A simple program using structure
that reads and displays details of a single ========OUTPUT=======
employee (using structures) Enter Employee ID and Employee
Name: 203 Deshpande
SYNTAX Enter Deshpande's Designation:
Engineer
#include<stdio.h> Enter Salary: 50000
#include<conio.h>
ID Name Designation Salary
void main() 203. Deshpande Engineer Rs.50000
{
struct emp
{ AIM: To add two numbers using
int id; structures.
char nm[50];
char desg[50] ;
long int sal; SYNTAX
} e;
clrscr(); #include<stdio.h>
printf("\nEnter Employee ID and #include<conio.h>
Employee Name: ");
scanf("%d", &e.id); void main()
gets(e.nm); {
// printf("Enter Employee name: "); clrscr();
second printf() struct add
{
/*If we use printf before gets(as in this int a,b,c; // structure members
case) then the program doesn't run } s; // structure variable
properly, i.e the compiler skips the next // struct add s;
printf() and gets() functions. printf("Enter two numbers: ");
Problem: gets() only reads the input for scanf("%d %d", &s.a, &s.b);
in the intial statement and skips in case s.c=s.a+s.b;
it is used for the next statements. printf("Addition = %d", s.c);
Solution: To read input using gets() for getch();
subsequent statements, use gets() }
directly after the scanf() statement. */
========OUTPUT=======
printf("Enter %s's Designation: ", Enter two numbers: 2 3
e.nm); Addition = 5
scanf("%s", &e.desg);
printf("Enter Salary: ");
scanf("%ld", &e.sal);
printf("ID \t
Name\t\tDesignation\tSalary");
printf("\n%d.\t%s\t%s%ld", e.id,
e.nm, e.desg, e.sal);
getch(); }
OMKAR DESHPANDE | SE-CSE-1 | 25 | STRUCTURES
AIM: A program (using structures) that ========OUTPUT========
reads and displays details of as many Enter the number of Books: 10
books as user wants and later displays Enter Book Name: C_Language
only those books, which lie within a Enter Book ID and its Price: 1 1900
certain range of user provided price. Enter Book Name: Advanced_C
Enter Book ID and its Price: 2 2000
SYNTAX Enter Book Name: C++_Language
#include<stdio.h> Enter Book ID and its Price: 3 2300
#include<conio.h> Enter Book Name: Advanced_C++
void main() Enter Book ID and its Price: 4 2105
{ int i, n,max, min; Enter Book Name: Web_Designing
clrscr(); Enter Book ID and its Price: 5 2500
struct book Enter Book Name: Web_Develop
{ int id, price; Enter Book ID and its Price: 6 970
char name[50]; Enter Book Name: JAVA_Language
} b[50]; //Let the max be 10 books. Enter Book ID and its Price: 7 1700
printf("Enter the number of Books: "); Enter Book Name: Advanced_Java
scanf("%d", &n); Enter Book ID and its Price: 8 3001
for(i=0; i<n; i++) Enter Book Name: HTML_Courses
{ Enter Book ID and its Price: 9 2999
printf("Enter Book Name: "); Enter Book Name: Info_Tech
scanf("%s",&b[i].name); Enter Book ID and its Price: 10 3000
printf("Enter Book ID and its price:
"); ID Book Name Price
scanf("%d %d", &b[i].id, 1. C_Language Rs.1900
&b[i].price); 2. Advanced_C Rs.2000
} 3. C++_Language Rs.2300
printf("\nID\tBook Name\tPrice\n"); 4. Advanced_C++ Rs.2105
for(i=0; i<n; i++) 5. Web_Designing Rs.2500
{ 6. Web_Develop Rs.970
printf("%d.\t%s\tRs.%d\n", 7. JAVA_Language Rs.1700
b[i].id,b[i].name,b[i].price); 8. Advanced_Java Rs.3001
} 9. HTML_Courses Rs.2999
printf("\nEnter the range of price of 10. Info_Tech Rs.3000
books, you are searching for: ");
scanf("%d-%d", &min, &max); Enter the range of price of books, you
printf("\nFollowing books lie within are searching for: 2000-3000
the range of Rs.%d to Rs.%d: Following books lie within the range of
",min,max); Rs.2000 to Rs.3000:
printf("\nID\tBook Name\tPrice\n"); ID Book Name Price
for(i=0; i<n; i++) 2. Advanced_C Rs.2000
{ if(b[i].price>=min && 3. C++_Language Rs.2300
b[i].price<=max) 4. Advanced_C++ Rs.2105
{ printf("\n%d.\t%s\tRs.%d\n", 5. Web_Designing Rs.2500
b[i].id,b[i].name,b[i].price); } 9. HTML_Courses Rs.2999
} 10. Info_Tech Rs.3000
getch(); }
OMKAR DESHPANDE | SE-CSE-1 | 25 | STRUCTURES

Call by Value DIFFERENCE Call by Reference


AIM : Perform swapping using regular AIM : Perform swapping using pointers.
fuctions (without using pointers).

SYNTAX SYNTAX

#include<stdio.h> #include<stdio.h>
#include<conio.h> #include<conio.h>
void swap(int,int); void swap(int *p , int *q);//With
Argument No Return
void main() void main()
{ {
clrscr(); clrscr();
int a,b; int a,b,d;
printf("Enter two Numbers to swap: printf("Enter two Numbers to swap:
"); ");
scanf("%d %d", &a, &b); scanf("%d %d", &a, &b);
printf("In main function = %d %d", swap(&a,&b);
a,b); printf("\nIn main function = %d %d",
swap(a,b); a,b);
getch(); getch();
} }
void swap(int p, int q)
{ // In swapping program
int temp; void swap(int *p, int *q)
temp=p; {
p=q; int temp;
q=temp; temp=*p;
printf("\nIn swap function = %d %d", *p=*q;
p,q); *q=temp;
/* We cannot use two return values printf("In swap function = %d %d",
return(p); *p, *q);
return(q); */ }
}
=========OUTPUT======== =========OUTPUT==========
Enter two Numbers to swap: 10 20 Enter two Numbers to swap: 10 20
In main function = 10 20 In swap function = 20 10
In swap function = 20 10 In main function = 20 10

 The changes made in the variables by the  The changes made in the variables by the
function is not reflected in the main function. function is reflected in the main function.
 Values of actual parameters (i.e a, b) are  The addresses (i.e &a &b) are passed to the
passed to the function. function.
 We can return only one value to the calling  We can return more than one values to the
function. calling function (because we use pointers)
SYNTAX
Name: Omkar Deshpande #include<stdio.h>
Class: SE CSE 1 #include<conio.h>
Roll No: 25 void main()
Subject: Data Structures {
int ary[30], ele, i, num, pstn, ch;
clrscr();
printf("Note: Only 30 elements can be
inserted!\n");
printf("Enter the number of elements you
want in the array: ");
scanf("%d", &num);
/*Scans the elements*/
for(i=0;i<num;i++)
{
scanf("\t%d", &ary[i]);
AIM: }
• To insert an element in an array start: /*Goto statement*/
• To delete an element from the printf("\nSelect an Operation to be
array performed:\n\t[1]
INSERTION\n\t[2]
• To modify(over-write) an DELETION\n\t[3] MODIFIY\n\t[4]
element in the array EXIT");
printf("\nYour Choice: ");
scanf("%d", &ch);
/*A switch case is used to toggle between
various operations.*/
switch(ch)
{
case 1: /*To insert an element*/
printf("Enter the element you want
to insert: ");
scanf("\n%d", &ele);

printf("Enter the position to insert


the element: ");
scanf("%d", &pstn);

for(i=num;i>=pstn;i--)
{
ary[i]=ary[i-1];
}
num++;
ary[pstn-1]=ele;
for(i=0;i<num;i++)
{
printf("\t%d", ary[i]); }
/* To go back to the previous Menu we ==========Output========
use goto statement*/ Note: Only 30 elements can be inserted!
goto start; Enter the number of elements you want in
the array: 5
case 2: /*To Delete an element*/ 10
printf("Enter the position of 20
element you want to delete: "); 30
scanf("%d", &pstn); 40
for(i=pstn;i<num;i++) 50
{ Select an Operation to be performed:
ary[i-1]=ary[i]; [1] INSERTION
} [2] DELETION
num--; [3] MODIFIY
for(i=0;i<num;i++) [4] EXIT
{ Your Choice: 1
printf("\t%d", ary[i]); Enter the element you want to insert: 90
} Enter the position to insert the element: 3
goto start; 10 20 90 30 40 50
Select an Operation to be performed:
case 3: /*To Modify an Element*/ [1] INSERTION
printf("Enter the position of the [2] DELETION
element you want to modify: "); [3] MODIFIY
scanf("%d", &pstn); [4] EXIT
printf("Enter the element you Your Choice: 2
want to insert in %d position: ", Enter the position of element you want to
pstn); delete: 2
scanf("%d", &ele); 10 90 30 40 50
ary[pstn-1]=ele; Select an Operation to be performed:
printf("New Array is: "); [1] INSERTION
for(i=0;i<num;i++) [2] DELETION
{ [3] MODIFIY
printf("\t%d", ary[i]); [4] EXIT
} Your Choice: 3
goto start; Enter the position of the element you want
to modify: 2
case 4: break; Enter the element you want to insert in 2
} position: 20
printf("\nProgram Terminated."); New Array is: 10 20 30 40 50
getch(); Select an Operation to be performed:
} [1] INSERTION
[2] DELETION
[3] MODIFIY
[4] EXIT
Your Choice: 4
Program Terminated.
AIM: A simple program using structure AIM: A simple program using structure
that reads and displays details of a single that reads and displays details of a single
book. employee (using structures)

SYNTAX SYNTAX

#include<stdio.h> #include<stdio.h>
#include<conio.h> #include<conio.h>
void main()
{ void main()
struct book {
{ struct emp
int b_id; {
char name[50]; int id;
int price; char nm[50];
} b; char desg[50] ;
clrscr(); long int sal;
printf("Enter Book name: "); } e;
gets(b.name); clrscr();
//scanf cannot print the entire book printf("\nEnter Employee ID and
name, but only prints one word. Hence Employee Name: ");
we use gets. scanf("%d", &e.id);
printf("Enter Book ID: "); gets(e.nm);
scanf("%d", &b.b_id); printf("Enter %s's Designation: ",
printf("Enter Price: "); e.nm);
scanf("%d", &b.price); scanf("%s", &e.desg);
printf("Enter Salary: ");
printf("\nYou have entered the scanf("%ld", &e.sal);
following book:\nBook ID: %d\nName: printf("ID \t
%s\nPrice: Rs.%d\n",b.b_id, b.name, Name\t\tDesignation\tSalary");
b.price); printf("\n%d.\t%s\t%s%ld", e.id,
getch(); e.nm, e.desg, e.sal);
} getch(); }

========OUTPUT======= ========OUTPUT=======
Enter Book name: Programming in C Enter Employee ID and Employee
Enter Book ID: 203 Name: 203 Deshpande
Enter Price: 340 Enter Deshpande's Designation:
Engineer
You have entered the following book: Enter Salary: 50000
Book ID: 203
Name: Programming in C ID Name Designation Salary
Price: Rs.340 203. Deshpande Engineer Rs.50000

Omkar Deshpande | SE-CSE1 | 25 | DS


AIM: A program (using structures) that ========OUTPUT========
reads and displays details of as many Enter the number of Books: 10
books as user wants and later displays Enter Book Name: C_Language
only those books, which lie within a Enter Book ID and its Price: 1 1900
certain range of user provided price. Enter Book Name: Advanced_C
Enter Book ID and its Price: 2 2000
SYNTAX Enter Book Name: C++_Language
#include<stdio.h> Enter Book ID and its Price: 3 2300
#include<conio.h> Enter Book Name: Advanced_C++
void main() Enter Book ID and its Price: 4 2105
{ int i, n,max, min; Enter Book Name: Web_Designing
clrscr(); Enter Book ID and its Price: 5 2500
struct book Enter Book Name: Web_Develop
{ int id, price; Enter Book ID and its Price: 6 970
char name[50]; Enter Book Name: JAVA_Language
} b[50]; //Let the max be 10 books. Enter Book ID and its Price: 7 1700
printf("Enter the number of Books: "); Enter Book Name: Advanced_Java
scanf("%d", &n); Enter Book ID and its Price: 8 3001
for(i=0; i<n; i++) Enter Book Name: HTML_Courses
{ Enter Book ID and its Price: 9 2999
printf("Enter Book Name: "); Enter Book Name: Info_Tech
scanf("%s",&b[i].name); Enter Book ID and its Price: 10 3000
printf("Enter Book ID and its price:
"); ID Book Name Price
scanf("%d %d", &b[i].id, 1. C_Language Rs.1900
&b[i].price); 2. Advanced_C Rs.2000
} 3. C++_Language Rs.2300
printf("\nID\tBook Name\tPrice\n"); 4. Advanced_C++ Rs.2105
for(i=0; i<n; i++) 5. Web_Designing Rs.2500
{ 6. Web_Develop Rs.970
printf("%d.\t%s\tRs.%d\n", 7. JAVA_Language Rs.1700
b[i].id,b[i].name,b[i].price); 8. Advanced_Java Rs.3001
} 9. HTML_Courses Rs.2999
printf("\nEnter the range of price of 10. Info_Tech Rs.3000
books, you are searching for: ");
scanf("%d-%d", &min, &max); Enter the range of price of books, you
printf("\nFollowing books lie within are searching for: 2000-3000
the range of Rs.%d to Rs.%d: Following books lie within the range of
",min,max); Rs.2000 to Rs.3000:
printf("\nID\tBook Name\tPrice\n"); ID Book Name Price
for(i=0; i<n; i++) 2. Advanced_C Rs.2000
{ if(b[i].price>=min && 3. C++_Language Rs.2300
b[i].price<=max) 4. Advanced_C++ Rs.2105
{ printf("\n%d.\t%s\tRs.%d\n", 5. Web_Designing Rs.2500
b[i].id,b[i].name,b[i].price); } 9. HTML_Courses Rs.2999
} 10. Info_Tech Rs.3000
getch(); }
Omkar Deshpande | SE-CSE1 | 25 | DS
AIM: To implement stack data if(top==-1)
structure and its Operations (i.e {
PUSH and POP) printf("\nStack is already
empty.\nPlease push an element first.\n");
}
SYNTAX: else
#include<stdio.h> {
#include<conio.h> x=a[top];
# define MAX 5 --top;
printf("%d is POPed out of
/* Drawback: Only 5 elements can be stack.\n",x);
inserted at MAX in this array. We need if(top>=0)
to use dynamic memory allocation, in {
order to dynamically give size of array printf("The element in the stack
at the time of execution. */ are:");
for(i=top;i>=0; i--)
/* Global Declaration of variables */ {
int a[4],ch,n,top=-1,ele,i; printf("\n| %d |\n", a[i]);
}
/* Push Function pushes the element in }
stack. */ }
int push (int a[], int ele) }
{
if(top==MAX-1) void main()
{ {
printf("\nStack is full\n\n"); clrscr();
return (1); /* We use Goto statement to return back
} to the main menu */
else start:
{ printf("Select the operation you would like
++top; to perform: ");
a[top]=ele; printf("\n\t[1]PUSH\n\t[2]POP\n\t[3]EXIT\
printf("%d is inserted\n", ele); n");
printf("Elements in stack are:\n "); printf("Your Choice: ");
for(i=top;i>=0; i--) scanf("%d", &ch);
{ switch(ch)
printf("| %d |\n", a[i]); {
} case 1:
return(0); printf("Enter the element you want
} to push in the stack: ");
} scanf("%d", &ele);
push(a,ele);
/* Pop Function pops out the top element goto start;
in stack */
void pop (int a[]) case 2:
{ pop(a);
int x; goto start;
case 3: break; 2. When the user wants to POP out all
the elements from the stack.
default:
printf("\nPlease enter a valid input"); Your Choice: 2
} 45 is POPed out of stack.
printf("Program Terminated\n"); The element in the stack are:
getch(); | 23 |
} Select the operation you would like to
perform:
===========OUTPUT========== [1]PUSH
[2]POP
[3]EXIT
Your Choice: 2
1. The user enters elements one by one 23 is POPed out of stack.
in the stack. Select the operation you would like to
perform:
Your Choice: 1 [1]PUSH
Enter the element you want to push in [2]POP
the stack: 33 [3]EXIT
33 is inserted Your Choice: 2
Elements in stack are:
| 33 | Stack is already empty.
| 20 | Please push an element first.
| 22 |
| 45 |
| 23 |
Select the operation you would like to 3. Hence, Push and Pop operation is
perform: done. The user decides to exit the
[1]PUSH program:
[2]POP
[3]EXIT Select the operation you would like to
Your Choice: 1 perform:
Enter the element you want to push in [1]PUSH
the stack: 45 [2]POP
[3]EXIT
Stack is full Your Choice: 3
Program Terminated

Name: Omkar Deshpande


Class: SE CSE 1
Roll No: 25
Subject: Data Structures
DESHPANDE OMKAR | CLASS: SE CSE 1 | ROLL NO: 23 | DATA STRUCTURES
AIM: To display operators AIM: To convert given Infix
according to their precedence. expression to Postfix.

PROGRAM: PROGRAM:

#include<stdio.h> /* This program converts infix expression to


#include<conio.h> postfix expression.
void main() * This program assume that there are Five
{ operators: (*, /, +, -,^)
int i,j; in infix expression and operands can
char stack[10]; be of single-digit only.
char p[10]={'(',')','^','%','/','*','+','-'}; * This program will not work for fractional
clrscr(); numbers.
printf("Enter the operator: "); * Further this program does not check
for( i=0;i<=8;i++) whether infix expression is
{ valid or not in terms of number of operators
scanf("%c",&stack[i]); and operands.*/
}
for(i=0;i<=8;i++) #include<stdio.h>
{ #include<stdlib.h> /* for exit() */
for(j=0;j<=8;j++) #include<ctype.h> /* for isdigit(char ) */
{ #include<string.h>
if(p[i]==stack[j])
{ #define SIZE 100
printf("%c\t",stack[j]);
} /* declared here as global variable because
} stack[]
} * is used by more than one fucntions */
getch(); char stack[SIZE];
} int top = -1;
=============Output========
Enter the operator: +-)(*^%/ /* define push operation */
( ) ^ % / * +
- void push(char item)
{
if(top >= SIZE-1)
{
printf("\nStack Overflow.");
}
else
{
top = top+1;
stack[top] = item;

1|Page
DESHPANDE OMKAR | CLASS: SE CSE 1 | ROLL NO: 23 | DATA STRUCTURES
} }
} else
{
/* define pop operation */ return 0;
char pop() }
{ }
char item ;
/* define fucntion that is used to assign
if(top == -1) precendence to operator.
{ * Here ^ denotes exponent operator.
printf("stack under flow: * In this fucntion we assume that higher
invalid infix expression"); integer value
getchar(); * means higher precendence */
/* underflow may occur for
invalid expression */ int precedence(char symbol)
/* where ( and ) are not {
matched */ if(symbol == '^')/* exponent
exit(1); operator, highest precedence*/
} {
else return(3);
{ }
item = stack[top]; else if(symbol == '*' || symbol ==
top = top-1; '/')
return(item); {
} return(2);
} }
else if(symbol == '+' || symbol == '-
/* define function that is used to ') /* lowest precedence */
determine whether any symbol is {
operator or not return(1);
(that is symbol is operand) }
* this fucntion returns 1 if symbol is else
opreator else return 0 */ {
return(0);
int is_operator(char symbol) }
{ }
if(symbol == '^' || symbol == '*' ||
symbol == '/' || symbol == '+' || symbol void InfixToPostfix(char infix_exp[],
=='-') char postfix_exp[])
{ {
return 1; int i, j;

2|Page
DESHPANDE OMKAR | CLASS: SE CSE 1 | ROLL NO: 23 | DATA STRUCTURES
char item; }
char x; push(x);
/* because just above
push('('); /* push while loop will terminate we have
'(' onto stack */ oppped one extra item
strcat(infix_exp,")"); /* for which condition
add ')' to infix expression */ fails and loop terminates, so that one*/

i=0; push(item);
j=0; /* push current oprerator symbol onto
item=infix_exp[i]; /* initialize stack */
before loop*/ }
else if(item == ')') /* if
while(item != '\0') /* run loop current symbol is ')' then */
till end of infix expression */ {
{ x = pop(); /*
if(item == '(') pop and keep popping until */
{ while(x != '(')
push(item); /* '(' encounterd */
} {
else if( isdigit(item) || postfix_exp[j] =
isalpha(item)) x;
{ j++;
postfix_exp[j] = item; x = pop();
/* add operand symbol to postfix expr */ }
j++; }
} else
else if(is_operator(item) == { /* if current symbol is
1) /* means symbol is operator */ neither operand not '(' nor ')' and nor
{ operator */
x=pop(); printf("\nInvalid infix
while(is_operator(x) == Expression.\n"); /* the it is illegeal
1 && precedence(x)>= symbol */
precedence(item)) getchar();
{ exit(1);
postfix_exp[j] = }
x; /* so pop all higher i++;
precendence operator and */
j++;
x = pop(); item = infix_exp[i]; /* go to
/* add them to postfix expresion */ next symbol of infix expression */

3|Page
DESHPANDE OMKAR | CLASS: SE CSE 1 | ROLL NO: 23 | DATA STRUCTURES
} /* while loop ends here */ * */
if(top>0) printf("ASSUMPTION: The infix
{ expression contains single letter variables
printf("\nInvalid infix and single digit constants only.\n");
Expression.\n"); /* the it is illegeal printf("\nEnter Infix expression :
symbol */ ");
getchar(); gets(infix);
exit(1);
} InfixToPostfix(infix,postfix);
if(top>0) /* call to convert */
{ printf("Postfix Expression: ");
printf("\nInvalid infix puts(postfix); /* print
Expression.\n"); /* the it is illegeal postfix expression */
symbol */
getchar(); return 0;
exit(1); }
}

postfix_exp[j] = '\0'; /* add sentinel


else puts() fucntion */ ==========OUTPUT========
/* will print entire postfix[] array
upto SIZE */
ASSUMPTION: The infix expression
} contains single letter variables and
single dig
/* main function begins */ it constants only.
int main() Enter Infix expression : (A+B)*C
{ Postfix Expression: AB+C*
char infix[SIZE], postfix[SIZE];
/* declare infix string and postfix string
ASSUMPTION: The infix expression
*/
contains single letter variables and
/* why we asked the user to enter single dig
infix expression it constants only.
* in parentheses ( ) Enter Infix expression : (A+B)
* What changes are required in Postfix Expression: AB+
porgram to
* get rid of this restriction since it
is not
* in algorithm

4|Page
OMKAR U. DESHPANDE | SE-CSE-1 | 25
AIM: Write a program to
/*------PUSH Function-----*/
evaluate a postfix
void push(int value)
expression {
++top;
PROGRAM
stack[top]=value;
}
#include<stdio.h> /*------POP Function------*/
#include<conio.h> int pop()
#include<string.h> {
#include<math.h> int x = stack[top];
--top;
/* Let the max value of the arrays
we use be 100 */
return(x);
#define MAX 100 }
/*------IsOperand Function---*/
/* Declare few global variables /*This function returns 1(if
which can be used by more than 1 operand) else 0*/
function */ int isOperand(char symbol)
int stack[MAX], top=-1; {
if(symbol>='a' && symbol<='z')
/* We need the following return 1;
operations to perform PostFix else
Evaluation return 0;
1) PUSH Function }
2) POP Function /*-----IsOperator Function ---*/
3) IsOperand Function /*This Function returns 1(if
4) IsOperator Function operator) else returns 0*/
5) Operation Function (Optional, int isOperator(char symbol)
can also be included in the {
main()) if(symbol=='^' || symbol=='*' ||
*/ symbol=='/' || symbol=='+' ||
symbol=='-')
/* We don’t consider the case to print
return 1;
stack overflow condition or
underflow condition, as this will be else
printed in the output along with the return 0;
Postfix expression.*/ }
OMKAR U. DESHPANDE | SE-CSE-1 | 25
/*---------MAIN Function------*/ opr1=pop();
void main() switch(symbol)
{ {
/* case '^':
STEP 1: Get the postfix expression result=pow(opr1,opr2);break;
STEP 2: Evaluate each postfix
case '*':
symbol, if Operator or Operand
2.1: If Operand, ask user for the result=opr1*opr2;break;
value of operand case '/':
2.2: 1: If Operator, pop first 2 result=opr1/opr2;break;
elements from the postfix case '+': result=opr1+opr2;
2.2.1: Perform the operation break;
between the 2 operands case '-': result=opr1-opr2;
2.2.2: Store it back into the stack break;
STEP 3: Repeat Step 2 till the end of
}
Expression.
STEP 4: display final Result. push(result);
*/ }
char postfix[MAX],symbol; i++;//incrementing to check for
int i=0, result, opr1, opr2, x, another postfix symbol.
res_final; }//While loop ending
clrscr(); /*Once all the symbols are
scanned, we pop the final result*/
printf("\nEnter the postfix
res_final=pop();
Expression: ");
printf("\nThe result of the
gets(postfix);
Postfix Expression is: %d",
while(postfix[i]!='\0')
res_final);
{
getch();
symbol=postfix[i];
}
if(isOperand(symbol)==1)
{
printf("\nEnter the value of OUTPUT
%c: ", symbol); Enter the postfix Expression:
scanf("%d", &x); abc*+
push(x); Enter the value of a: 1
} Enter the value of b: 2
if(isOperator(symbol)==1) Enter the value of c: 3
{
The result of the Postfix
opr2=pop();
Expression is: 7
Omkar Deshpande | SE-CSE-1 | 25 | DATA STRUCTURES
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<string.h>
#define MAX 50
AIM: Write a program to /* Declare few global variables
check imbalance of which can be used by more than 1
parenthesis using stack. function */
char stack[MAX];
int top=-1;
/*-------PUSH Function------*/
void push(char sym)
{
To execute the program as
++top;
required, we need to consider stack[top]=sym;
the following main points: }
• Number of opening /*-------POP Function------*/
parenthesis('{') must be same int pop()
as number of closing {
parenthesis('}'). if(top==-1)
• For every '}', there must {
return 0;
be a corresponding '{' before. }
• At any moment of time else
number of '{' must be >= {
number of '}'. stack[top];
--top;
return 1;
}
}
/*-------Check Function------*/
void check(char exp[])
{
int length;
length = strlen(exp);
for(int i=0; i<length; i++)
{
Omkar Deshpande | SE-CSE-1 | 25 | DATA STRUCTURES
if(exp[i]=='{')
{ OUTPUT
push(exp[i]); Enter the expression:
} {a+b{c/d}}
if(exp[i]=='}') A closing bracket(at position 9)
{ has a balancing opening bracket.
int x; A closing bracket(at position 10)
x=pop(); has a balancing opening bracket.
if(x==1)
{ Enter the expression:
printf("A closing bracket(at
position %d) has a balancing
{a+b{c/d}}*d}
A closing bracket(at position 9)
opening bracket.\n", i+1);
has a balancing opening bracket.
}
A closing bracket(at position 10)
if(x==0)
has a balancing opening bracket.
{
There is no opening bracket for a
printf("There is no opening
closing bracket(of Postion-13).
bracket before a closing bracket
(of Postion-%d).\n",i+1); Enter the expression:
} {d*{a+b{c/d}}
} A closing bracket(at position 12)
}/*For loop Termination*/ has a balancing opening bracket.
if (pop()==1) A closing bracket(at position 13)
{ has a balancing opening bracket.
printf("There is/are extra There is/are extra opening
opening bracket(s).\n") ; bracket(s).
}
}
Enter the expression: }}a+b{{
/*-------MAIN Function------*/
There is no opening bracket
void main()
before a closing bracket(of
{
Postion-1).
char exp[MAX];
There is no opening bracket
clrscr();
before a closing bracket(of
printf("Enter the expression: ");
Postion-2).
gets(exp);
There is/are extra opening
check(exp);
bracket(s).
getch(); }
OMKAR DESHPANDE | ROLL NO: 25 | SE-CSE-1 | DS

AIM: To find the sum of


Fibonacci series using
recursion function. /*RECURSION FUNCTION*/
int rec(int f1,int f2,int n)
PROGRAM: {
sum=f1+f2;
if(sum<n)
#include<stdio.h>
{
#include<conio.h>
printf(" %d ", sum);
/*Function Prototype*/
rec(f2,sum,n);
int rec(int,int,int);
}
}
/*Since, sum variable is used by
other function, hence we declare
it globally.*/
int sum=0; OUTPUT

/*----Main Function----*/ Enter the number: 10


void main() Sum: 1 2 3 5 8
{
int n,m,k ;
clrscr();
printf("Enter the number: ");
scanf("%d",&k);
printf("\nSum: ");
m=0;
n=1;
rec(m,n,k);
getch();
}
OMKAR DESHPANDE | ROLL NO: 25 | SE-CSE-1 | DS
OMKAR DESHPANDE | ROLL NO: 25 | SE-CSE-1 | DS

AIM: Implement Binary


Search using Iteration.

PROGRAM else if(array[mid]<n)


{
#include<stdio.h> mid++;
#include<conio.h> first=mid;
#include<stdlib.h> }
else if(array[mid]>n)
/*We have directly initialized {
the array with 10 elements mid--;
placed in a sorted way last=mid;
(Ascending) */ }
int else
array[]={10,20,30,40,50,60,70,80, {
90,100}; printf("Element is not found in
the list\n");
/*---BINARY SEARCH -----*/ exit(0);
void search(int n, int first, int mid, }
int last) } /*While loop termination*/
{
while(array[mid]!=n && if(array[mid]==n)
first<=last) printf("The element is found at
{ position: %d", mid+1);
mid=(first+last)/2; else
if(array[mid]==n) {
{ printf("The element %d doesnot
printf("The given element is exist in the list.\n", n);
found at position %d.\n", mid+1); }
exit(0); } /*Function Terminates Here*/
}
OMKAR DESHPANDE | ROLL NO: 25 | SE-CSE-1 | DS

/*------MAIN FUNCTION-----*/
void main() OUTPUT
{
int n,i,first=0,last=9,mid; Enter the element you want to
clrscr(); search for: 60
The given element is found at
/*We can also get input from the position 6.
user, instead of initializing the
array.*/ Enter the element you want to
search for: 100
/* The element is found at position:
printf("Enter the 10 numbers 10
[Ascending order only]:\n ");
for(i=0;i<10;i++) Enter the element you want to
{ search for: 53
scanf("%d", &array[i]); The element 53 doesnot exist in
} the list.
*/

printf("\nEnter the element you


want to search for: ");
scanf("%d", &n);
mid=(first+last)/2;
search(n,first,mid,last);
getch();
}
OMKAR DESHPANDE | ROLL NO: 25 | SE-CSE-1 | DS

{
AIM: To implement Binary /*mid++;
Search using Recursion. first=mid; (in case of
iteration)*/
PROGRAM search(n,mid+1,mid+1,last);
}
#include<stdio.h> else if(array[mid]>n)
#include<conio.h> {
#include<stdlib.h> /*mid--;
last=mid; (In case of
/*We have directly initialized iteration)*/
the array with 10 elements search(n,first,mid-1,mid-1);
placed in a sorted way }
(Ascending) */ else
int {
array[]={10,20,30,40,50,60,70,80, printf("Element is not found in
90,100}; the list\n");
exit(0);
/*---BINARY SEARCH -----*/ }
void search(int n, int first, int mid, }
int last) if(array[mid]==n)
{
{
printf("The element is found at
if(array[mid]!=n && first<=last)
position: %d", mid+1);
{ exit(0);
mid=(first+last)/2; }
if(array[mid]==n) else
{ {
printf("The given element is printf("The element %d doesnot
found at position %d.\n", mid+1); exixt in the list.\n", n);
exit(0); exit(0);
} }
else if(array[mid]<n)
}
OMKAR DESHPANDE | ROLL NO: 25 | SE-CSE-1 | DS

/*--------Main Function--------*/
void main()
{
int n,i,first=0,last=9,mid; OUTPUT
clrscr();
Enter the element you want to
/*We can also get input from the search for: 60
user, instead of initializing the The given element is found at
array.*/ position 6.

/* Enter the element you want to


printf("Enter the 10 numbers search for: 100
[Ascending order only]:\n "); The element is found at position:
for(i=0;i<10;i++) 10
{
scanf("%d", &array[i]); Enter the element you want to
} search for: 53
*/ The element 53 doesnot exixt in
the list.
printf("\nEnter the element you
want to search for: ");
scanf("%d", &n);
mid=(first+last)/2;
search(n,first,mid,last);
getch();
}
OMKAR DESHPANDE | ROLL NO: 25 | SE-CSE-1 | DS

AIM: Implement the


program of Tower of Hanoi /*--------Main Function------*/
using recursion. void main()
{
PROGRAM int n;
char beg='A',end='C',aux='B';
clrscr();
#include<stdio.h>
printf("Enter no. of Disks: ");
#include<conio.h>
scanf("%d", &n);
TOH(n,beg,end,aux);
int i=0;/*To set a counter, that
getch();
counts the number of moves
}
taken.*/

/*--Tower of Hanoi Function--*/ OUTPUT


void TOH(int n, char beg, char
end, char aux) Enter no. of Disks: 3
{ Move No: 1 | Moved the disk 1
if(n==1) from A to C
{ Move No: 2 | Moved the disk 2
printf("Move No: %d | Moved from A to B
the disk 1 from %c to Move No: 3 | Moved the disk 1
%c\n",++i,beg, end); from C to B
} Move No: 4 | Moved the disk 3
else from A to C
{ Move No: 5 | Moved the disk 1
TOH(n-1,beg,aux,end); from B to A
printf("Move No: %d | Moved Move No: 6 | Moved the disk 2
the disk %d from %c to from B to C
%c\n",++i,n,beg,end); Move No: 7 | Moved the disk 1
TOH(n-1,aux,end,beg); from A to C
}
}
OMKAR DESHPANDE | ROLL NO: 25 | SE-CSE-1 | DS

OUTPUT
Enter no. of Disks: 4
Move No: 1 | Moved the disk 1 from A to B
Move No: 2 | Moved the disk 2 from A to C
Move No: 3 | Moved the disk 1 from B to C
Move No: 4 | Moved the disk 3 from A to B
Move No: 5 | Moved the disk 1 from C to A
Move No: 6 | Moved the disk 2 from C to B
Move No: 7 | Moved the disk 1 from A to B
Move No: 8 | Moved the disk 4 from A to C
Move No: 9 | Moved the disk 1 from B to C
Move No: 10 | Moved the disk 2 from B to A
Move No: 11 | Moved the disk 1 from C to A
Move No: 12 | Moved the disk 3 from B to C
Move No: 13 | Moved the disk 1 from A to B
Move No: 14 | Moved the disk 2 from A to C
Move No: 15 | Moved the disk 1 from B to C
OMKAR DESHPANDE | SE-CSE-1 | 25 | QUEUES | DS

AIM: To implement simple if(rear==MAX-1)


Queue and the following printf("Overflow Condition\n");
queue operations: else
[1]. Enqueue {
[2]. Dequeue if(front==-1)
[3]. Display front=0;

++rear;
PROGRAM
A[rear]=num;
printf("Enqueued | %d |\n",
#include<stdio.h> num);
#include<conio.h> }
#include<stdlib.h>/*for exit(0)*/ }
#define MAX 5 /*-------Dequeue Function------*/
void dequeue()
/*Globally declared variables, {
because these are used by more
than one functions.*/
int num, A[MAX], rear=-1, /*
front=-1; Following cases should be
considered for dequeue-ing:
/*-------Enqueue Function------*/ (i) If Front and Rear, both are -
void enqueue(int num) 1 then the queue is empty.
{ (ii) Front cannot exceed Rear.
Hence, the max condition for
/*Following cases should be front should be front<rear.
considered: (iii) If front=rear, then the
(i) An Overflow condition element will be dequeued and
(ii) Set the front to 0 from reset front & rear gets reset to -1.
condition of -1and then insert */
the element.
*/
OMKAR DESHPANDE | SE-CSE-1 | 25 | QUEUES | DS
if(front==-1 && rear==-1) void main()
{ {
printf("UnderFlow int ch;
Condition.\n"); clrscr();
} printf("=====MENU====\n") ;
else if(front<rear) printf("\t[1] Enqueue\n\t[2]
{ Dequeue\n\t[3] Display\n\t[4]
int temp = A[front]; EXIT\n");
++front; start: /*goto concept*/
printf("Successfully printf("\nEnter your choice: ");
Dequeued | %d |.\n", temp); scanf("%d", &ch);
} switch(ch)
else if(front==rear) {
{ case 1:
temp = A[front]; printf("Enter a number to
printf("Successfully enqueue: ");
Dequeued | %d |.\n", temp); scanf("%d", &num);
front=-1; enqueue(num);
rear=-1; goto start;
} case 2:
} dequeue();
/*---------Display Function------*/ goto start;
void display() case 3:
{ display();
int i; goto start;
printf("The Elements in the queue case 4:
are: \n"); exit(0);
for(i=front;i<=rear;i++) default:
{ printf("Please enter a valid
printf(" %d |", A[i]); choice.\n");
} goto start;
} }
/*We display elements that lie getch();
/*-------MAIN
within FUNCTION-----*/
front to rear*/ }
OMKAR DESHPANDE | SE-CSE-1 | 25 | QUEUES | DS

OUTPUT Diagram Representation

=====MENU==== REAR

Empty Queue
[1] Enqueue
[2] Dequeue -1
A[0] A[1] A[1] A[1] A[1]
[3] Display
[4] EXIT FRONT
Enter your choice: 1 REAR
Enqueue

enqueue(2)
Enter a number to enqueue: 2
Enqueue
Enqueued | 2 | -1 2
A[0] A[1] A[1] A[1] A[1]
Enter your choice: 1 FRONT
Enter a number to enqueue: 4
REAR
Enqueued | 4 | Enqueue

enqueue(4)
Enqueue
-1 2 4
Enter your choice: 1 A[0] A[1] A[1] A[1] A[1]
Enter a number to enqueue: 5 FRONT
Enqueued | 5 |
REAR
Enqueue

enqueue(5)
Enter your choice: 1 Enqueue
Enter a number to enqueue: 8 -1 2 4 5
A[0] A[1] A[1] A[1] A[1]
Enqueued | 8 |
FRONT

Enter your choice: 1 Enqueue REAR

enqueue(8)
Enter a number to enqueue: 4 Enqueue
Enqueued | 4 | -1 2 4 5 8
A[0] A[1] A[1] A[1] A[1]

Enter your choice: 1 FRONT


Enter a number to enqueue: 9 REAR
Enqueue
Overflow Condition Enqueue
enqueue(4)

Enter your choice: 3 -1 2 4 5 8 4


The Elements in the queue are: A[0] A[1] A[1] A[1] A[1]
2|4|5|8|4| FRONT
OMKAR DESHPANDE | SE-CSE-1 | 25 | QUEUES | DS

Dequeue REAR

dequeue( )
Enter your choice: 2 Enqueue
Successfully Dequeued | 2 |. -1 4 5 8 4
A[0] A[1] A[1] A[1] A[1]

Enter your choice: 2 FRONT


Successfully Dequeued | 4 |.
Dequeue REAR

dequeue( )
Enqueue
Enter your choice: 2 5 8 4
-1
Successfully Dequeued | 5 |. A[0] A[1] A[1] A[1] A[1]

FRONT
Enter your choice: 3
The Elements in the queue are: Dequeue REAR

dequeue( )
|8|4 Enqueue
-1 8 4
A[0] A[1] A[1] A[1] A[1]
Enter your choice: 1
FRONT
Enter a number to enqueue: 5
Overflow Condition Disadvantage: In a normal Queue, we
can insert elements until queue becomes
full. But once queue becomes full, we
Enter your choice: 2 cannot insert the next element even if
Successfully Dequeued | 8 |. there is a space in front of queue.
Enter your choice: 2 Dequeue REAR

dequeue( )
Successfully Dequeued | 4 |. Enqueue
___ 4
-1
A[0] A[1] A[1] A[1] A[1]

Enter your choice: 2 FRONT


UnderFlow Condition. We reset front and rear to -1.
REAR
Dequeue
Reset

Enter your choice: 1 Enqueue


-1
Enter a number to enqueue: 5 A[0] A[1] A[1] A[1] A[1]
Enqueued | 5 | FRONT

REAR
enqueue(5)

Enter your choice: 4 We can again use the queue.


-1 5
A[0] A[1] A[1] A[1] A[1]

FRONT
OMKAR DESHPANDE | SE-CSE-1 | Roll No: 25 | DOUBLE ENDED QUEUE
AIM: Double Ended Queue using }
array implemention. }
getch();
PROGRAM }
/*Enqueue from rear Operation*/
void enqueue_r( int x)
#include<stdio.h>
{
#include<conio.h>
if(r==max)
#define max 10
printf("\nCan not enqueue. ");
int queue[max],f=max/2,r=max/2;
else
void enqueue_f();
printf("\nEnter element to enqueue: ");
void enqueue_r();
scanf("%d",&x);
void dequeue_f();
queue[r]=x;
void dequeue_r();
r++;
void display();
}
void main()
{ /*Enqueue from front Operation*/
void enqueue_f( int x)
int ch;
{
clrscr();
if(f==-1)
printf("\n--- Double ended Queue
printf("\nCan not enqueue. ");
implemention ---");
else
while(ch!=0)
printf("\nEnter element to enqueue: ");
{
scanf("%d",&x);
printf("\n1.Enqueue_F 2.Enqueue_R
f--;
3.Dequeue_F 4.Dequeue_R 5.Display
queue[f]=x;
0.Exit");
printf("\nEnter a choice: ");
}
scanf("%d",&ch);
switch(ch)
{ /*Dequeue from front Operation*/
void dequeue_f()
case 1: enqueue_f();
{
break;
int t;
case 2: enqueue_r();
if(f==max/2)
break;
{
case 3: dequeue_f();
printf("\nQueue is underflow ");
break;
}
case 4: dequeue_r();
else
break;
{
case 5: display();
t=queue[f];
break;
f++;
case 0: exit(0);
printf("\n<%d> element is dequeued.",t);
break;
}
default:
printf("\nInvalid Choice");
}
break;
OMKAR DESHPANDE | SE-CSE-1 | Roll No: 25 | DOUBLE ENDED QUEUE
/*Dequeue from rear Operation*/ 1.Enqueue_F 2.Enqueue_R 3.Dequeue_F
void dequeue_r() 4.Dequeue_R 5.Display 0.Exit
{ Enter a choice: 1
int t; Enter element to enqueue: 3
if(r==max/2)
{ 1.Enqueue_F 2.Enqueue_R 3.Dequeue_F
printf("\nQueue is underflow from rear."); 4.Dequeue_R 5.Display 0.Exit
} Enter a choice: 1
else Enter element to enqueue: 4
{
t=queue[r-1]; 1.Enqueue_F 2.Enqueue_R 3.Dequeue_F
r-- ; 4.Dequeue_R 5.Display 0.Exit
printf("\n%d Element is dequeued from Enter a choice: 5
rear.",t); Displaying elements in queue:
432
} 1.Enqueue_F 2.Enqueue_R 3.Dequeue_F
} 4.Dequeue_R 5.Display 0.Exit
/*Display Function*/ Enter a choice: 2
void display() Enter element to enqueue: 34
{ 1.Enqueue_F 2.Enqueue_R 3.Dequeue_F
int i; 4.Dequeue_R 5.Display 0.Exit
if(f==r) Enter a choice: 2
{ Enter element to enqueue: 45
printf("\nQueue is empty."); 1.Enqueue_F 2.Enqueue_R 3.Dequeue_F
} 4.Dequeue_R 5.Display 0.Exit
else Enter a choice: 5
{ Displaying elements in queue:
printf("\nDisplaying elements in 4 3 2 34 45
queue:\n"); 1.Enqueue_F 2.Enqueue_R 3.Dequeue_F
printf("\n"); 4.Dequeue_R 5.Display 0.Exit
for(i=f;i<r;i++) Enter a choice: 3
{ <4> element is dequeued.
printf("%d ",queue[i]); 1.Enqueue_F 2.Enqueue_R 3.Dequeue_F
} 4.Dequeue_R 5.Display 0.Exit
Enter a choice: 4
} 45 Element is dequeued from rear.
} 1.Enqueue_F 2.Enqueue_R 3.Dequeue_F
4.Dequeue_R 5.Display 0.Exit
OUTPUT Enter a choice: 5
Displaying elements in queue:
--- Double ended Queue implemention --- 3 2 34
1.Enqueue_F 2.Enqueue_R 3.Dequeue_F 1.Enqueue_F 2.Enqueue_R 3.Dequeue_F
4.Dequeue_R 5.Display 0.Exit 4.Dequeue_R 5.Display 0.Exit
Enter a choice: 1 Enter a choice: 0 */
Enter element to enqueue: 2
OMKAR DESHPANDE | SE-CSE-1 | 25 | CIRCULAR QUEUE | DS

AIM: To implement a /*-------Enqueue Function------*/


program of Circular Queue. int enqueue(int ele)
{
PROGRAM if(rear==MAX-1 && front==0)
{
#include<stdio.h> printf("QUEUE
#include<conio.h> OVERFLOW.\n");
#include<stdlib.h>/*For exit( )*/ return 0;
#define MAX 5 }
else if(front==rear+1)
/*We declare these variables {
globally, because they are used printf("QUEUE
by more than one functions.*/ OVERFLOW.\n");
int queue[MAX], rear=-1,front=- return 0;
1; }
else if(front==-1) /*Case 2*/
/* We need to consider all the {
following cases for enqueue: front=0;
Case 1: Overflow Conditions, i.e rear=0;
when rear reaches MAX-1 and queue[rear]=ele;
when front exceeds rear by 1. return 1;
Case 2: If front is -1(Reset }
Condition) then set front and else if(rear==MAX-1 &&
rear to 0. front!=0) /*Case 3*/
Case 3: When rear reaches {
MAX-1 but there is empty space rear=0;
in front of queue. Hence, we set queue[rear]=ele;
rear to 0. return 1;
Case 4: It will be a default case. }
Here rear is incremented by 1 else
and the element gets into the {
queue. ++rear;
*/ queue[rear]=ele;
OMKAR DESHPANDE | SE-CSE-1 | 25 | CIRCULAR QUEUE | DS
return 1; if(front==-1 && rear==-
} 1)/*Case-1*/
} {
printf("UNDERFLOW
/* We need to consider all the CONDITION.\n");
return 0;
following cases for dequeue:
}
Case 1: Underflow Condition, i.e
else if(front==rear)/*Case-2*/
when front and rear at -1
{
(Reset values).
ele=queue[front];
Case 2: When queue will just be printf("Dequeued %d.",
empty, i.e both front and rear are queue[front]);
equal. front=rear=-1;
We reset them back to -1, after return ele;
dequeue-ing the last remaining }
element. else if(front==MAX-1)/*Case-
Case 3: When front reaches 3*/
MAX-1 but there are elements {
in front of queue. Hence, we set ele=queue[front];
front to 0. printf("Dequeued %d.",
Case 4: It will be a default case. queue[front]);
Here element will be stored in a front=0;
variable and front is incremented return ele;
by 1. }

else/*Case-4*/
*/ {
/*-------Dequeue Function------*/ ele=queue[front];
printf("Dequeued %d.\n",
int dequeue() queue[front]);
{ ++front;
int ele; return ele;
}
}
OMKAR DESHPANDE | SE-CSE-1 | 25 | CIRCULAR QUEUE | DS
/*--------Display Function------*/ /*--------MAIN Function-------*/
void display() void main()
{ {
int i; int ele, ret=1, ch;
if(front<=rear) clrscr();
{ printf("=====MENU====\n");
for(i=front;i<=rear;i++) printf("\t[1] ENQUEUE\n\t[2]
{ DEQUEUE\n\t[3]
printf("| %d |\t", queue[i]); DISPLAY\n\t[4] EXIT\n");
} /*while(ret==1) We can either use
} {*/ while loop or goto
if(front>rear) start: statement.

{ printf("\nYour choice: ");


for(i=front;i<MAX;i++) scanf("%d", &ch);
printf("| %d |\t", queue[i]); switch(ch)
for(i=0;i<=rear;i++) {
printf("| %d |\t", queue[i]); case 1:
} printf("Enter the element to
} push: ");
scanf("%d", &ele);
Front can either be greater than ret=enqueue(ele);
or smaller than Rear. Hence we goto start;
apply two conditions to display case 2:
the elements in the queue. ret=dequeue();
REAR
goto start;
FRONT
case 3:
8 4
Front <=Rear 4 0 display();
7 3 2 1 5
goto start;
case 4:
8 1 6 exit(0);
4 0 }
7 3 2 1 2 Front>Rear /* } while-loop termination*/
REAR
6 getch();
FRONT
}
OMKAR DESHPANDE | SE-CSE-1 | 25 | CIRCULAR QUEUE | DS

OUTPUT DIAGRAMATIC REPRESENTATION


TEST CASE-1: Overflow - 1 TEST CASE – 1: Overflow - 1
=====MENU==== REAR
[1] ENQUEUE
4 01 Reset values
[2] DEQUEUE 3 11 -1 Empty Queue
[3] DISPLAY 2
[4] EXIT 2 FRONT
Your choice: 1
Enter the element to push: 4 FRONT
Your choice: 1 REAR
Enter the element to push: 5 Front and Rear
4 0 are set to 0
Your choice: 1 3 1
Enter the element to push: 6 2
Your choice: 1
Enter the element to push: 7
FRONT FRONT
Your choice: 1 1. 2.
Enter the element to push: 8 4 4
Your choice: 1 4 0 4 0
REAR REAR
3 1 3 1 5
Enter the element to push: 9 2 2
QUEUE OVERFLOW.

Your choice: 3 Rear is first incremented, ++rear;


|4| |5| |6| |7| |8| and then the element is inserted into
the queue, queue[rear]=ele;
Your choice: 4
REAR FRONT
8 4 enqueue(6)
4 0 enqueue(7)
7 3 2 1 5 enqueue(8)
6
OMKAR DESHPANDE | SE-CSE-1 | 25 | CIRCULAR QUEUE | DS
TEST CASE-2: Overflow-2
TEST CASE-2: Overflow-2 FRONT
Your choice: 2 REAR REAR
Dequeued 4. 8 4 8
Your choice: 2 4 0 4 0
Dequeued 5. 7 3 2 1 5 7 3 2 1 5
6 6
FRONT
Your choice: 1 ++front;
ele=queue[front];
Enter the element to push: 1
The element to be deleted is first
stored in another variable(ele), and
Your choice: 1 then front is incremented, ++front.
Enter the element to push: 2
REAR
Your choice: 1
8
Enter the element to push: 3 4 0 dequeue(4)
QUEUE OVERFLOW. 7 3 2 1 dequeue(5)
6
Your choice: 3 FRONT
8 1
|6| |7| |8| |1| |2| 4 0 enqueue(1)
Your choice: 4 7 3 2 1 2 enqueue(2)
REAR
6
Advantage: Efficient Utilization FRONT

of Memory: In Linear Queue


Cannot insert 1 and 2 in empty spaces
when we delete any element
only the front is incremented by -1 6 7 8
1, but that position is not used A[0] A[1] A[2] A[3] A[4]

later. So, memory wastage FRONT REAR

increases. But in Circular Queue 8 1


memory is utilized, if we delete 4 0
7 3 1 2
any element, that position is 2 REAR
used later on, because it is 6
circular. FRONT
OMKAR DESHPANDE | SE-CSE-1 | 25 | CIRCULAR QUEUE | DS
TEST CASE-3: Underflow-1
TEST CASE-3: Underflow-1 REAR FRONT

8 4
Your choice: 2
4 0
Dequeued 4.
7 3 1 5 Initial state of Queue
Your choice: 2 2
Dequeued 5. 6
Your choice: 2
Dequeued 6. REAR
dequeue(4)
Your choice: 2 dequeue(5)
4 0
Dequeued 7. 3 1
-1 dequeue(6)
2 dequeue(7)
Your choice: 2 dequeue(8)
Dequeued 8. FRONT

Your choice: 2 TEST CASE-4: Underflow-2


UNDERFLOW CONDITION.
Your choice: 4 8 1 dequeue(6)
4 0 dequeue(7)
TEST CASE-4: Underflow-2 7 3 2 1 2 dequeue(8)
REAR dequeue(1)
6
Your choice: 3 FRONT
|6| |7| |8| |1| |2|
Your choice: 2 dequeue(6)
Dequeued 6. 4 0 dequeue(7)
3 FRONT dequeue(8)
Your choice: 2 1 2 dequeue(2)
2 REAR dequeue(1)
Dequeued 7.
Your choice: 2
Dequeued 7. Now element ‘2’ is stored in another
Your choice: 2 variable(ele) and both the front and
Dequeued 1. rear are reset to ‘-1’.
Your choice: 2 REAR
Dequeued 2.
Your choice: 2 4 0
3 1
-1
UNDERFLOW CONDITION. 2
Your choice: 4 FRONT
OMKAR DESHPANDE | SE-CSE-1 | ROLL No: 25 | SINGY LINKED LIST

PRACTICAL NO: PROGRAM

AIM: Implement Singly Linked #include<stdio.h>


#include<conio.h>
List using C. #include<stdlib.h>

struct node
{
The following program performs the int data;
following operations on Singly Linked struct node *next;
List: };
struct node *head=NULL;
1. Insert an element from Beginning
2. Insert an element from End /*------Insert from Beginning------*/
3. Insert an element in any void insert_beg(int ele)
Intermediate Position {
4. Delete an element from struct node *temp=(struct
Beginning node*)malloc(sizeof(struct node));
5. Delete an element from End temp->data=ele;
6. Delete an element from any temp->next=head;
Intermediate Position head=temp;
7. count() : It counts the number of }
elements in the linked list
8. display(): It displays the current /*------Insert from End------*/
elements in the linked list void insert_end(int ele)
{
struct node *temp1=head;
struct node *temp=(struct
node*)malloc(sizeof(struct node));
temp->data = ele;
temp->next = NULL;

if(head==NULL)
insert_beg(ele);
else
{
while(temp1->next!=NULL)
OMKAR DESHPANDE | SE-CSE-1 | ROLL No: 25 | SINGY LINKED LIST
{ temp1=temp;
temp1=temp1->next; temp = temp->next;
} }
temp1->next=temp; temp1->next=NULL;
} free(temp);
} }
}
/*------Delete from Beginning------*/
void delete_beg() /*-------Count Function----------*/
{ int count(void)
struct node *temp=head; {
if(head==NULL) struct node *temp=head;
printf("Cannot delete element. int count=1;
Underflow\n"); while(temp->next!=NULL)
else {
{ temp=temp->next;
head = temp->next; count++;
free(temp); }
} return (count);
} }

/*-------Insert Randomly----------*/
/*------------Delete from End----------*/ void insert_any(int ele, int pstn)
void delete_end() {
{ int k,i;
struct node *temp,*temp1; struct node *temp=(struct
temp=temp1=head; node*)malloc(sizeof(struct node));
if(head==NULL) temp->data=ele;
printf("Underflow Condition.\n"); temp->next=NULL;
else if(head->next==NULL)
{ k=count();
temp=head; if(pstn<=k+1)
free(temp); {
head=NULL; if(pstn==1)
} insert_beg(ele);
else else if(pstn==k+1)
{ insert_end(ele);
while(temp->next!=NULL) else
{ {
OMKAR DESHPANDE | SE-CSE-1 | ROLL No: 25 | SINGY LINKED LIST
struct node *temp1=head;
for(i=0;i<pstn-2;i++) printf("\nDeleted [ %d | %d ]
{ node.\n", temp1->data, temp1->next);
temp1=temp1->next; temp1->next=NULL;
}
temp->next=temp1->next; free(temp1);
temp1->next=temp; }
} }
} else
else {
{ printf("\nTotal elements: %d. Hence
printf("\nThe last position of the there is not element at %d position.\n",
element is %d. Hence, cannot insert k, pstn);
%d at %d position.", k,ele,pstn); }
} }
}
/*-------Display Function----------*/
/*-------Delete Randomly----------*/ void display()
void delete_any(int pstn) {
{ struct node *temp=head;
int k,i; printf("\nHead(%d)", head);
struct node *temp=head; while(temp!=NULL)
struct node *temp1=head; {
k=count(); printf("-->[%d(%d) | %d]", temp-
>data,temp,temp->next);
if(pstn<=k) temp=temp->next;
{ }
if(pstn==1) printf("\n");
delete_beg(); }
else if(pstn==k)
delete_end();
else
{
for(i=0;i<pstn-2;i++)
{
temp=temp->next;
}
temp1=temp->next;
temp->next=temp1->next;
OMKAR DESHPANDE | SE-CSE-1 | ROLL No: 25 | SINGY LINKED LIST
case 4:
/*-------MAIN FUNCTION----------*/ delete_end();
int ele,pstn; break;
void main() case 5:
{ printf("\nEnter the element to
int ch; insert: ");
clrscr(); scanf("%d", &ele);
do printf("\nWhere do you want to
{ insert %d? ", ele);
printf("\t=====MENU=======\n"); scanf("%d", &pstn);
printf("\t1] Insert from insert_any(ele,pstn);
Beginning\n"); break;
printf("\t2] Delete from case 6:
Beginning\n"); printf("\nEnter the position of
printf("\t3] Insert from End\n"); element you want to delete.");
printf("\t4] Delete from End\n"); scanf("%d", &pstn);
printf("\t5] Insert at any position\n"); delete_any(pstn);
printf("\t6] Delete from any break;
position\n"); case 7:
printf("\t7] Display.\n"); display();
printf("Enter your choice: "); break;
scanf("%d",&ch); }
switch(ch) }
{ while(ch);
case 1: getch();
printf("\nEnter the element you }
want to insert: ");
scanf("%d", &ele);
insert_beg(ele);
break;
case 2:
delete_beg();
break;
case 3:
printf("\nEnter the element you
want to insert: ");
scanf("%d", &ele);
insert_end(ele);
break;
OMKAR DESHPANDE | SE-CSE-1 | ROLL No: 25 | SINGY LINKED LIST
OUTPUT OUTPUT

Case 1: Insert from Beginning Case 2: Delete from Beginning


=====MENU======= =====MENU=======
1] Insert from Beginning 1] Insert from Beginning
2] Delete from Beginning 2] Delete from Beginning
Menu Repeats

Menu Repeats
3] Insert from End 3] Insert from End
4] Delete from End 4] Delete from End
5] Insert at any position 5] Insert at any position
6] Delete from any position 6] Delete from any position
7] Display. 7] Display.
Enter your choice: 1 Enter your choice: 2
Enter the element you want to insert: 3
Enter your choice: 7
Enter your choice: 1
Enter the element you want to insert: 5 Head(2526)-->[5(2518) | 2510]--
>[3(2510) | 0]
Enter your choice: 1
Enter the element you want to insert: 7

Enter your choice: 7

Head(2526)-->[7(2526) | 2518]--
>[5(2518) | 2510]-->[3(2510) | 0]

Diagrammatic Representation Diagrammatic Representation

NULL
7 2518 3 2510 7 3 2510

2526 2518 2526 2518

Head 5 NULL
Head 5 NULL

2510 2510
OMKAR DESHPANDE | SE-CSE-1 | ROLL No: 25 | SINGY LINKED LIST

OUTPUT OUTPUT

Case 3: Insert from the End Case 4: Delete from End


=====MENU======= =====MENU=======
1] Insert from Beginning 1] Insert from Beginning
2] Delete from Beginning 2] Delete from Beginning
Menu Repeats

Menu Repeats
3] Insert from End 3] Insert from End
4] Delete from End 4] Delete from End
5] Insert at any position 5] Insert at any position
6] Delete from any position 6] Delete from any position
7] Display. 7] Display.
Enter your choice: 3 Enter your choice: 4
Enter the element you want to insert: 7
Enter your choice: 7
Enter your choice: 7
Head(2526) -->[5(2518) | 2510]--
Head(2526) -->[5(2518) | 2510]-- >[3(2510) | 0]
>[3(2510) | 2526] -->[7(2526) | 0]

Diagrammatic Representation Diagrammatic Representation

5 2510 3 NULL

5 2510 3 2526
2518 2510
2518 2510
Head
Head
7 NULL
7 NULL

2526 2526
OMKAR DESHPANDE | SE-CSE-1 | ROLL No: 25 | CIRCULAR LINKED LIST

PRACTICAL NO: PROGRAM

AIM: Implement Circular #include<stdio.h>


#include<conio.h>
Linked List using C. #include<stdlib.h>

struct node
{
The following program performs the int data;
following operations on Circular struct node *next;
Linked List: };
struct node *front=NULL;
1. Insert an element from Beginning struct node *rear=NULL;
2. Insert an element from End
3. Insert an element in any /*------Insert from Beginning------*/
Intermediate Position void insert_beg(int ele)
4. Delete an element from {
Beginning struct node *temp=(struct
5. Delete an element from End node*)malloc(sizeof(struct node));
6. Delete an element from any temp->data=ele;
Intermediate Position
7. count() : It counts the number of if(front==NULL && rear==NULL)
elements in the linked list {
8. display(): It displays the current front=rear=temp;
elements in the linked list temp->next=front;
}
else
{
temp->next=front;
front=temp;
rear->next=front;
}
}
OMKAR DESHPANDE | SE-CSE-1 | ROLL No: 25 | CIRCULAR LINKED LIST
/*------Insert from End------*/ /*------------Delete from End----------*/
void insert_end(int ele) void delete_end()
{ {
if(front==NULL && rear==NULL) struct node *temp;
{ temp=front;
insert_beg(ele); if(temp==NULL)
} printf("Underflow Condition.\n");
else else if(front==rear)
{ {
struct node *temp=(struct front=rear=NULL;
node*)malloc(sizeof(struct node)); free(temp);
temp->data=ele; }
rear->next=temp; else
rear=temp; {
rear->next=front; while(temp->next!=rear)
} {
} temp=temp->next;
}
/*------Delete from Beginning------*/ rear=temp;
void delete_beg() temp=temp->next;
{ rear->next=front;
struct node *temp=front; temp->next=NULL;
if(temp==NULL) free(temp);
printf("Cannot delete element. }
Underflow\n"); }
else if(front==rear) /*-------Count Function----------*/
{ int count(void)
front=rear=NULL; {
free(temp); struct node *temp=front;
} int count=1;
else while(temp->next!=front)
{ {
front=temp->next; temp=temp->next;
rear->next=front; count++;
temp->next=NULL; }
free(temp); return(count);
} }
}
OMKAR DESHPANDE | SE-CSE-1 | ROLL No: 25 | CIRCULAR LINKED LIST
/*-------Insert Randomly----------*/ /*-------Delete Randomly----------*/
void insert_any(int ele, int pstn) void delete_any(int pstn)
{ {
int k,i; int k,i;
k=count(); struct node *temp=front;
if(pstn<=k+1) struct node *temp1=front;
{ k=count();
if(pstn==1)
insert_beg(ele); if(pstn<=k)
else if(pstn==k+1) {
insert_end(ele); if(pstn==1)
else delete_beg();
{ else if(pstn==k)
struct node *temp1=front; delete_end();
struct node *temp=(struct else
node*)malloc(sizeof(struct node)); {
temp->data=ele; for(i=0;i<pstn-2;i++)
temp->next=NULL; {
temp=temp->next;
for(i=0;i<pstn-2;i++) }
{ temp1=temp->next;
temp1=temp1->next; temp->next=temp1->next;
}
temp->next=temp1->next; printf("\nDeleted [ %d | %d ]
temp1->next=temp; node.\n", temp1->data, temp1->next);
} temp1->next=NULL;
}
else free(temp1);
{ }
printf("\nThe last position of the }
element is %d. Hence, cannot insert else
%d at %d position.", k,ele,pstn); {
} printf("\nTotal elements: %d. Hence
} there is not element at %d position.\n",
k, pstn);
}
}
OMKAR DESHPANDE | SE-CSE-1 | ROLL No: 25 | CIRCULAR LINKED LIST
/*-------Display Function----------*/ case 1:
void display() printf("\nEnter the element you
{ want to insert: ");
struct node *temp=front; scanf("%d", &ele);
printf("\nFront(%d)", front); insert_beg(ele);
do break;
{ case 2:
printf("-->[%d(%d) | %d]", temp- delete_beg();
>data,temp,temp->next); break;
temp=temp->next; case 3:
} printf("\nEnter the element you
while(temp!=front); want to insert: ");
printf("<--Rear(%d)", rear); scanf("%d", &ele);
insert_end(ele);
printf("\n"); break;
} case 4:
delete_end();
/*-------MAIN FUNCTION----------*/ break;
int ele,pstn; case 5:
void main() printf("\nEnter the element to
{ insert: ");
int ch; scanf("%d", &ele);
clrscr(); printf("\nWhere do you want to
do insert %d? ", ele);
{ scanf("%d", &pstn);
printf("\t=====MENU=======\n"); insert_any(ele,pstn); break;
printf("\t1] Insert from case 6:
Beginning\n"); printf("\nEnter the position of
printf("\t2] Delete from element you want to delete.");
Beginning\n"); scanf("%d", &pstn);
printf("\t3] Insert from End\n"); delete_any(pstn);
printf("\t4] Delete from End\n"); break;
printf("\t5] Insert at any position\n"); case 7:
printf("\t6] Delete from any display(); break;
position\n"); }
printf("\t7] Display.\n"); }
printf("Enter your choice: "); while(ch);
scanf("%d",&ch); getch();
switch(ch) { }
OMKAR DESHPANDE | SE-CSE-1 | ROLL No: 25 | CIRCULAR LINKED LIST
OUTPUT Front(2346)-->[4(2346) | 2338]--
>[3(2338) | 2354]-->[5(2354) |
=====MENU======= 2346]<--Rear(2354)
1] Insert from Beginning Case 5: Intermediate Insertion
2] Delete from Beginning
3] Insert from End Enter your choice: 5
Menu Repeats

4] Delete from End Enter the element to insert: 7


5] Insert at any position Where do you want to insert 7? 3
6] Delete from any position
7] Display. Enter your choice: 7
Front(2354)-->[4(2354) | 2346]--
Case 1: Insert from Beginning >[3(2346) | 2362]-->[7(2362) | 2338]--
Enter your choice: 1 >[5(2338) | 2354]<--Rear(2338)
Enter the element you want to insert: 3 Case 6: Intermediate Deletion

Enter your choice: 1 Enter your choice: 6


Enter the element you want to insert: 4 Enter the position of element you want
to delete:2
Enter your choice: 1 Deleted [ 3 | 2362 ] node.
Enter the element you want to insert: 5
Enter your choice: 7 Enter your choice: 7
Front(2354)-->[5(2354) | 2346]-- Front(2354)-->[4(2354) | 2362]--
>[4(2346) | 2338]-->[3(2338) | >[7(2362) | 2338]-->[5(2338) |
2354]<--Rear(2338) 2354]<--Rear(2338)
Case 2: Delete from End Enter your choice: 0
Enter your choice: 2
Enter your choice: 7
Front(2346)-->[4(2346) | 2338]--
>[3(2338) | 2346]<--Rear(2338)
Case 3: Insert from End
Enter your choice: 3
Enter the element you want to insert: 5

Enter your choice: 7


OMKAR DESHPANDE | SE-CSE-1 | ROLL No: 25 | DOUBLY LINKED LIST

PRACTICAL NO: PROGRAM

AIM: Implement Doubly #include<stdio.h>


#include<conio.h>
Linked List using C. #include<stdlib.h>

struct node
{
The following program performs the int data;
following operations on Doubly struct node *next;
Linked List: struct node *prev;
};
1. Insert an element from Beginning struct node *head=NULL;
2. Insert an element from End
3. Insert an element in any /*-------newnode function-------*/
Intermediate Position struct node* newnode(int ele)
4. Delete an element from {
Beginning struct node *temp=(struct
5. Delete an element from End node*)malloc(sizeof(struct node));
6. Delete an element from any temp->data=ele;
Intermediate Position temp->next=NULL;
7. count() : It counts the number of head->prev=NULL;
elements in the linked list return(temp);
8. display(): It displays the current }
elements in the linked list
/*------Insert from Beginning------*/
void insert_beg(int ele)
{
struct node *temp=newnode(ele);
temp->next=head;
head->prev=temp;
head=temp;
}
OMKAR DESHPANDE | SE-CSE-1 | ROLL No: 25 | DOUBLY LINKED LIST
/*------Insert from End------*/ /*------------Delete from End----------*/
void insert_end(int ele) void delete_end()
{ {
if(head==NULL) if(head==NULL)
insert_beg(ele); printf("Underflow Condition.\n");
else else if(head->next==NULL)
{ {
struct node *temp1=head; delete_beg();
struct node *temp=newnode(ele); }
while(temp1->next!=NULL) else
{ {
temp1=temp1->next; struct node *temp,*temp1;
} temp=temp1=head;
temp1->next=temp; while(temp->next!=NULL)
temp->prev=temp1; temp = temp->next;
} temp1=temp->prev;
} temp1->next=NULL;
free(temp);
/*------Delete from Beginning------*/ }
void delete_beg() }
{
struct node *temp=head; /*-------Count Function----------*/
if(head==NULL) int count(void)
printf("Cannot delete element. {
Underflow\n"); struct node *temp=head;
else if(head->next==NULL) int count=1;
{ if(head==NULL)
temp=head; {
head=NULL; count=0;
free(temp); return(count);
} }
else while(temp->next!=NULL)
{ {
head = temp->next; temp=temp->next;
free(temp); count++;
head->prev=NULL; }
} return (count);
} }
OMKAR DESHPANDE | SE-CSE-1 | ROLL No: 25 | DOUBLY LINKED LIST
/*-------Insert Randomly----------*/ /*-------Delete Randomly----------*/
void insert_any(int ele, int pstn) void delete_any(int pstn)
{ {
int k,i; int k,i;
k=count(); k=count();
if(pstn<=k+1)
{ if(pstn<=k)
if(pstn==1) {
insert_beg(ele); if(pstn==1)
else if(pstn==k+1) delete_beg();
insert_end(ele); else if(pstn==k)
else delete_end();
{ else
struct node *temp=newnode(ele); {
struct node *temp1=head; struct node *temp=head;
for(i=0;i<pstn-2;i++) struct node *temp1=head;
{ for(i=0;i<pstn-2;i++)
temp1=temp1->next; {
} temp=temp->next;
temp->next=temp1->next; }
temp1->next=temp; temp1=temp->next;
temp->prev=temp1; temp->next=temp1->next;
temp1=temp->next; temp=temp1->next;
temp1->prev=temp; temp->prev=temp1->prev;
} printf("\nDeleted [ %d | %d | %d ]
} node.\n", temp1->prev, temp1->data,
else temp1->next);
{ free(temp1);
printf("\nThe position of the last }
element is %d. Hence, cannot insert }
%d at %d position.", k,ele,pstn); else
} {
} printf("\nTotal elements: %d. Hence
there is no element at %d position.\n",
k, pstn);
}
}
OMKAR DESHPANDE | SE-CSE-1 | ROLL No: 25 | DOUBLY LINKED LIST
/*-------Display Function----------*/ printf("\nEnter the element you
void display() want to insert: ");
{ scanf("%d", &ele);
struct node *temp=head; insert_beg(ele);
printf("\nHead(%d)", head); break;
while(temp!=NULL) case 2:
{ delete_beg(); break;
printf("<-->[%d|%d(%d)|%d]", case 3:
temp->prev,temp->data,temp,temp- printf("\nEnter the element you
>next); want to insert: ");
temp=temp->next; scanf("%d", &ele);
} insert_end(ele);
printf("\n"); break;
} case 4:
delete_end();
/*-------MAIN FUNCTION----------*/ break;
int ele,pstn; case 5:
void main() printf("\nEnter the element to
{ insert: ");
int ch; scanf("%d", &ele);
clrscr(); printf("\nWhere do you want to
do insert %d? ", ele);
{ scanf("%d", &pstn);
printf("\t=====MENU=======\n"); insert_any(ele,pstn);
printf("\t1] Insert from break;
Beginning\n"); case 6:
printf("\t2] Delete from printf("\nEnter the position of
Beginning\n"); element you want to delete.");
printf("\t3] Insert from End\n"); scanf("%d", &pstn);
printf("\t4] Delete from End\n"); delete_any(pstn);
printf("\t5] Insert at any position\n"); break;
printf("\t6] Delete from any case 7:
position\n"); display();
printf("\t7] Display.\n"); break;
printf("Enter your choice: "); }
scanf("%d",&ch); }
switch(ch) while(ch);
{ getch();
case 1: }
OMKAR DESHPANDE | SE-CSE-1 | ROLL No: 25 | DOUBLY LINKED LIST
OUTPUT Head(2340)<-->[0|3(2340)|2330]<--
>[2340|1(2330)|2350]<--
=====MENU======= >[2330|4(2350)|0]
1] Insert from Beginning
2] Delete from Beginning Case 4: Delete from the End
3] Insert from End Enter your choice: 4
4] Delete from End
Menu Repeats

5] Insert at any position


6] Delete from any position Enter your choice: 7
7] Display. Head(2340)<-->[0|3(2340)|2330]<--
>[2340|1(2330)|0]
Case 5: Intermediate Insertion
Enter your choice: 1
Enter the element you want to insert: 1 Enter your choice: 5
Enter the element to insert: 2
Enter your choice: 1 Where do you want to insert 2? 2
Enter the element you want to insert: 3
Enter your choice: 7
Enter your choice: 1 Head(2340)<-->[0|3(2340)|2350]<--
Enter the element you want to insert: 2 >[2340|2(2350)|2330]<--
>[2350|1(2330)|0]
Enter your choice: 7
Case 6: Intermediate Deletion
Head(2350)<-->[0|2(2350)|2340]<--
>[2350|3(2340)|2330]<-- Enter your choice: 6
>[2340|1(2330)|0] Enter the position of element you want
to delete.2
Case 2: Delete from Beginning
Deleted [ 2340 | 2 | 2330 ] node.
Enter your choice: 2
Enter your choice: 7
Enter your choice: 7 Head(2340)<-->[0|3(2340)|2330]<--
Head(2340)<-->[0|3(2340)|2330]<-- >[2340|1(2330)|0]
>[2340|1(2330)|0]
Enter your choice: 0
Case 3: Insert from the End
Enter your choice: 3
Enter the element you want to insert: 4

Enter your choice: 7


OMKAR DESHPANDE | SE-CSE-1 | ROLL No: 25 | DOUBLY LINKED LIST

Graphical Representation

Case 1: Insert from Beginning

Head

NULL 2 2340 2350 3 2340 2340 1 NULL


2350 2340 2330

Case 2: Delete from Beginning

Head

NULL 2 NULL NULL 3 2340 2340 1 NULL


2350 2340 2330

Case 3: Insert from the End

Head

NULL 3 2340 2340 1 2350 2330 3 NULL


2340 2330 2350

Case 4: Delete from the End

Head

NULL 3 2340 2340 1 NULL NULL 3 NULL


2340 2330 2350
OMKAR DESHPANDE | SE-CSE-1 | ROLL No: 25 | LINKED LIST APPLICATIONS

PRACTICAL NO:
PROGRAM
AIM: Implement Addition of
#include<stdio.h>
two polynomials using Linked #include<conio.h>
List. #include<stdlib.h>

void display(void);
The following program performs the
addition operation on two polynomials struct node
using Doubly Linked List. {
int coef, expo;
It includes the following functions: struct node *next;
};
1. newnode( ): Generates new node struct node *p1=NULL;
dynamically. struct node *p2=NULL;
2. Insert_p1( ): Creates node for struct node *sum=NULL;
every term of polynomial – 1 and
links it to p1 head node. /*------Newnode Function------*/
3. Insert_p2( ): Creates node for struct node* newnode(int num1, int
every term of polynomial – 2 and num2)
links it to p1 head node. {
4. Insert_sum( ): The resultant struct node *temp=(struct
summation of two polynomials is node*)malloc(sizeof(struct node));
stored in this linked list. temp->coef=num1;
5. Addition( ): Has the actual logic, temp->expo=num2;
to add given two polynomials. temp->next=NULL;
6. Initialize_poly( ): It reads the return(temp);
values of the two polynomials, }
7. Display( ): Displays the required
resultant sum linked list.
OMKAR DESHPANDE | SE-CSE-1 | ROLL No: 25 | LINKED LIST APPLICATIONS
/*------Polynomial – 1 Function-----*/ /*--------Sum Linked List---------*/
void insert_p1(int num1, int num2) void insert_sum(int num1, int num2)
{ {
struct node struct node
*temp=newnode(num1,num2); *temp=newnode(num1,num2);
if(p1==NULL) if(sum==NULL)
{ {
p1=temp; sum=temp;
} }
else else
{ {
struct node *temp1=p1; struct node *temp1=sum;
while(temp1->next!=NULL)
{ while(temp1->next!=NULL)
temp1=temp1->next; {
} temp1=temp1->next;
temp1->next=temp; }
} temp1->next=temp;
} }
}
/*------Polynomial – 2 Function-----*/ /*-------Display Function----------*/
void insert_p2(int num1, int num2) void display()
{ {
struct node struct node *temp=sum;
*temp=newnode(num1,num2); printf("\nHead(%d)", sum);
if(p2==NULL) while(temp!=NULL)
{ {
p2=temp; printf("-->[%d|%d(%d)|%d]", temp-
} >coef,temp->expo,temp,temp->next);
else temp=temp->next;
{ }
struct node *temp1=p2; printf("\n");
while(temp1->next!=NULL) p1=p2=sum=NULL;
{ printf("\nThe pointers are now reset to
temp1=temp1->next; NULL. New values can be entered.
} \n");
temp1->next=temp; }
}
}
OMKAR DESHPANDE | SE-CSE-1 | ROLL No: 25 | LINKED LIST APPLICATIONS
/*---------Addition Operation---------*/ {
void addition() while(p1!=NULL)
{ {
int k; insert_sum(p1->coef,p1->expo);
while(p1!=NULL && p2!=NULL) p1=p1->next;
{ }
if(p1->expo==p2->expo) addition();
{ }
k=p1->coef+p2->coef; }
insert_sum(k,p1->expo); }
p1=p1->next;
p2=p2->next; /*-------Initializing polynomials------*/
addition(); void initialize_poly()
} {
int i,n;
if(p1->expo<p2->expo) printf("\nEnter the no. of terms of
{ Poly-1: ");
insert_sum(p2->coef,p2->expo); scanf("%d", &n);
p2=p2->next; for(i=0;i<n;i++)
addition(); {
} printf("Enter Coefficient and
Exponent: ");
if(p1->expo>p2->expo) scanf("%d %d", &num1, &num2);
{ insert_p1(num1,num2);
insert_sum(p1->coef,p1->expo); }
p1=p1->next; printf("\nEnter the no. of terms of
addition(); Poly-2: ");
} scanf("%d", &n);
if(p1==NULL) for(i=0;i<n;i++)
{ {
while(p2!=NULL) printf("Enter Coefficient and
{ Exponent: ");
insert_sum(p2->coef,p2->expo); scanf("%d %d", &num1, &num2);
p2=p2->next; insert_p2(num1,num2);
} }
addition(); }
}

if(p2==NULL)
OMKAR DESHPANDE | SE-CSE-1 | ROLL No: 25 | LINKED LIST APPLICATIONS
/*-------MAIN FUNCTION----------*/ OUTPUT
int num1,num2;
void main() Enter the no. of terms of Poly-1: 3
{ Enter Coefficient and Exponent: 4 2
int ch; Enter Coefficient and Exponent: 3 1
clrscr(); Enter Coefficient and Exponent: 1 0
do
{ Enter the no. of terms of Poly-2: 4
initialize_poly(); Enter Coefficient and Exponent: 7 3
addition(); Enter Coefficient and Exponent: 8 2
display(); Enter Coefficient and Exponent: 9 1
printf("Continue? (1/0): "); Enter Coefficient and Exponent: 7 0
scanf("%d", &ch);
} Head(2178)-->[7|3(2178)|2188]--
while(ch); >[12|2(2188)|2198]--
getch(); >[12|1(2198)|2208]-->[8|0(220
} 8)|0]

The pointers are now reset to NULL.


New values can be entered.
Continue? (1/0): 0
Graphical Representation

P1 Poly-1: 4x2 + 3x + 1
4 2 next 3 1 next 1 0 NULL

P2 Poly-2: 7x3 +8x2 + 9x + 7


7 3 next 8 2 next 9 1 next

7 0 NULL

SUM
Sum: 7x3 +12x2 + 12x + 8
7 3 2188 12 2 2198 12 1 2208
2178 2188 2198 NULL
8 0
2208
OMKAR DESHPANDE | SE-CSE-1 | ROLL No: 25 | PRE-IN-POST-ORDER TREES
/*------Preorder Function-----*/
PRACTICAL NO: void Preorder(struct node *Node)
{
if(Node==NULL)
AIM: Implement Pre-order, return;
In-order and Post-order on a printf(" %d ", Node->data);
Preorder(Node->left);
Tree using C Program.
Preorder(Node->right);
PROGRAM }

#include<stdio.h> /*------Inorder Function-----*/


#include<conio.h> void Inorder(struct node *Node)
{
struct node if(Node==NULL)
{ return;
struct node *left; Inorder(Node->left);
int data; printf(" %d ", Node->data);
struct node *right; Inorder(Node->right);
}; }
struct node *root;
/*--------PostOrder Function---------*/
/*------Newnode Function------*/ void Postorder(struct node *Node)
struct node* newnode(int ele) {
{ if(Node==NULL)
struct node *temp=(struct return;
node*)malloc(sizeof(struct node)); Postorder(Node->left);
temp->left=NULL; Postorder(Node->right);
temp->right=NULL; printf(" %d ", Node->data);
temp->data=ele; }
return (temp);
}
OMKAR DESHPANDE | SE-CSE-1 | ROLL No: 25 | PRE-IN-POST-ORDER TREES
/*-------MAIN FUNCTION----------*/
void main() OUTPUT
{
Preorder Traversal of Binary Tree
struct node *root=newnode(1); is: 1 2 4 5 3
clrscr(); Inorder Traversal of Binary Tree:
root->left=newnode(2); 4 2 5 1 3
root->right=newnode(3); Postorder Traversal of Binary
root->left->left=newnode(4); Tree: 4 5 2 3 1
root->left->right=newnode(5);

printf("\nPreorder Traversal of Graphical Representation


Binary Tree is: ");
Preorder(root);
printf("\nInorder Traversal of 1
Binary Tree: ");
Inorder(root);
printf("\nPostorder Traversal of 2 3
Binary Tree: ");
Postorder(root);
4 5
getch();
}
OMKAR DESHPANDE | SE-CSE-1 | ROLL No: 25 | ADJACENCY LIST | GRAPHS
/*------Adj. List Logic Function------*/
PRACTICAL NO: void create_graph()
{
int src,dest,i;
AIM: Implement Adjacency struct node *temp, *temp1, *t,*t1;
List Representation of Graph g=(struct graph*)malloc(sizeof(struct
graph));
using a C Program.
printf("\nEnter the no. of vertices and
PROGRAM edges: ");
scanf("%d %d", &g->v,&g->e);
#include<stdio.h> g->list=NULL;
#include<conio.h> g->list=(struct node*)malloc(g->v *
(sizeof(struct node)));
struct node
{ for(i=0;i<g->v;i++)
int data; {
struct node *next; t=g->list+i;
}; t->data = i;
t->next=NULL;
struct graph }
{
int v; for(i=0;i<g->e;i++)
int e; {
struct node *list; printf("\nConnections: ");
}; scanf("%d %d", &src,&dest);
temp=(struct
struct graph *g; /*This pointer points node*)malloc(sizeof(struct node));
to the graph structure*/ temp->data=dest;
temp->next=NULL;
t1=g->list+src;

if(t1->next==NULL)
t1->next=temp;
OMKAR DESHPANDE | SE-CSE-1 | ROLL No: 25 | ADJACENCY LIST | GRAPHS
else OUTPUT
{
temp1=t1;
while(temp1->next!=NULL) Enter the no. of vertices and
temp1=temp1->next; edges: 4 5
temp1->next=temp; Connections: 0 1
} Connections: 0 2
} Connections: 1 2
Connections: 1 3
for(i=0;i<g->v;i++) Connections: 2 3
{
struct node *temp2;
temp2=g->list+i; | 0(1938) | 1958 |-->| 1(1958) |
printf("\n| %d(%d) | %d |",temp2- 1966 |-->| 2(1966) | 0 |
>data,temp2,temp2->next); | 1(1942) | 1974 |-->| 2(1974) |
while(temp2->next!=NULL) 1982 |-->| 3(1982) | 0 |
{ | 2(1946) | 1990 |-->| 3(1990) | 0 |
temp2=temp2->next; | 3(1950) | 0 |
printf("-->| %d(%d) | %d |",temp2-
>data,temp2,temp2->next);
} Graphical Representation
}
0 1
} A B

/*-------MAIN FUNCTION----------*/
void main() D C
3 2
{
clrscr();
1 1966 2 NUL
create_graph(); 0 1958
getch(); 1 1974 2 1982 3 NUL
}
2 1990
3 NUL
3 NUL
OMKAR DESHPANDE | SE-CSE-1 | ROLL No: 25 | ADJACENCY MATRIX | GRAPHS

}
PRACTICAL NO: }
printf("Enter Connections: ");
for(i=0;i<g->edges;i++)
AIM: Implement Adjacency {
Matrix Representation of scanf("%d %d", &x, &y);
g->mat[x][y]=1;
Graph using a C Program.
//g->mat[y][x]=1;
PROGRAM }
for(i=0;i<g->nodes;i++)
#include<stdio.h> {
#include<conio.h> for(j=0;j<g->nodes;j++)
{
struct graph printf("| %d |\t", g->mat[i][j]);
{ }
int nodes; printf("\n");
int edges; }
int **mat; }
};
/*-------MAIN FUNCTION----------*/
/*------Adj. List Logic Function------*/ void main()
{
void create_graph() clrscr();
{ create_graph();
int i,j,k,x,y; getch();
struct graph *g=(struct }
graph*)malloc(sizeof(struct graph));
printf("Enter the number of nodes
and edges: "); OUTPUT
scanf("%d %d",&g->nodes,&g-
>edges); Enter the number of nodes and
g->mat=(int**)malloc(sizeof(int)*(g- edges: 2 1
>nodes * g->nodes)); Enter Connections: 0 1
for(i=0;i<g->nodes;i++)
|0| |1|
{
for(j=0;j<g->nodes;j++) |0| |0| Graphically,
{ 0 1
g->mat[i][j]=0;
A B
REFERENCES

 Class notes, by Bhagyashri Kapre Mam.


 ‘Data Structures and Algorithms Made Easy’ by
Narasimha Kurumanchi.
 http://www.geeksforgeeks.org
 https://stackoverflow.com/
 http://www.sanfoundry.com/
 https://github.com/
 http://www.c4learn.com

You might also like