Computer Lab Manual
Computer Lab Manual
1
INDEX
1 VISION/MISION 4
2. PEO 4
3. POS 5
4. COS 6
5. MAPPING OF CO & PO 6
6. SYLLABUS 7
7. BOOKS 8
8. INSTRUCTIONAL METHODS 8
9. LEARNING MATERIALS 8
Exp:-4 21
Objectives: - Write a program for addition, multiplication and transpose of
matrices.
2
Exp:-6 Objectives: - Write a program to implement singly linked list 26
Exp:-8 Objectives: - write a program to implement queue using array and linked list 38
Exp:-9 Objectives: - Write a program to implement stack using array and linked list 39
3
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTER
MISSION:
2. PEO
i. To enrich students with fundamental knowledge, effective computing, problem solving
and communication skills enable them to have successful career in Information
Technology.
ii. To enable students in acquiring Information Technology's latest tools, technologies and
management principles to give them an ability to solve multidisciplinary engineering
problems.
iii. To impart students with ethical values and commitment towards sustainable
development in collaborative mode.
iv. To imbibe students with research oriented and innovative approaches which help them
to identify, analyze, formulate and solve real life problems and motivates them for
lifelong learning.
v. To empower students with leadership quality and team building skills that prepare them
for employment, entrepreneurship and to become competent professionals to serve
societies and global needs.
3. PROGRAM OUTCOMES
4
1. Engineering Knowledge: Apply the knowledge of mathematics, science, engineering
fundamentals, and an engineering specialization to the solution of complex
engineering problems in IT.
2. Problem analysis: Identify, formulate, research literature, and analyze complex
engineering problems reaching substantiated conclusions using first principles of
mathematics, natural sciences, and engineering sciences in IT.
3. Design/development of solutions: Design solutions for complex engineering
problems and design system components or processes that meet the specified needs
with appropriate consideration for the public health and safety, and the cultural,
societal, and environmental considerations using IT.
4. Conduct investigations of complex problems: Use research-based knowledge and
research methods including design of experiments, analysis and interpretation of data,
and synthesis of the information to provide valid conclusions using IT.
5. Modern tool usage: Create, select, and apply appropriate techniques, resources, and
modern engineering and IT tools including prediction and modeling to complex
engineering activities with an understanding of the limitations in IT.
6. The engineer and society: Apply reasoning informed by the contextual knowledge
to assess societal, health, safety, legal and cultural issues and the consequent
responsibilities relevant to the professional engineering practice using IT.
7. Environment and sustainability: Understand the impact of the professional
engineering solutions in societal and environmental contexts, and demonstrate the
knowledge of, and need for sustainable development in IT.
8. Ethics: Apply ethical principles and commit to professional ethics and
responsibilities and norms of the engineering practice using IT.
9. Individual and team work: Function effectively as an individual, and as a member
or leader in diverse teams, and in multidisciplinary settings in IT.
10. Communication: Communicate effectively on complex engineering activities with
the engineering community and with society at large, such as, being able to
comprehend and write effective reports and design documentation, make effective
presentations, and give and receive clear instructions.
11. Project Management and finance: Demonstrate knowledge and understanding of
the engineering and management principles and apply these to one’s own work, as a
member and leader in a team, to manage IT projects and in multidisciplinary
environments.
12. Life –long Learning: Recognize the need for, and have the preparation and ability to
engage in independent and life-long learning in the broadest context of technological
changes needed in IT.
5
PROGRAM PROGRAM OUTCOMES
OBJECTIVE
S 1 2 3 4 5 6 7 8 9 10 11 12
I H L H
II M H M H H L H
III L H M H L M
IV L M H M H M
V M M
5. COURSE OUTCOMES
Graduates would be able:
1. To implement various searching and sorting techniques on linear/non linear data structures to
solve various computing problems.
2. To implement various operations on non linear data structures using linked lists.
3. To implement recursive/non recursive functions to perform various operations on data
structures.
4. To design a suitable data structure and algorithm to solve a real world problem
6. MAPPING OF CO & PO
COURSE PROGRAM OUTCOMES
OUTCOMES
1 2 3 4 5 6 7 8 9 10 11 12
I H H H H H L M L H M H H
II H H H H H L M L H M H H
III H H H H H L M L H M H H
IV H H H H H L M L H M H H
7. SYLLABUS
6
3IT8A Computer Programming Lab-I
7
Class: III Sem. B.Tech. Branch: Electronics & Communications
Schedule per Week Practical Hrs.: 2 Examination Time = Three (3) Hours
Maximum Marks = 75 [Sessional / Mid-term (45) & End-term (30)]
Outcomes: At the end of the semester, the students should have clearly understood and implemented
the following:
1. Perform the searching algorithms.
2. Perform various sorting algorithms.
3. Implementation of various types of linked list
4. Perform queue using array and linked list
5. Perform stack using array and linked list
8. BOOKS:-
1. Schaum, Schaum’s Outline Series of theory and problems of Data Structures, Edition2002, Tata
McGraw Hill
8.2 Reference Books:-
I. Algorithms
I. Problem solving
8
1. End term Practical exam (Conducted by RTU, KOTA)
2. Daily Lab interaction.
INSTRUCTIONS OF LAB
DO’s
1. Please switch off the Mobile/Cell phone before entering Lab.
2. Enter the Lab with complete source code and data.
3. Check whether all peripheral are available at your desktop before proceeding for
program.
4. Intimate the lab In charge whenever you are incompatible in using the system or
in case software get corrupted/ infected by virus.
5. Arrange all the peripheral and seats before leaving the lab.
6. Properly shutdown the system before leaving the lab.
7. Keep the bag outside in the racks.
8. Enter the lab on time and leave at proper time.
9. Maintain the decorum of the lab.
10. Utilize lab hours in the corresponding experiment.
11. Get your Cd / Pen drive checked by lab In charge before using it in the lab.
DON’TS
1. No one is allowed to bring storage devices like Pan Drive /Floppy etc. in the lab.
2. Don’t mishandle the system.
3. Don’t leave the system on standing for long
4. Don’t bring any external material in the lab.
5. Don’t make noise in the lab.
9
6. Don’t bring the mobile in the lab. If extremely necessary then keep ringers off.
7. Don’t enter in the lab without permission of lab Incharge.
8. Don’t litter in the lab.
9. Don’t delete or make any modification in system files.
10. Don’t carry any lab equipments outside the lab.
We need your full support and cooperation for smooth functioning of the
10
INSTRUCTIONS FOR STUDENT
All the students are supposed to prepare the theory regarding the next program.
Students are supposed to bring the practical file and the lab copy.
Previous programs should be written in the practical file.
Any student not following these instructions will be denied entry in the lab.
11
Experiment No. 1
Aim:
Description:
Let A be an array of n elements. Sorting A refers to the operation of rearranging the elements of A so they
are in increasing order. i.e. so that,
A[1] < A[2] < A[3] < A[4] < ……. < A[N]
Bubble Sort:
Algorithm:
BUBBLESORT (A [MAX], N)
[BUBBLESORT is a procedure to arrange the elements of array A (0: N-1) in sorted order.
Where N is the number of elements insert in the array and MAX is the size of an array. ]
Source Code:
#include<iostream.h>
#include<conio.h>
void main()
{
12
clrscr();
int x[50],temp,n;
cout<<"Enter the size of array : \n";
cin>>n;
cout<<"Enter the elements of array : \n";
for(int i=0;i<n;i++)
cin>>x[i];
for(i=0;i<n-1;i++)
{
for(int j=0;j<(n-i-1);j++)
{
if(x[j]>x[j+1])
{
temp=x[j];
x[j]=x[j+1];
x[j+1]=temp;
}
}
cout<<"\nArray after "<<i+1<<" phase is : \n";
for(int k=0;k<n;k++)
cout<<x[k]<<" ";
}
cout<<"\nThe sorted array is : \n";
for(i=0;i<n;i++)
cout<<x[i]<<" ";
getch();
}
Selection Sort:
Algorithm:
min = a[i]
Swap (min,a[j])
2. Exit
Source Code:
#include<iostream.h>
#include<conio.h>
void main()
13
{
clrscr();
int x[50],temp,n;
cout<<"Enter the size of array : \n";
cin>>n;
cout<<"Enter the elements of array : \n";
for(int i=0;i<n;i++)
cin>>x[i];
for(i=0;i<n-1;i++)
{
int pos=i,min=x[i];
for(int j=i+1;j<n;j++)
{
if(min>x[j])
{
min=x[j];
pos=j;
}
}
temp=x[pos];
x[pos]=x[i];
x[i]=temp;
cout<<"\nArray after "<<i+1<<" phase is : \n";
for(int k=0;k<n;k++)
cout<<x[k]<<" ";
}
cout<<"\nThe sorted array is : \n";
for(i=0;i<n;i++)
cout<<x[i]<<" ";
getch();
}
Insertion Sort:
Algorithm:
INSERTIONSORT (A [MAX], N)
[The procedure INSERTIONSORT is used to arrange the elements of array A (0: N-1) in sorted
order. Where N is the number of elements insert in the array and MAX is the size of an array.]
14
Step 7: Repeat step 8 for I: = 0 to N-1 by 1 do:
Step 8: Write A [I]
[End of for loop]
Step 9: Stop
Source Code:
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int x[50],temp,n;
cout<<"Enter the size of array : \n";
cin>>n;
cout<<"Enter the elements of array : \n";
for(int i=0;i<n;i++)
cin>>x[i];
for(i=0;i<n-1;i++)
{
int j=i,y=x[i+1];
while(j>=0&&y<x[j])
{
x[j+1]=x[j];
j=j-1;
}
x[j+1]=y;
}
cout<<"\nThe sorted array is : \n";
for(i=0;i<n;i++)
cout<<x[i]<<" ";
getch();
}
Output:
Given array is
12, 11, 13, 5, 6, 7
Sorted array is
5 6 7 11 12 13
15
Viva Questions:
16
Experiment No. 2
Aim:
Description:
General principle of Divide & Conquer: If a problem is given we divide it into no. of sub-problems if we
get solution of each part then we stop at that point. Otherwise we still divide the problem as we solve all
individual sub-problems at last combine all these solution which gives solution of main problem.
Algorithm:
Source Code:
#include<stdio.h>
#include<conio.h>
void main()
int a[10],n,i,j,temp;
int beg,end,mid,target;
17
clrscr();
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
beg=0;
end=n;
mid=(beg+end)/2;
scanf("%d",&target);
if(target<a[mid])
end=mid-1;
else
beg=mid+1;
mid=(beg+end)/2;
if(a[mid]==target)
else
18
printf("\nThe number is not found");
getch();
OutPut:
Viva Questions:
1. Define array.
2. If there is more than one position of item is exist, then what will be the position of item?
Experiment No. 3
Source Code:
#include <stdio.h>
19
int main()
{
int array[100], search, c, n;
return 0;
}
Output:
Position of an element: 3
Viva questions:
1. Define array.
2. If there is more than one position of item is exist, then what will be the position of item?
20
Experiment No.4
Algorithm
Matmul(a,b,m,n,p)
1 for(i=1 to m)
2 for(j = 1 to p)
3 c[i][j] =0;
4 for(k= 1to n)
5 c[i][j] = c[i][j]+a[i][j]*b[i][j]
6 exit
Output:
356
111
456
789
42 51 60
64 79 120
11 15 18
Algorithm
Matadd(a,b,m,n)
1 for (i=1 to m
2 for(j= 1 to n)
3c[i][j] = a[i][j]+b[i][j]
4 exit
21
Output:
356
111
456
789
3 5 7
7 10 12
8 9 10
Algorithm
Transpose(a,m,n)
1 for(i= 1 to m) for(j= 1 to n)
b[i][j]= a[j][i]
2 for (i=1to m) for (j= 1to n)
a[i][j]= b[i][j]
Exit
Output:
356
111
231
351
22
461
Viva Questions:
Experiment No. 5
Aim:
Description:
Like Merge Sort, QuickSort is a Divide and Conquer algorithm. It picks an element as pivot and partitions
the given array around the picked pivot. There are many different versions of quickSort that pick pivot in
different ways:
The key process in quickSort is partition(). Target of partitions is, given an array and an element x of
array as pivot, put x at its correct position in sorted array and put all smaller elements (smaller than x)
before x, and put all greater elements (greater than x) after x. All this should be done in linear time.
Partition Algorithm
There can be many ways to do partition, following code adopts the method given in CLRS book. The
logic is simple, we start from the leftmost element and keep track of index of smaller (or equal to)
elements as i. While traversing, if we find a smaller element, we swap current element with arr[i].
Otherwise we ignore current element.
Algorithm:
4. low = low +1
23
5. Repeat step 6 while (a[high] > key)
6. high = high – 1
a) temp = a[low]
b) a[low] = a[high]
e) high = high + 1
10. Exit
1. Top:= NULL
2. If N>1, then Top:= Top+1, Lower[1]:=N
3. Repeat steps 4 to 7 while Top!=NULL
4. [pop sublist from stacks]
Set low= Lower[Top], high:= Upper[Top], Top:= Top-1
5. return
Source Code:
#include<stdio.h>
int main(){
int x[20],size,i;
quicksort(x,0,size-1);
24
printf("Sorted elements: ");
for(i=0;i<size;i++)
printf(" %d",x[i]);
return 0;
}
if(first<last){
pivot=first;
i=first;
j=last;
while(i<j){
while(x[i]<=x[pivot]&&i<last)
i++;
while(x[j]>x[pivot])
j--;
if(i<j){
temp=x[i];
x[i]=x[j];
x[j]=temp;
}
}
temp=x[pivot];
x[pivot]=x[j];
x[j]=temp;
quicksort(x,first,j-1);
quicksort(x,j+1,last);
}
}
Output:
Sorted array 3 5 7 12 15
Viva Questions:
25
i. What is the average case, best case and worst case complexity for quick sort
algorithm complexity of quick sort?
ii. Difference between merge sort and quick sort.
Experiment No. 6
1. t = newmode( )
2. Enter info to be inserted
3. Read n
4. t Æ info = n
5. t Æ next = start
6. Start = t
INSERTION
BEGIN
1. t Æ next = start
2. start = t
Return
MIDDLE
LAST
1. p = start
2. Repeat step 3 until p Æ next NULL
3. p = p Æ next
4. t Æ next = NULL
5. p Æ next = t
6. Return
DELETION
BEGIN
1. x = start
2. start = start Æ next
3. delnode(x)
MIDDLE
26
1. Enter the info of node to be deleted
2. Read n
3. p = start
4. c = start
5. while (c Æ info < > NULL)
p=c
c = c Æ next
6. p Æ next = c Æ next
7. delnode ( c )
8. Return
LAST
1. p = start
c = start
2. while (cÆnext < > NULL)
p=c
c = cÆnext
3. p Æ next = c Æ next
4. delnode ( c)
5. Return
TRAVERSAL
1. p = start
2. while (p < > NULL)
Print p Æ info
P = p Æ next
4. Return
Source code:
#include<stdio.h>
#include<stdlib.h>
typedef struct Node
{
int data;
struct Node *next;
}node;
void insert(node *pointer, int data)
{
/* Iterate through the list till we encounter the last node.*/
while(pointer->next!=NULL)
{
pointer = pointer -> next;
}
27
/* Allocate memory for the new node and put data in it.*/
pointer->next = (node *)malloc(sizeof(node));
pointer = pointer->next;
pointer->data = data;
pointer->next = NULL;
}
int find(node *pointer, int key)
{
pointer = pointer -> next; //First node is dummy node.
/* Iterate through the entire linked list and search for the key. */
while(pointer!=NULL)
{
if(pointer->data == key) //key is found.
{
return 1;
}
pointer = pointer -> next;//Search in the next node.
}
/*Key is not found */
return 0;
}
void delete(node *pointer, int data)
{
/* Go to the node for which the node next to it has to be deleted */
while(pointer->next!=NULL && (pointer->next)->data != data)
{
pointer = pointer -> next;
}
if(pointer->next==NULL)
{
printf("Element %d is not present in the list\n",data);
return;
}
/* Now pointer points to a node and the node next to it has to be removed */
node *temp;
temp = pointer -> next;
28
/*temp points to the node which has to be removed*/
pointer->next = temp->next;
/*We removed the node which is next to the pointer (which is also temp) */
free(temp);
/* Beacuse we deleted the node, we no longer require the memory used for it .
free() will deallocate the memory.
*/
return;
}
void print(node *pointer)
{
if(pointer==NULL)
{
return;
}
printf("%d ",pointer->data);
print(pointer->next);
}
int main()
{
/* start always points to the first node of the linked list.
temp is used to point to the last node of the linked list.*/
node *start,*temp;
start = (node *)malloc(sizeof(node));
temp = start;
temp -> next = NULL;
/* Here in this code, we take the first node as a dummy node.
The first node does not contain data, but it used because to avoid handling special cases
in insert and delete functions.
*/
printf("1. Insert\n");
printf("2. Delete\n");
printf("3. Print\n");
printf("4. Find\n");
while(1)
{
29
int query;
scanf("%d",&query);
if(query==1)
{
int data;
scanf("%d",&data);
insert(start,data);
}
else if(query==2)
{
int data;
scanf("%d",&data);
delete(start,data);
}
else if(query==3)
{
printf("The list is ");
print(start->next);
printf("\n");
}
else if(query==4)
{
int data;
scanf("%d",&data);
int status = find(start,data);
if(status)
{
printf("Element Found\n");
}
else
{
printf("Element Not Found\n");
}
}
30
}
}
Output:
Enter the list: 12->100 13->101 14->104 15->NULL
Insert item at first position: 11
New list: 11->1000 12->100 13->101 14->104 15->NULL
Delete an item from last position:
Delete item: 15
New list: 11->1000 12->100 13->101 14->NULL
Viva Questions:
Experiment No. 7
Aim:
Write a program to implement double linked list
1. t = new node
2. Enter “the info to be inserted”
3. Read n
4. t Æ info = n
5. t Æ next = NULL
6. t Æ prev NULL
INSERTION
BEGIN
1. If start = NULL
start = t
2. else
t Æ next = NULL
t Æ next Æ prev = t
start = t
Return
MIDDLE
1. Print “ enter info of the node after which you want to insert”
2. Read x
3. p = start
4. Repeat while p< > NULL If
(pÆ info = n)
tÆnext = pÆ next
31
pÆnext = t
t Æ prev = p
p Æ nextÆ prev = t
Return
Else
P = pÆ next
5. Print x not found
tÆnext = NULL
pÆnext = t
DELETION
BEGIN
1. p = start
2. pÆnextÆprev = NULL
3. start = pÆnext
4. start = pÆnext
5. delnode(p)
6. Return
MIDDLE
1. Enter “info of the node to be deleted”
2. Read x
3. p = start
4. Repeat until p< > NULL
If(pÆinfo = x) pÆprevÆnext =
pÆnext pÆ next Æ prev = pÆprev
delnode(p)
Return
Else
P = pÆ next
5. Print “x not found”
LAST
1. P = start
2. Repeat while p< > NULL
If(pÆnext = NULL)
Delnode(p)
3. Return
DISPLAY
1. p = start
2. Repeat while p < > NULL
Print pÆinfo
P = p Æ next
Source code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
struct node {
32
int data;
int key;
return length;
}
//display the list in from first to last
void displayForward(){
//start from the beginning
struct node *ptr = head;
while(ptr != NULL){
printf("(%d,%d) ",ptr->key,ptr->data);
ptr = ptr->next;
}
printf(" ]");
}
//display the list from last to first
void displayBackward(){
//start from the last
struct node *ptr = last;
while(ptr != NULL){
//print data
printf("(%d,%d) ",ptr->key,ptr->data);
33
//move to next item
ptr = ptr ->prev;
printf(" ");
}
printf(" ]");
}
if(isEmpty()){
//make it the last link
last = link;
}else {
//update first prev link
head->prev = link;
}
//point it to old first link
link->next = head;
if(isEmpty()){
//make it the last link
last = link;
}else {
//make link a new last link
last->next = link;
//mark old last node as prev of new link
link->prev = last;
}
//point last to new last node
last = link;
}
//delete first item
struct node* deleteFirst(){
//save reference to first link
struct node *tempLink = head;
34
if(head->next == NULL){
last = NULL;
}else {
head->next->prev = NULL;
}
head = head->next;
//return the deleted link
return tempLink;
}
//delete link at the last location
struct node* deleteLast(){
//save reference to last link
struct node *tempLink = last;
last = last->prev;
if(current->next == NULL){
return NULL;
}else {
//store reference to current link
previous = current;
35
}else {
//bypass the current link
current->prev->next = current->next;
}
if(current == last){
//change last to point to prev link
last = current->prev;
}else {
current->next->prev = current->prev;
}
return current;
}
bool insertAfter(int key, int newKey, int data){
//start from the first link
struct node *current = head;
//create a link
struct node *newLink = (struct node*) malloc(sizeof(struct node));
newLink->key = key;
newLink->data = data;
if(current == last) {
newLink->next = NULL;
last = newLink;
}else {
newLink->next = current->next;
current->next->prev = newLink;
}
newLink->prev = current;
current->next = newLink;
return true;
}
main() {
insertFirst(1,10);
insertFirst(2,20);
insertFirst(3,30);
insertFirst(4,1);
insertFirst(5,40);
insertFirst(6,56);
36
printf("\nList (First to Last): ");
displayForward();
printf("\n");
printf("\nList (Last to first): ");
displayBackward();
printf("\nList , after deleting first record: ");
deleteFirst();
displayForward();
printf("\nList , after deleting last record: ");
deleteLast();
displayForward();
printf("\nList , insert after key(4) : ");
insertAfter(4,7, 13);
displayForward();
Viva Questions:
37
Experiment No.8
Aim:
write a program to implement queue using array and linked list
CREATE
1. t = new node
2. Enter info to be inserted
3. Read n
4. t Æ info = n
5. t Æ next = front
6. front = t
INSERTION
1. r Æ next = t
2. t Æ next = NULL
3. Return
DELETION
1. x = front
2. front = front Æ next
3. delnode(x)
4. Return
DISPLAY
1. If (front = NULL)
Print “ empty queue”
Return
Else
P = start
Repeat until (p< > NULL)
Print p Æ info
P = pÆ next
Return
Output:
Enter the list: 107<-12->100 1000<-13->101 100<-14->104 101<-15->NULL
Insert item at first position: 11
New list: 108<-11->1000 107<-12->100 1000<-13->101 100<-14->104
101<-15->NULL
Delete an item from last position:
Delete item: 15
New list: 108<-11->1000 107<-12->100 1000<-13->101 100<-14-> NULL
38
Viva Questions:
1. What is queue?
Experiment No. 9
Aim:
Write a program to implement stack using array and linked list
Using array:
INSERTION
PUSH(item)
1. If (item = max of stack)
Print “overflow”
Return
2. top = top + 1
3. stack[top] = item
4. Return
DELETION
POP(item)
1. If (top = - 1)
Print “underflow”
Return
2. Item = stack[top]
3. top = top – 1
4. Return
DISPLAY
1. If top = - 1
Print “underflow”
2. repeat step 3 for i = top to i >= 0
3. Print stack[i]
4. Return
Using Linked List
PUSH( )
1. t = newnode( )
2. Enter info to be inserted
3. Read n
4. tÆinfo = n
5. tÆnext = top
6. top = t
7. Return
POP( )
1. If (top = NULL) Print
“ underflow” Return
2. x = top
3. top = top Æ next
39
4. delnode(x)
5. Return
Output:
Enter the no of elements: 5
Enter the elements in stack: 5 10 15 16 17
New element: 9
New stack: 5 10 15 16 17
#include <stdio.h>
#include <stdlib.h>
struct node
{
int info;
struct node *ptr;
}*top,*top1,*temp;
int topelement();
void push(int data);
void pop();
void empty();
void display();
void destroy();
void stack_count();
void create();
int count = 0;
void main()
{
int no, ch, e;
printf("\n 1 - Push");
printf("\n 2 - Pop");
printf("\n 3 - Top");
printf("\n 4 - Empty");
printf("\n 5 - Exit");
printf("\n 6 - Dipslay");
printf("\n 7 - Stack Count");
printf("\n 8 - Destroy stack");
create();
while (1)
{
printf("\n Enter choice : ");
scanf("%d", &ch);
switch (ch)
{
case 1:
printf("Enter data : ");
scanf("%d", &no);
push(no);
break;
case 2:
pop();
40
break;
case 3:
if (top == NULL)
printf("No elements in stack");
else
{
e = topelement();
printf("\n Top element : %d", e);
}
break;
case 4:
empty();
break;
case 5:
exit(0);
case 6:
display();
break;
case 7:
stack_count();
break;
case 8:
destroy();
break;
default :
printf(" Wrong choice, Please enter correct choice ");
break;
}
}
}
/* Create empty stack */
void create()
{
top = NULL;
}
/* Count stack elements */
void stack_count()
{
printf("\n No. of elements in stack : %d", count);
}
/* Push data into stack */
void push(int data)
{
if (top == NULL)
{
top =(struct node *)malloc(1*sizeof(struct node));
top->ptr = NULL;
top->info = data;
}
else
{
temp =(struct node *)malloc(1*sizeof(struct node));
temp->ptr = top;
temp->info = data;
top = temp;
}
count++;
}
/* Display stack elements */
void display()
41
{
top1 = top;
if (top1 == NULL)
{
printf("Stack is empty");
return;
}
while (top1 != NULL)
{
printf("%d ", top1->info);
top1 = top1->ptr;
}
}
/* Pop Operation on stack */
void pop()
{
top1 = top;
if (top1 == NULL)
{
printf("\n Error : Trying to pop from empty stack");
return;
}
else
top1 = top1->ptr;
printf("\n Popped value : %d", top->info);
free(top);
top = top1;
count--;
}
/* Return top element */
int topelement()
{
return(top->info);
}
/* Check if stack is empty or not */
void empty()
{
if (top == NULL)
printf("\n Stack is empty");
else
printf("\n Stack is not empty with %d elements", count);
}
/* Destroy entire stack */
void destroy()
{
top1 = top;
while (top1 != NULL)
{
top1 = top->ptr;
free(top);
top = top1;
top1 = top1->ptr;
}
free(top1);
top = NULL;
42
printf("\n All stack elements destroyed");
count = 0;
}
Output
1 - Push
2 - Pop
3 - Top
4 - Empty
5 - Exit
6 - Dipslay
7 - Stack Count
8 - Destroy stack
Enter choice : 1
Enter data : 56
Enter choice : 1
Enter data : 80
Enter choice : 2
Popped value : 80
Enter choice : 3
Top element : 56
Enter choice : 1
Enter data : 78
Enter choice : 1
Enter data : 90
Enter choice : 6
90 78 56
Enter choice : 7
No. of elements in stack : 3
Enter choice : 8
All stack elements destroyed
Enter choice : 4
Stack is empty
Enter choice : 5
Viva Questions:
1. What is stack?
2. What are the difference between stack and queue?
43
Experiment No. 10
ENTER (a[10],n)
1. Repeat step 2 for i = 0 to (n-1)
2. Input a[i]
3. Return
DISPLAY(c[20],p)
1. Repeat step 2 for k = 0 to p-1
2. Print c[k]
3. Return
MAIN( )
1. Start
st nd
2. Input no. of elements in 1 & 2 array as ‘n’ & ‘m’
3. Enter (a.n)
4. Enter (b,m)
5. i = j = k = 0
6. Repeat step 7 to 12 while ((i < n)&&(j < m))
7. If (a[i] >= b[j]),goto step 9
8. c[k+1] = a[i+1]
9. If a[i] = b[j] ,goto step 11
10. c[k++] = b[j++]
goto step 7
11. c[k++] = a[i++]
12. j++
13. Repeat step 14 while (i<n)
14. c[k++] = a[i++]
15. Repeat step 16 while m > j
16. c[k++] = b[j++]
17. Display merged arrays as display(c;k)
18. Exit
#include <stdio.h>
44
int main() {
int a[100], b[100], m, n, c, sorted[200];
merge(a, m, b, n, sorted);
printf("Sorted array:\n");
return 0;
}
j = k = 0;
45
sorted[i] = a[j];
j++;
i++;
}
}
}
}
Output:
Enter first array: 5 15 20 25
Enter first array: 3 10 11 45
After merging:
3 5 10 11 15 20 25 45
Viva Question:
1. What is an array?
46