Ds Lab Manual
Ds Lab Manual
Ds Lab Manual
LAB MANUAL
R21
R21 Regulations
JAWAHARLAL NEHRU TECHNOLOGICAL UNIVERSITY ANANTAPUR
(Established by Govt. of A.P., ACT No.30 of 2008) ANANTAPUR
– 515 002 (A.P) INDIA
Course Code L T P C
DS Lab
21F00107 0 1 2 2
Course Objectives
To get familiar with the basic concepts of C programming.
To design programs using arrays, strings, pointers and structures.
To illustrate the use of Stacks and Queues
To apply different operations on linked lists.
To demonstrate Binary search tree traversal techniques.
To design searching and sorting techniques.
Course outcomes (CO) : After completion of the course, the student can able to
Develop C programs for computing and real-life applications using basic elements
like control statements, arrays, functions, pointers and strings, and data structures
like stacks, queues and linked lists.
19. Write a C program that uses Stack operations to perform evaluating the postfix expression.
20. Write a C program that uses functions to perform the creation, insertion, deletion and traversal
operations on singly linked list.
21. Write a C program that uses functions to perform the creation, insertion, deletion and traversal
operations on doubly linked list.
22. Write a C program that uses functions to perform the creation, insertion, deletion and traversal
operations on circular linked list.
23. Write a C program that uses functions to perform the following create a Binary Tree of integers Traverse the
above binary tree in preorder, in order and post order
24. Write a C program to perform linear search operation for a key value in a given list of integers.
25. Write a C program to perform binary search operation for a key value in a given list of integers.
26. Write a C program to perform linear search operation for a key value in a given list of integers using
recursive function.
27. Write a C program to perform binary search operation for a key value in a given list of integers using
recursive function.
28. Write a C program that implements bubble sort to sort given list of integers in ascending order.
29. Write a C program that implements selection sort to sort given list of integers in ascending order.
30. Write a C program that implements insertion sort to sort given list of integers in ascending order.
DATA STRUCTURES LAB MANUAL
Week-1
Algorithm:-
1. Start
2. Read a number N
3. Call a function factorial(N) by passing the values of N
4. If N=1 then it returns 1 as the factorial
5. Otherwise, it calculates the factorial f = N * factorial(N-1) by calling the same function again
and again
6. Display the factorial of number N
7. Stop
(ii) To find the GCD (Greatest Common Divisor) of two given integers.
Algorithm:-
1. Start
2. Read two numbers a and b
3. Call a function gcd (a, b) by passing the value of a and b
4. Check a != b, then
5. Check a > b then calculate gcd of two numbers by calling the function gcd(a-b, b)
6. Otherwise calculate gcd of two numbers by calling the function gcd(a, b-a)
7. Display the gcd of two numbers
8. Stop
Program#2(a) GCD of a given two integer by using Recursive Function #include<stdio.h>
int gcd(int m,int n)
{
if(n==0)
return m; else
gcd(n,m%n);
}
void main()
{ int a,b,g,l; printf("\n enter a: ");
scanf("%d",&a); printf("\n enter b: ");
scanf("%d",&b); g=gcd(a,b); l=(a*b)/g;
printf("\n GCD of %d and %d : %d",a,b,g);
printf("\n LCM of %d and %d : %d",a,b,l);
}
Week-2
(1) Write a C program to find out the largest and smallest number in a list of integers
(1) Write a C program to find out the largest and smallest number in a list of integers
Problem
Let the user enter four series of integers in the console, find out a number which is smallest and
largest in a series Solution
To calculate the small and large number, we use if conditions. The logic we use to find the
largest and smallest number is −
if(minno>q) //checking 1st and 2nd number
minno=q; else if(maxno&l;q) maxno=q;
if(minno>r) //checking 1st and 3rd number
minno=r;
Program#1: program to find out the largest and smallest number in a list of integers
#include<stdio.h> int main(){ int minno,maxno,p,q,r,s; printf("enter any four numbers:");
scanf("%d%d%d%d",&p,&q,&r,&s);
minno=p;
maxno=p;
if(minno>q) //checking 1st and 2nd number
minno=q;
else if(maxno<q)
maxno=q;
if(minno>r) //checking 1st and 3rd number
minno=r;
else if(maxno<r)
maxno=r;
if(minno>s) //checking 1st and 4th number
minno=s; else if(maxno<s) maxno=s; printf("Largest
number from the given 4 numbers is:%d\n",maxno);
printf("Smallest numbers from the given 4 numbers is:%d",minno);
return 0;
}
Output
enter any four numbers:34 78 23 12 Largest
number from the given 4 numbers is:78
Smallest numbers from the given 4 numbers is:12
2(i) Algorithm:
Step 1: Start
Step 2: Declare i, j, k, A[3][3], B[3][2], C[3][2] as integers
Step 3: Initialize i = 0, j = 0
Step 4: Till i < 3 execute step 5 else goto step 9
Step 5: Till j < 3 execute steps 6 to 7 else goto step 8
Step 6: Read A[i][j]
Step 7: Increment j by 1 goto step 5 Step
8: Increment i by 1 goto step 4
Step 9: Initialize i = 0, j = 0
Step 10: Till i < 3 execute step 11 else goto step15
Step 11: Till j < 2 execute steps 6 to 7 else goto step 14
Step 12: Read B[i][j]
Step 13: Increment j by 1 goto step 11 Step
14: Increment i by 1 goto step 10
Step 15: Initialize i = 0, j = 0, k = 0
Step 16: Till i < 3 execute step 17 else goto step 24
Step 17: Till j < 2 execute step 18 else goto step 23
Step 18: Initialize C[i][j] = 0
Step 19: Till k<3 execute steps 20 to 21 else goto step
Step 20: calculate C[i][j] = C[i][j] + A[i][k] * B[k][j]
Step 21: Increment k by 1 goto step 19
Step 22: Increment j by 1 goto step 17 Step
23: Increment i by 1 goto step 16
Step 24: Initialize i = 0, j = 0
Step 25: Till i < 3 execute step 26 else goto step 30
Step 26: Till j < 3 execute steps 27 to 28 else goto step 29
Step 27: Print C[i][j]
Step 28: Increment j by1 goto step 26
Step 29: Increment i by 1 goto step 25
Step 30: Stop Addition of
Two Matrices
Program:
Program:
#include<stdio.h> #include<conio.h>
void main()
{
int a[3][3], b[3][3], c[3][3], i, j, k;
clrscr();
printf("Enter the elements of 3*3 matrix a \n");
for(i = 0; i < 3; i++)
{
for(j = 0; j < 3; j++)
{
scanf("%d", &a[i][j]);
}
}
printf("Enter the elements of 3*3 matrix b \n");
for(i = 0; i < 3; i++)
{
for(j = 0; j < 3; j++)
{
scanf("%d", &b[i][j]);
}
}
for(i = 0; i < 3; i++)
{
for(j = 0; j < 3; j++)
{ c[i][j] = 0
for(k = 0; k < 3; k++)
{
c[i][j] = c[i][j] + (a[i][k] * b[k][j])
}
}
}
printf("The resultant 3*3 matrix c is \n");
for(i = 0; i < 3; i++)
{
for(j = 0; j < 3; j++)
{
printf("%d\t", c[i][j]);
}
printf("\n");
}
getch();
}
Week-3
Algorithm:
Step 1: Start
Step 2: read main string and sub string
Step 3: find the length of main string(r) Step
4: find length of sub string(n)
Step 5: copy main string into sub string
Step 6: read the position to insert the sub string(p)
Step 7: copy sub string into main string from position p - 1
Step 8: copy temporary string into main string from position p + n - 1
Step 9: print the strings
Step 10: Stop Program:
#include<stdio.h>
#include<conio.h> #include<string.h>
void main()
{
char str1[20], str2[20];
int l1, l2, n, i;
clrscr();
puts("Enter the string 1\n");
gets(str1); l1 =
strlen(str1); puts("Enter the
string 2\n");
gets(str2); l2 =
strlen(str2);
printf("Enter the position where the string is to be inserted\n");
scanf("%d", &n);
for(i = n; i < l1; i++)
{
str1[i + l2] = str1[i];
}
for(i = 0; i < l2; i++)
{
str1[n + i] = str2[i];
}
str2[l2 + 1] = '\0';
printf("After inserting the string is %s", str1);
getch();
}
Algorithm:
Step 1: Start
Step 2: read string
Step 3: find the length of the string
Step 4: read the value of number of characters to be deleted and positioned
Step 5: string copy part of string from position to end, and (position + number of characters to
end) Step 6: Stop
Program:
#include<stdio.h>
#include<conio.h> #include<string.h>
void main()
{
char str[20];
int i, n, l, pos;
clrscr();
puts("Enter the string\n");
gets(str);
printf("Enter the position where the characters are to be deleted\n");
scanf("%d", &pos);
printf("Enter the number of characters to be deleted\n");
scanf("%d", &n); l = strlen(str);
for(i = pos + n; i < l; i++)
{
str[i - n] = str[i];
}
str[i - n] = '\0';
printf("The string is %s", str);
getch();
}
Week-4
(1) Write a C program that displays the position or index in the string S where the string T
begins, or – 1 if S doesn’t contain T.
Algorithm:
Step 1: Start
Step 2: read the string and then displayed
Step 3: read the string to be searched and then displayed
Step 4: searching the string T in string S and then perform the following steps i. found
= strstr(S, T) ii. if found print the second string is found in the first string at the
position. If not goto step5
Step 5: print the -1
Step 6: Stop
Program:
#include<stdio.h>
#include<string.h> #include<conio.h>
void main()
{
char s[30], t[20];
char *found;
clrscr();
puts("Enter the first string: ");
gets(s);
puts("Enter the string to be searched: ");
gets(t); found = strstr(s, t); if(found)
{
printf("Second String is found in the First String at %d position.\n", found - s);
}
else
{
printf("-1");
}
getch();
}
Algorithm:
Step 1: Start
Step 2: Read the text until an empty line
Step 3: Compare each character with newline char ‘\n’ to count no of lines
Step 4: Compare each character with tab char ‘\t\’ or space char ‘ ‘ to count no of words
Step 5: Compare first character with NULL char ‘\0’ to find the end of text
Step 6: No of characters = length of each line of text
Step 7: Print no of lines, no of words, no of chars Step
8: Stop
Program:
#include <stdio.h>
#include <conio.h> #include
<string.h>
void main()
{
char str[100]; int
i = 0, l = 0, f = 1;
clrscr();
puts("Enter any string\n");
gets(str);
for(i = 0; str[i] !='\0'; i++)
{
l = l + 1;
}
printf("The number of characters in the string are %d\n", l);
for(i = 0; i <= l-1; i++)
{
if(str[i] == ' ')
{ f=
f + 1;
}
}
printf("The number of words in the string are %d", f);
getch();
}
Week-5
Enter Number1: 20
Enter Number2: 10
Expected output:
Sum=30
Subtraction=10
Multiplication=200
Division=2.0000
Before learning how to write a C program to perform all arithmetic operations we need to know
what is arithmetic operators means.
Arithmetic Operators are the operators which are used to perform various mathematical
operations such as addition, subtraction, division, multiplication etc.
2. Minus Operator (-): is used for subtract one number from another number like 23 - 8 = 15, 65
- 25 = 40.
3. Multiplication Operator (*) : is represented as an asterisk (*) symbol and it returns product
of given numbers like 5 * 5 = 25, 7 * 12 = 84 etc.
4. Division Operator (/) : is denoted by forward slash (/) symbol and it divides first number by
the second number like 35 / 5 = 7.
5. Modulus Operator (%) : is denoted by percentage (%) symbol and it returns remainder. for
ex: 25 / 5 = 0, 45 / 6 = 3 etc.
#include<stdio.h>
int main()
{
int no1,no2;
int *ptr1,*ptr2;
int sum,sub,mult;
float div;
printf("Enter number1:\n");
scanf("%d",&no1); printf("Enter
number2:\n");
scanf("%d",&no2);
sum=(*ptr1) + (*ptr2);
sub=(*ptr1) - (*ptr2); mult=(*ptr1)
* (*ptr2);
div=(*ptr1) / (*ptr2);
printf("sum= %d\n",sum);
printf("subtraction= %d\n",sub);
printf("Multiplication= %d\n",mult);
printf("Division= %f\n",div);
return 0;
}
Step by step logic of the given program:
1. Accept two numbers from user store it in variable say no1 and no2.
2. Store address of no1 in pointer variable ptr1 and store address of no2 in variable ptr2 using
reference operator (&):
ptr1=&no1; ptr2=&no2;
3. After that calculate addition, subtraction, multiplication and division using dereference
operator(*):
sum=(*ptr1) + (*ptr2);
sub=(*ptr1) - (*ptr2);
mult=(*ptr1) * (*ptr2);
div=(*ptr1) / (*ptr2);
4. Last print sum, sub, mult and div on the output screen.
Call by Value
#include<stdio.h> void
change(int num) {
printf("Before adding value inside function num=%d \n",num);
num=num+100;
printf("After adding value inside function num=%d \n", num);
}
int main() { int x=100;
printf("Before function call x=%d \n", x);
change(x);//passing value in function
printf("After function call x=%d \n", x);
return 0;
}
Output
Call by Reference
#include<stdio.h> void
change(int *num) {
printf("Before adding value inside function num=%d \n",*num);
(*num) += 100;
printf("After adding value inside function num=%d \n", *num);
}
int main() { int x=100; printf("Before
function call x=%d \n", x);
change(&x);//passing reference in function
printf("After function call x=%d \n", x);
return 0;
}
Output
#include <stdio.h>
void swap(int , int); //prototype of the function int
main()
{
int a = 10;
int b = 20;
printf("Before swapping the values in main a = %d, b = %d\n",a,b); // printing the value of a a
nd b in main swap(a,b);
printf("After swapping values in main a = %d, b = %d\n",a,b); // The value of actual parameter
s do not change by changing the formal parameters in call by value, a = 10, b = 20
}
void swap (int a, int b)
{
int temp;
temp = a;
a=b; b=temp;
printf("After swapping values in function a = %d, b = %d\n",a,b); // Formal parameters, a =
20 , b = 10
}
Output
#include <stdio.h>
void swap(int *, int *); //prototype of the function int
main()
{
int a = 10;
int b = 20;
printf("Before swapping the values in main a = %d, b = %d\n",a,b);
// printing the value of a and b in main
swap(&a,&b);
printf("After swapping values in main a = %d, b = %d\n",a,b);
// The values of actual parameters do change in call by reference, a = 10, b = 20
}
void swap (int *a, int *b)
{
int temp;
temp = *a;
*a=*b;
*b=temp;
printf("After swapping values in function a = %d, b = %d\n",*a,*b);
// Formal parameters, a = 20, b = 10
}
Output
Week-6
Write a C program to perform i)Reading a complex number ii) Writing a complex number iii)
Addition of two complex numbers iv) Multiplication of two complex numbers (Note: represent
complex number using a structure.) using Functions
#include <stdio.h>
#include <conio.h>
struct complex
{
float real, imag; }a, b, c; struct complex read(void);
void write(struct complex); struct complex
add(struct complex, struct complex); struct complex
sub(struct complex, struct complex); struct complex
mul(struct complex, struct complex); struct complex
div(struct complex, struct complex); void main ()
{
clrscr();
printf("Enter the 1st complex number\n ");
a = read();
write(a);
printf("Enter the 2nd complex number\n");
b = read(); write(b); printf("Addition\n "); c = add(a, b);
write(c); printf("Substraction\n "); c = sub(a, b);
write(c); printf("Multiplication\n"); c = mul(a, b);
write(c); printf("Division\n"); c = div(a, b); write(c);
getch();
}
struct complex read(void)
{
struct complex t; printf("Enter the
real part\n"); scanf("%f", &t.real);
printf("Enter the imaginary part\n");
scanf("%f", &t.imag); return t;
}
void write(struct complex a)
{
printf("Complex number is\n");
printf(" %.1f + i %.1f", a.real, a.imag);
printf("\n");
}
struct complex add(struct complex p, struct complex q)
{
struct complex t;
t.real = (p.real + q.real);
t.imag = (p.imag + q.imag);
return t;
}
struct complex sub(struct complex p, struct complex q)
{
struct complex t;
t.real = (p.real - q.real);
t.imag = (p.imag - q.imag);
return t;
}
struct complex mul(struct complex p, struct complex q)
{
struct complex t;
t.real=(p.real * q.real) - (p.imag * q.imag);
t.imag=(p.real * q.imag) + (p.imag * q.real);
return t;
}
struct complex div(struct complex p, struct complex q)
{
struct complex t;
t.real = ((p.imag * q.real) - (p.real * q.imag)) / ((q.real * q.real) + (q.imag * q.imag));
t.imag = ((p.real * q.real) + (p.imag * q.imag)) / ((q.real * q.real) + (q.imag * q.imag));
return(t);
}
Week 7
Write C programs that implement stack (its operations) using
i) Arrays ii) Pointers
/* C Program to implement Stack Operations Using Pointer */
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define MAX 50
int size;
int main()
{
int element, opt, val;
struct stack ptr;
init_stk(&ptr); printf("Enter
Stack Size :: "); scanf("%d",
&size);
while (1) {
printf("\n\tSTACK PRIMITIVE OPERATIONS.........\n");
printf("\n1.PUSH"); printf("\
n2.POP"); printf("\n3.DISPLAY");
printf("\n4.QUIT"); printf("\n");
printf("\nEnter your option :: ");
scanf("%d", &opt);
switch (opt)
{
case 1:
printf("\nEnter the element into stack:");
scanf("%d", &val); push(&ptr, val);
break;
case 2:
element = pop(&ptr);
printf("\nThe element popped from stack is : %d", element);
break;
case 3:
printf("\nThe current stack elements are:");
display(&ptr);
break;
case 4:
exit(0);
default:
printf("\nEnter correct option!Try again.");
}
}
return (0);
}
Output:
1.PUSH
2.POP
3.DISPLAY
4.QUIT
1.PUSH
2.POP
3.DISPLAY 4.QUIT
1.PUSH
2.POP
3.DISPLAY
4.QUIT
1.PUSH
2.POP
3.DISPLAY
4.QUIT
1.PUSH
2.POP
3.DISPLAY
4.QUIT
1.PUSH
2.POP
3.DISPLAY 4.QUIT
1.PUSH
2.POP
3.DISPLAY 4.QUIT
1.PUSH
2.POP
3.DISPLAY 4.QUIT
1.PUSH
2.POP
3.DISPLAY
4.QUIT
Enter your option :: 2
1.PUSH
2.POP
3.DISPLAY
4.QUIT
1.PUSH
2.POP
3.DISPLAY
4.QUIT
1.PUSH
2.POP
3.DISPLAY
4.QUIT
1.PUSH
2.POP
3.DISPLAY 4.QUIT
1.PUSH
2.POP
3.DISPLAY
4.QUIT
1.PUSH
2.POP
3.DISPLAY
4.QUIT
1.PUSH
2.POP
3.DISPLAY
4.QUIT
1.PUSH
2.POP
3.DISPLAY
4.QUIT
Enter your option :: 4
Process returned 0
{ case
1:
{
push();
break;
}
case 2:
{
pop();
break;
}
case 3:
{ display(
);
break;
}
case 4:
{
printf("\n\t EXIT POINT ");
break;
}
default:
{
printf ("\n\t Please Enter a Valid Choice(1/2/3/4)");
}
}
}
while(choice!=4);
return 0;
}
void push()
{
if(top>=n-1)
{
printf("\n\tSTACK is over flow");
}
else
{
printf(" Enter a value to be pushed:");
scanf("%d",&x); top++;
stack[top]=x;
}
}
void pop()
{
if(top<=-1)
{
printf("\n\t Stack is under flow");
}
else
{
printf("\n\t The popped elements is %d",stack[top]);
top--;
}
}
void display()
{
if(top>=0)
{
printf("\n The elements in STACK \n");
for(i=top; i>=0; i--) printf("\n
%d",stack[i]);
printf("\n Press Next Choice");
}
else
{
printf("\n The STACK is empty");
}
Output:
98
24
12
Press Next Choice
Enter the Choice:2
The popped elements is 98
Enter the Choice:3
24
12
Press Next Choice
Enter the Choice:4
EXIT POINT
Week 8
Write C programs that implement Queue (its operations) using
i) Arrays ii) Pointers
/*
* C Program to Implement a Queue using an Array
*/
#include <stdio.h>
#define MAX 50
{ case 1:
insert();
break;
case 2:
delete();
break;
case 3:
display();
break;
case 4:
exit(1);
default:
printf("Wrong choice \n");
} /* End of switch */
} /* End of while */
} /* End of main() */
void insert()
{
int add_item; if
(rear == MAX - 1)
printf("Queue Overflow \n");
else
{
if (front == - 1) /*If queue
is initially empty */
front = 0;
printf("Inset the element in queue : ");
scanf("%d", &add_item);
rear = rear + 1;
queue_array[rear] = add_item;
}
} /* End of insert() */
void delete()
{
if (front == - 1 || front > rear)
{
printf("Queue Underflow \n");
return ;
}
else
{
printf("Element deleted from queue is : %d\n", queue_array[front]);
front = front + 1;
}
} /* End of delete() */
void display()
{
int i; if
(front == - 1)
printf("Queue is empty \n");
else
{
printf("Queue is : \n"); for
(i = front; i <= rear; i++)
printf("%d ", queue_array[i]);
printf("\n");
}
} /* End of display() */
/* Write C programs that implement Queue (its operations) using ii) Pointers */
#define true 1
#define false 0
#include<stdio.h>
#include<conio.h> #include<process.h>
struct q_point
{ int
ele;
struct
q_poin
t* n;
};
struct q_point *f_ptr = NULL;
/*main function*/
void main()
{
int ele,choice,j;
while(1)
{ clrscr();
printf(“\n\n****IMPLEMENTATION OF QUEUE USING POINTERS****\n”);
printf(“==============================================”);
printf(“\n\t\t MENU\n”);
printf(“==============================================”);
printf(“\n\t[1] To insert an element”); printf(“\
n\t[2] To remove an element”); printf(“\n\t[3]
To display all the elements”); printf(“\n\t[4]
Exit”);
printf(“\n\n\tEnter your choice:”); scanf(“%d”,
&choice);
switch(choice)
{
case 1:
{
printf(“\n\tElement to be inserted:”);
scanf(“%d”,&ele); add_ele(ele);
getch();
break;
}
Advertisements
REPORT THIS AD
case 2:
{
if(!e_que())
{
j=rem_ele();
printf(“\n\t%d is removed from the queue”,j);
getch();
}
else
{
printf(“\n\tQueue is Empty.”);
getch();
}
break;
}
case 3:
show_ele();
getch(); break;
case 4:
exit(1); break;
default: printf(“\n\tInvalid
choice.”); getch(); break;
}
}
}
#include <stdio.h>
#include <ctype.h>
/* declare stack and its top pointer to be used during postfix expression
evaluation*/ int stack[MAXSTACK];
int top = -1; /* because array index in C begins at 0 */
/* can be do this initialization somewhere else */
/* define function that is used to input postfix expression and to evaluate it */ void
EvalPostfix(char postfix[])
{
int i;
char ch;
int val;
int A, B;
int main()
{
int i;
/* declare character array to store postfix expression */
char postfix[POSTFIXSIZE];
printf("ASSUMPTION: There are only four operators(*, /, +, -) in an expression and operand is single
digit only.\n");
printf(" \nEnter postfix expression,\npress right parenthesis ')' for end expression : ");
EvalPostfix(postfix);
return 0;
}
Output
First Run:
Enter postfix expression, press right parenthesis ')' for end expression : 456*+) Result
of expression evaluation : 34
Second Run:
Enter postfix expression, press right parenthesis ')' for end expression: 12345*+*+) Result
of expression evaluation: 47
Week 10
Write a C program that uses functions to perform the following operations on singly linked list. i)
Creation ii) Insertion iii) Deletion iv) Traversal
#include<stdio.h>
#include<conio.h>
#include<stdlib.h> int
count=0;
struct node
{
int data; struct node
*next; }*head,*new
n,*trav;
//---------------------------------------------------------- void
create_list()
{
int value; struct
node *temp;
temp=head;
newn=(struct node *)malloc(sizeof (struct node)); printf("\nenter
the value to be inserted");
scanf("%d",&value); newn->data=value;
if(head==NULL)
{
head=newn; head-
>next=NULL; count++;
}
else
{
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=newn; newn->next=NULL;
count++;
}
}
//---------------------------------------------------- void
insert_at_begning(int value)
{
newn=(struct node *)malloc(sizeof (struct node));
newn->data=value;
if(head==NULL)
{
head=newn; head->next=NULL;
count++;
}
else
{
newn->next=head; head=newn;
count++;
}
}
//---------------------------------------------------------- void
insert_at_end(int value)
{
struct node *temp; temp=head;
newn=(struct node *)malloc(sizeof (struct node));
newn->data=value;
if(head==NULL)
{
head=newn; head->next=NULL;
count++;
}
else
{
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=newn; newn->next=NULL;
count++;
}
}
//------------------------------------------------------ int
insert_at_middle()
{
if(count>=2)
{
struct node *var1,*temp; int
loc,value;
printf("\n after which value you want to insert : ");
scanf("%d",&loc); printf("\nenter the value to be
inserted"); scanf("%d",&value);
newn=(struct node *)malloc(sizeof (struct node));
newn->data=value;
temp=head;
/* if(head==NULL)
{
head=newn;
head->next=NULL;
count++; return 0;
}
else {*/
while(temp->data!=loc)
{
temp=temp->next;
if(temp==NULL)
{
printf("\nSORRY...there is no %d element",loc); return
0;
}
}
//var1=temp->next; newn->next=temp->next;//var1;
temp->next=newn;
count++;
//}
}
else
{
printf("\nthe no of nodes must be >=2");
}
}
//----------------------------------------------------------------
int delete_from_middle()
{
if(count==0)
printf("\n List is Empty!!!! you can't delete elements\n"); else
if(count>2)
{
struct node *temp,*var;
int value;
temp=head;
printf("\nenter the data that you want to delete from the list shown above");
scanf("%d",&value);
while(temp->data!=value)
{
var=temp; temp=temp->next;
if(temp==NULL)
{
printf("\nSORRY...there is no %d element",value); return
0;
}
}
if(temp==head)
{
head=temp->next;
}
else{
var->next=temp->next; temp->next=NULL;
}
count--; if(temp==NULL)
printf("Element is not avilable in the list \n**enter only middle elements..**"); else
printf("\ndata deleted from list is %d",value); free(temp);
}
else
{
printf("\nthere no middle elemts..only %d elemts is avilable\n",count);
}
}
//------------------------------------------------------------------ int
delete_from_front()
{
struct node *temp; temp=head;
if(head==NULL)
{
printf("\nno elements for deletion in the list\n"); return
0;
}
else
{
printf("\ndeleted element is :%d",head->data);
if(temp->next==NULL)
{
head=NULL;
}
else{
head=temp->next;
temp->next=NULL;
}
count--; free(temp);
}
}
//--------------------------------------------------------
int delete_from_end()
{
struct node *temp,*var; temp=head;
if(head==NULL)
{
printf("\nno elemts in the list"); return
0;
}
else{
if(temp->next==NULL )
{
head=NULL;//temp->next;
}
else{
while(temp->next != NULL)
{
var=temp;
temp=temp->next;
}
var->next=NULL;
}
printf("\ndata deleted from list is %d",temp->data); free(temp);
count--;
}
return 0;
}
//------------------------------------------------------ int
display()
{
trav=head;
if(trav==NULL)
{
printf("\nList is Empty\n"); return
0;
}
else
{
printf("\n\nElements in the Single Linked List is %d:\n",count);
while(trav!=NULL)
{
printf(" -> %d ",trav->data);
trav=trav->next;
}
printf("\n");
}
}
//--------------------------------------------------------------- int
main()
{
int ch=0; char
ch1;
head=NULL;
while(1)
{
printf("\n1.create linked list"); printf("\n2.insertion at
begning of linked list"); printf("\n3.insertion at the end
of linked list"); printf("\n4.insertion at the middle
where you want"); printf("\n5.deletion from the front
of linked list"); printf("\n6.deletion from the end of
linked list "); printf("\n7.deletion of the middle data
that you want");
printf("\n8.display the linked list"); printf("\n9.exit\n");
printf("\nenter the choice of operation to perform on linked list"); scanf("%d",&ch);
switch(ch)
{
case 1:
{
do{ create_list();
display();
printf("do you want to create list ,y / n");
getchar(); scanf("%c",&ch1);
}while(ch1=='y');
break;
}
case 2:
{
int value;
printf("\nenter the value to be inserted");
scanf("%d",&value); insert_at_begning(value);
display(); break;
}
case 3:
{
int value;
printf("\nenter value to be inserted");
scanf("%d",&value);
insert_at_end(value); display();
break;
}
case 4:
{
insert_at_middle();
display(); break;
}
case 5:
{
delete_from_front();
display(); }break;
case 6:
{
delete_from_end();
display(); break;
}
case 7:
{
display();
delete_from_middle();
display(); break;
}
case 8:
{
display(); break;
}
case 9:
{
exit(1);
}
default:printf("\n****Please enter correct choice****\n");
}
}
getch();
}
Output:
Week 11
Write a C program that uses functions to perform the following operations on Doubly linkedlist.
i) Creation ii) Insertion iii) Deletion iv) Traversal
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int count=0; struct
node
{
int data; struct node
*next,*prev; }*head,*last,
*newn,*trav;
//---------------------------------------------------- void
create_list()
{
struct node *temp;
int value; temp=last;
newn=(struct node *)malloc(sizeof (struct node));
printf("\n enter value");
scanf("%d",&value); newn->data=value;
if(last==NULL)
{
head=last=newn; head->prev=NULL;
head->next=NULL;
count++;
}
else
{
newn->next=NULL;
newn->prev=last;
last->next=newn;
last=newn; count++;
}
}
//---------------------------------------------------- void
insert_at_begning(int value)
{
newn=(struct node *)malloc(sizeof (struct node));
newn->data=value;
if(head==NULL)
{
head=last=newn;
head->prev=NULL; head->next=NULL;
count++;
}
else
{
newn->next=head; head-
>prev=newn; newn-
>prev=NULL; head=newn;
count++;
}
}
//---------------------------------------------------------- void
insert_at_end(int value)
{
struct node *temp; temp=last;
newn=(struct node *)malloc(sizeof (struct node));
newn->data=value;
if(last==NULL)
{
head=last=newn; head->prev=NULL;
head->next=NULL;
count++;
}
else
{
newn->next=NULL;
newn->prev=last;
last->next=newn;
last=newn; count++;
}
}
//------------------------------------------------------ int
insert_at_middle()
{
if(count>=2)
{
struct node *var2,*temp; int
loc,value;
printf("\nselect location where you want to insert the data"); scanf("%d",&loc);
printf("\nenter which value do u want to inserted"); scanf("%d",&value);
newn=(struct node *)malloc(sizeof (struct node));
newn->data=value; temp=head;
while(temp->data!=loc)
{
temp=temp->next;
if(temp==NULL)
{
printf("\nSORRY...there is no %d element",loc); return
0;
}
}
if(temp->next==NULL)
{
printf("\n%d is last node..please enter midddle node values ",loc) ; return;
}
var2=temp->next; temp->next=newn;
newn->next=var2; newn-
>prev=temp; var2->prev=newn;
count++;
}
else
{
printf("\nthe no of nodes must be >=2");
}
}
//----------------------------------------------------------------
int delete_from_middle()
{
if(count>2)
{
struct node *temp,*var;
int value;
temp=head;
printf("\nenter the data that you want to delete from the list shown above");
scanf("%d",&value);
while(temp!=NULL)
{
if(temp->data == value)
{
if(temp->next==NULL)//if(temp==head)
{
printf("\n\n sorry %d is last node ..please enter middle nodes only",value); return
0;
}
else
{
if(temp==head)
{
printf("\n\n %d is first node..plz enter middle nodes",value); return;
}
var->next=temp->next;
temp->next->prev=var;
free(temp); count--;
return 0;
}
}
else
{
var=temp; temp=temp->next;
}
}
if(temp==NULL)
printf("\n%d is not avilable.. enter only middle elements..",value); else
printf("\ndata deleted from list is %d",value);
}
else
{
printf("\nthere no middle elemts..only %d elemts is avilable",count);
}
}
//------------------------------------------------------------------ int
delete_from_front()
{
struct node *temp;
if(head==NULL)
{
printf("no elements for deletion in the list"); return
0;
}
else if(head->next==NULL)
{
printf("deleted element is :%d",head->data);
head=last=NULL;
}
else
{
temp=head->next; printf("deleted element
is :%d",head->data);
head->next=NULL; temp->prev=NULL;
head=temp;
count--; return
0;
}
}
//--------------------------------------------------------
int delete_from_end()
{
struct node *temp,*var;
temp=last; if(last==NULL)
{
printf("no elemts in the list"); return
0;
}
else if(last->prev==NULL)
{
printf("data deleted from list is %d",last->data);
head=last=NULL;
//free(last); count--;
return 0;
}
else{
printf("data deleted from list is %d",last->data);
var=last->prev; last->prev->next=NULL;
last=var; count--;
return 0;
}
}
//------------------------------------------------------ int
display()
{
trav=last;//head; if(trav==NULL)
{
printf("\nList is Empty"); return
0;
}
else
{
printf("\n\nElements in the List is %d:\n",count);
while(trav!=NULL)
{
printf("%d<--> ",trav->data);
trav=trav->prev;//next;
}
printf("\n");
}
}
//--------------------------------------------------------------- int
main()
{
int ch=0;
char ch1; //
clrscr();
head=NULL; last=NULL;
while(1)
{
Printf("\n Double Linked List Operations") printf("\n1.Create
Double Linked List"); printf("\n2.insertion at begning of
linked list"); printf("\n3.insertion at the end of linked list");
printf("\n4.insertion at the middle where you want");
printf("\n5.deletion from the front of linked list"); printf("\
n6.deletion from the end of linked list "); printf("\
n7.deletion of the middle data that you want");
printf("\n8.display"); printf("\n9.exit\n");
printf("\nenter the choice of operation to perform on linked list"); scanf("%d",&ch);
switch(ch)
{
case 1:
{
do{ create_list();
display();
printf("do you want to create list ,y / n");
getchar();
scanf("%c",&ch1); }while(ch1=='y');
break;
}
case 2:
{
int value;
printf("\nenter the value to be inserted");
scanf("%d",&value);
insert_at_begning(value); display();
break;
}
case 3:
{
int value; printf("\nenter value to be
inserted"); scanf("%d",&value);
insert_at_end(value); display();
break;
}
case 4:
{
insert_at_middle(); display();
break;
}
case 5:
{
delete_from_front();
display(); }break;
case 6:
{
delete_from_end();
display(); break;
}
case 7:
{
display();
delete_from_middle();
display(); break;
}
case 8:display();break; case
9:
{
exit(0);
}
}
}
getch();
}
Output:
Week 12
Write a C program that uses functions to perform the following operations on circular linkedlist.
i) Creation ii) Insertion iii) Deletion iv) Traversal
#include<stdio.h> #include<stdlib.h>
struct node
{
int data; struct
node *next;
};
struct node *head;
void beginsert (); void
lastinsert (); void
randominsert(); void
begin_delete(); void
last_delete(); void
random_delete(); void
display(); void
search();
void main ()
{
int choice =0;
while(choice != 7)
{
printf("\n*********Main Menu*********\n"); printf("\nChoose one
option from the following list ...\n"); printf("\
n===============================================\n");
printf("\n1.Insert in begining\n2.Insert at last\n3.Delete from Beginning\n4.Delete from
last\n5.Search for an element\n6.Show\n7.Exit\n"); printf("\nEnter your choice?\n");
scanf("\n%d",&choice); switch(choice)
{
case 1:
beginsert();
break; case 2:
lastinsert();
break; case 3:
begin_delete();
break;
case 4: last_delete(); break; case 5:
search(); break; case 6: display();
break; case 7: exit(0); break;
default: printf("Please enter valid
choice..");
}
}
}
void beginsert()
{
struct node *ptr,*temp; int
item;
ptr = (struct node *)malloc(sizeof(struct node)); if(ptr
== NULL)
{
printf("\nOVERFLOW");
}
else
{
printf("\nEnter the node data?");
scanf("%d",&item); ptr -> data =
item;
if(head == NULL)
{
head = ptr; ptr ->
next = head;
}
else
{
temp = head; while(temp-
>next != head) temp =
temp->next; ptr->next =
head; temp -> next = ptr;
head = ptr;
}
printf("\nnode inserted\n");
}
}
void lastinsert()
{
struct node *ptr,*temp; int
item;
ptr = (struct node *)malloc(sizeof(struct node)); if(ptr
== NULL)
{
printf("\nOVERFLOW\n");
}
else
{
printf("\nEnter Data?");
scanf("%d",&item); ptr-
>data = item;
if(head == NULL)
{
head = ptr; ptr ->
next = head;
}
else
{
temp = head; while(temp ->
next != head)
{
temp = temp -> next;
}
temp -> next = ptr;
ptr -> next = head;
}
printf("\nnode inserted\n");
}
}
void begin_delete()
{
struct node *ptr;
if(head == NULL)
{
printf("\nUNDERFLOW");
}
else if(head->next == head)
{
head = NULL; free(head);
printf("\nnode deleted\n");
}
else { ptr = head;
while(ptr -> next != head)
ptr = ptr -> next; ptr-
>next = head->next;
free(head); head = ptr-
>next;
printf("\nnode deleted\n");
}
}
void last_delete()
{
struct node *ptr, *preptr;
if(head==NULL)
{
printf("\nUNDERFLOW");
}
else if (head ->next == head)
{
head = NULL; free(head);
printf("\nnode deleted\n");
}
else
{
ptr = head; while(ptr -
>next != head)
{
preptr=ptr;
ptr = ptr->next;
}
preptr->next = ptr -> next; free(ptr);
printf("\nnode deleted\n");
}
}
void search()
{ struct node *ptr;
int item,i=0,flag=1;
ptr = head; if(ptr
== NULL)
{
printf("\nEmpty List\n");
}
else
{
printf("\nEnter item which you want to search?\n"); scanf("%d",&item);
if(head ->data == item)
{
printf("item found at location %d",i+1); flag=0;
}
else
{
while (ptr->next != head)
{
if(ptr->data == item)
{
printf("item found at location %d ",i+1);
flag=0; break;
}
else
{
flag=1;
}
i++; ptr = ptr ->
next;
}
} if(flag !=
0)
{
printf("Item not found\n");
}}}
void display()
{
struct node *ptr; ptr=head;
if(head == NULL)
{
printf("\nnothing to print");
}
else
{
printf("\n printing values ... \n"); while(ptr
-> next != head)
{
printf("%d\n", ptr -> data);
ptr = ptr -> next;
}
printf("%d\n", ptr -> data);
}
}
Output:
// Stack Operations
struct Stack* createStack( unsigned capacity )
{
struct Stack* stack = (struct Stack*) malloc(sizeof(struct Stack));
return stack;
}
int isEmpty(struct Stack* stack)
{
return stack->top == -1 ;
}
char peek(struct Stack* stack)
{
return stack->array[stack->top];
}
char pop(struct Stack* stack)
{
if (!isEmpty(stack)) return stack-
>array[stack->top--] ; return '$';
}
void push(struct Stack* stack, char op)
{
stack->array[++stack->top] = op;
}
// The main function that returns value of a given postfix expression int
evaluatePostfix(char* exp)
{
// Create a stack of capacity equal to expression size
struct Stack* stack = createStack(strlen(exp));
int i;
Week 14
Write C programs that use both recursive and non-recursive functions to perform the following
searching operations for a key value in a given list of integers: i) Linear search ii) Binary search
// Write C programs that use both recursive and non recursive functions
// to perform the following searching operation for a Key value in a given list of integers : //
i) Linear search
#include <stdio.h>
#include <conio.h>
#define MAX_LEN 10
int main()
{
int l[MAX_LEN], num, ele;
int ch;
printf("======================================================"); printf("\n\t\t\
tMENU");
printf("\n=====================================================");
printf("\n[1] Linary Search using Recursion method");
printf("\n[2] Linary Search using Non-Recursion method");
printf("\n\nEnter your Choice:"); scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\n**Recursion method**\n");
l_search_recursive(l,num,ele);
getch();
break;
case 2:
printf("\n**Non-Recursion method**\n");
l_search_nonrecursive(l,num,ele);
getch();
break;
}
}
getch();
}
//end main
//Non-Recursive method
#include<stdio.h> #include<conio.h>
void main()
{
int a[20], i, n, key, low, high, mid; clrscr();
printf(“Enter the array elements in ascending order”);
for(i = 0; i < n; i++)
{
scanf(“%d”, &a[i]);
}
printf(“Enter the key element\n”);
scanf(“%d”, &key);
low = 0;
high = n - 1;
while(high >= low)
{
mid = (low + high) / 2;
if(key == a[mid])
break;
else
{
if(key > a[mid])
low = mid + 1; else
high = mid - 1;
}
}
if(key == a[mid])
printf(“The key element is found at location %d”, mid + 1); else
printf(“the key element is not found”);
getch();
}
Week 15
Write a C program that implements the following sorting methods to sort a given list of integers
in
ascending order
i) Bubble sort ii)
Selection sort
iii) Insertion sort
#include<stdio.h>
int main(){
return 0;
}
Output:
#include<stdio.h> int
main(){
/* Here i & j for loop counters, temp for swapping,
* count for total number of elements, number[] to
* store the input numbers in array. You can increase
* or decrease the size of number array as per requirement
*/
int i, j, count, temp, number[25];
return 0;
}
Output:
int main(){
return 0;
}
Output: