Professional Documents
Culture Documents
DS Practical File Omkar
DS Practical File Omkar
By Omkar Deshpande
INDEX
Sr. OBJECTIVES
1. Structures: Programs on
a. Details of Book
b. Details of an employee
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
14. Trees:
a. Pre-Order
b. In-Order
c. Post-Order
#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
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);
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
PROGRAM: PROGRAM:
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); }
}
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
/*------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.
*/
{
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.
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
++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
=====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
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]
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]
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]
REAR
enqueue(5)
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
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.
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
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
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
Head(2526)-->[7(2526) | 2518]--
>[5(2518) | 2510]-->[3(2510) | 0]
NULL
7 2518 3 2510 7 3 2510
Head 5 NULL
Head 5 NULL
2510 2510
OMKAR DESHPANDE | SE-CSE-1 | ROLL No: 25 | SINGY LINKED LIST
OUTPUT OUTPUT
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]
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
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
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
Graphical Representation
Head
Head
Head
Head
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]
P1 Poly-1: 4x2 + 3x + 1
4 2 next 3 1 next 1 0 NULL
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 }
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