0% found this document useful (0 votes)
6 views47 pages

Computer Programming Lab-I

The document is a lab manual for the Computer Programming Lab-I at Jaipur Engineering College and Research Centre, detailing various sorting algorithms (bubble sort, selection sort, insertion sort) and searching algorithms (binary search, linear search) with their respective algorithms and source codes. It includes experiment objectives, descriptions, algorithms, source codes, outputs, and viva questions for each experiment. The manual aims to provide practical programming experience for second-year Electronics and Communication Engineering students.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views47 pages

Computer Programming Lab-I

The document is a lab manual for the Computer Programming Lab-I at Jaipur Engineering College and Research Centre, detailing various sorting algorithms (bubble sort, selection sort, insertion sort) and searching algorithms (binary search, linear search) with their respective algorithms and source codes. It includes experiment objectives, descriptions, algorithms, source codes, outputs, and viva questions for each experiment. The manual aims to provide practical programming experience for second-year Electronics and Communication Engineering students.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 47

Jaipur Engineering College and Research Centre Department of Electronics &

Communication Engineering

LAB-MANUAL
(II Year III SEM ECE)

Lab Name : Computer Programming Lab-I

Lab Code : 3EC8A

Branch : Electronics & Communication Engineering

Experiment Object:-
Introduction of sorting methods Implementation of bubble, selection and insertion sort.

Jaipur Engineering College and Research Centre


Shri Ram ki Nangal, via Sitapura RIICO
Tonk Road, Jaipur-302 022, Rajasthan

EXPERIMENT NO:-1
Object:- Introduction of sorting methods Implementation of bubble, selection and insertion sort.

(ECE/LAB MANUAL/3EC8A COMPUTER PROGRAMMING LAB-I) Page 1


Jaipur Engineering College and Research Centre Department of Electronics &
Communication Engineering

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]

Example: A[]: 15, 5, 3, 6, 9, 1, 2

After Soritng A[]: 1, 2, 3, 5, 6, 9, 15

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. ]

Step 1: Repeat steps 2 to 3 for I: = 0 to N-2 by 1 do:


Step 2: Repeat step 3 for J: = 0 to N-I-2 by 1 do:
Step 3: If A [J] > A [J+1] then:
a. Set TEMP: = A[J]
b. Set A[J]: = A[J+1]
c. Set A[J+1]: = TEMP
[End of If structure]
[End of step 2 for loop]
[End of step 1 for loop]
Step 4: Repeat step 5 for I: = 0 to N-1 by 1 do:
Step 5: Write A [I]
Step 6: Stop

Source Code:

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int x[50],temp,n;

(ECE/LAB MANUAL/3EC8A COMPUTER PROGRAMMING LAB-I) Page 2


Jaipur Engineering College and Research Centre Department of Electronics &
Communication Engineering

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:

1. For (i = 0; i <=n-2 ; i++)

min = a[i]

for (j = i+1 ; j <=n-1 ; j++) If (min >a[j])

Swap (min,a[j])

2. Exit

Source Code:

#include<iostream.h>
#include<conio.h>
void main()

(ECE/LAB MANUAL/3EC8A COMPUTER PROGRAMMING LAB-I) Page 3


Jaipur Engineering College and Research Centre Department of Electronics &
Communication Engineering

{
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.]

(ECE/LAB MANUAL/3EC8A COMPUTER PROGRAMMING LAB-I) Page 4


Jaipur Engineering College and Research Centre Department of Electronics &
Communication Engineering

Step 1: Repeat steps 2 to 6 for I: = 0 to N-2 by 1 do:


Step 2: set J: = I, Y: = A [I+1]
Step 3: Repeat steps 4 and 5 while J>=0 and Y< A [J]
Step 4: set A [J+1]: = A [J]
Step 5: set J: = J-1
[End of step 3 While loop]
Step 6: set A [J+1]: = Y
[End of step 1 for loop]
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<<"\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();
}

(ECE/LAB MANUAL/3EC8A COMPUTER PROGRAMMING LAB-I) Page 5


Jaipur Engineering College and Research Centre Department of Electronics &
Communication Engineering

Output:
Given array is
12, 11, 13, 5, 6, 7

Sorted array is
5 6 7 11 12 13
Viva Questions:
1. what are the needs of bubble, selection and insertion sort
2. complexity of bubble sort, selection and insertion sort
3. In a selection sort of n elements, how many times is the swap function called in the complete execution of
the algorithm?

(ECE/LAB MANUAL/3EC8A COMPUTER PROGRAMMING LAB-I) Page 6


Jaipur Engineering College and Research Centre Department of Electronics &
Communication Engineering

LAB-MANUAL
(II Year III SEM ECE)

Lab Name : Computer Programming Lab-I

Lab Code : 3EC8A

Branch : Electronics & Communication Engineering

Experiment Object:-
To search an element using Binary Search

Jaipur Engineering College and Research Centre


Shri Ram ki Nangal, via Sitapura RIICO
Tonk Road, Jaipur-302 022, Rajasthan

EXPERIMENT NO:-2
Object:- To search an element using Binary Search

Description:
In Binary Search we use Divide & Conquer principle.

(ECE/LAB MANUAL/3EC8A COMPUTER PROGRAMMING LAB-I) Page 7


Jaipur Engineering College and Research Centre Department of Electronics &
Communication Engineering

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:
Step 1: if (low > high) then return -1

Step 2: if (low < high) the mid=(low + high)/2

Step 3: X be a key. If a[mid] = X then return mid

Step 4: If a[mid] > X then search for X from a[low] to a[mid-1]

Step 5: If a[mid] < X then search for X from a[mid + 1] to a[high]

Source Code:

#include<stdio.h>

#include<conio.h>

void main()

int a[10],n,i,j,temp;

int beg,end,mid,target;

clrscr();

printf("Enter the total numbers:");

scanf("%d",&n);

printf("Enter the array elements:" );

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

scanf("%d",&a[i]);

beg=0;

(ECE/LAB MANUAL/3EC8A COMPUTER PROGRAMMING LAB-I) Page 8


Jaipur Engineering College and Research Centre Department of Electronics &
Communication Engineering

end=n;

mid=(beg+end)/2;

printf("\nEnter the number to be searched:");

scanf("%d",&target);

while(beg<=end && a[mid]!=target)

if(target<a[mid])

end=mid-1;

else

beg=mid+1;

mid=(beg+end)/2;

if(a[mid]==target)

printf("\nThe number is found at position %2d",mid);

else

printf("\nThe number is not found");

getch();

OutPut:
Enter the total numbers:5

Enter the array elements:1 2 3 4 5

Enter the number to be searched: 5

(ECE/LAB MANUAL/3EC8A COMPUTER PROGRAMMING LAB-I) Page 9


Jaipur Engineering College and Research Centre Department of Electronics &
Communication Engineering

The number is found at position 4

Result:Hence, BinarySearch is implemented.


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?

(ECE/LAB MANUAL/3EC8A COMPUTER PROGRAMMING LAB-I) Page 10


Jaipur Engineering College and Research Centre Department of Electronics &
Communication Engineering

LAB-MANUAL
(II Year III SEM ECE)

Lab Name : Computer Programming Lab-I

Lab Code : 3EC8A

Branch : Electronics & Communication Engineering

Experiment Object:-
Write a program to search an element using linear search

Jaipur Engineering College and Research Centre


Shri Ram ki Nangal, via Sitapura RIICO
Tonk Road, Jaipur-302 022, Rajasthan

(ECE/LAB MANUAL/3EC8A COMPUTER PROGRAMMING LAB-I) Page 11


Jaipur Engineering College and Research Centre Department of Electronics &
Communication Engineering

EXPERIMENT NO:-3
Object: Write a program to search an element using linear search
1. Set k: = 1 & loc: = 0
2. Repeat step 3 & 4 while loc: = 0 &k < = n
3. If (item = data[k])
loc: = k
Else
K=k+1
4. If loc: = 0, then
Print “no. not found” Else
Print “loc is the location of item”
5.Exit

Source Code:
#include <stdio.h>

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

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


scanf("%d",&n);

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

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


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

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


scanf("%d", &search);

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


{
if (array[c] == search) /* if required element found */
{
printf("%d is present at location %d.\n", search, c+1);
break;
}
}
if (c == n)
printf("%d is not present in array.\n", search);

(ECE/LAB MANUAL/3EC8A COMPUTER PROGRAMMING LAB-I) Page 12


Jaipur Engineering College and Research Centre Department of Electronics &
Communication Engineering

return 0;
}

Output:
Enter the no of elements: 6

Enter the array: 10 20 30 40 50 60

Enter the number to search: 30

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?

(ECE/LAB MANUAL/3EC8A COMPUTER PROGRAMMING LAB-I) Page 13


Jaipur Engineering College and Research Centre Department of Electronics &
Communication Engineering

LAB-MANUAL
(II Year III SEM ECE)

Lab Name : Computer Programming Lab-I

Lab Code : 3EC8A

Branch : Electronics & Communication Engineering

Experiment Object:-
Write a program for addition, multiplication and transpose of matrices.

Jaipur Engineering College and Research Centre


Shri Ram ki Nangal, via Sitapura RIICO
Tonk Road, Jaipur-302 022, Rajasthan

(ECE/LAB MANUAL/3EC8A COMPUTER PROGRAMMING LAB-I) Page 14


Jaipur Engineering College and Research Centre Department of Electronics &
Communication Engineering

EXPERIMENT NO:-4

Object: - Write a program for addition, multiplication and transpose of matrices.

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:
Enter the first matrix

Enter the element 234

356

111

Enter the second matrix

Enter the element 123

456

789

Matrix after multiplication

42 51 60

64 79 120

11 15 18

Algorithm

Matadd(a,b,m,n)
1 for (i=1 to m

(ECE/LAB MANUAL/3EC8A COMPUTER PROGRAMMING LAB-I) Page 15


Jaipur Engineering College and Research Centre Department of Electronics &
Communication Engineering

2 for(j= 1 to n)
3c[i][j] = a[i][j]+b[i][j]
4 exit

Output:
Enter the first matrix

Enter the element 234

356

111

Enter the second matrix

Enter the element 123

456

789

Matrix after Addition

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:
Enter the first matrix

Enter the element 234

356

111

(ECE/LAB MANUAL/3EC8A COMPUTER PROGRAMMING LAB-I) Page 16


Jaipur Engineering College and Research Centre Department of Electronics &
Communication Engineering

Matrix after Transpose

231

351

461

Viva Questions:
1. What is the condition for multiplication of two matrices?
2. Explain the matrix representation in memory?
3. What is the condition for addition of two matrices?

(ECE/LAB MANUAL/3EC8A COMPUTER PROGRAMMING LAB-I) Page 17


Jaipur Engineering College and Research Centre Department of Electronics &
Communication Engineering

LAB-MANUAL
(II Year III SEM ECE)

Lab Name : Computer Programming Lab-I

Lab Code : 3EC8A

Branch : Electronics & Communication Engineering

Experiment Object:-
To sort an array using quick sort.

Jaipur Engineering College and Research Centre


Shri Ram ki Nangal, via Sitapura RIICO
Tonk Road, Jaipur-302 022, Rajasthan

EXPERIMENT NO:-5

Object: - To sort an array using quick sort.

Description:

(ECE/LAB MANUAL/3EC8A COMPUTER PROGRAMMING LAB-I) Page 18


Jaipur Engineering College and Research Centre Department of Electronics &
Communication Engineering

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:

1) Always pick first element as pivot.


2) Always pick last element as pivot (implemented below)
3) Pick a random element as pivot.
4) Pick median as pivot.

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:
1. low =l, high = h, key a[(l+h)/2]

2. Repeat through step 7 while (low <= high)

3. Repeat step 4 while (a[low] < key)

4. low = low +1

5. Repeat step 6 while (a[high] > key)

6. high = high – 1

7. If (low <= high)

a) temp = a[low]

b) a[low] = a[high]

c) a[high] = temp d) low = low + 1

e) high = high + 1

8. If (l < high) quicksort (a,l,high)

9. If (h>low) quicksort (a,low,h)

10. Exit

This algorithm sorts an array a.

(ECE/LAB MANUAL/3EC8A COMPUTER PROGRAMMING LAB-I) Page 19


Jaipur Engineering College and Research Centre Department of Electronics &
Communication Engineering

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>

void quicksort(int [10],int,int);

int main(){
int x[20],size,i;

printf("Enter size of the array: ");


scanf("%d",&size);

printf("Enter %d elements: ",size);


for(i=0;i<size;i++)
scanf("%d",&x[i]);

quicksort(x,0,size-1);

printf("Sorted elements: ");


for(i=0;i<size;i++)
printf(" %d",x[i]);

return 0;
}

void quicksort(int x[10],int first,int last){


int pivot,j,temp,i;

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;

(ECE/LAB MANUAL/3EC8A COMPUTER PROGRAMMING LAB-I) Page 20


Jaipur Engineering College and Research Centre Department of Electronics &
Communication Engineering

}
}

temp=x[pivot];
x[pivot]=x[j];
x[j]=temp;
quicksort(x,first,j-1);
quicksort(x,j+1,last);

}
}

Output:
Enter the no of elements 5

Enter the array 15 5 3 7 12

Sorted array 3 5 7 12 15

Viva Questions:
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.

LAB-MANUAL
(II Year III SEM ECE)

(ECE/LAB MANUAL/3EC8A COMPUTER PROGRAMMING LAB-I) Page 21


Jaipur Engineering College and Research Centre Department of Electronics &
Communication Engineering

Lab Name : Computer Programming Lab-I

Lab Code : 3EC8A

Branch : Electronics & Communication Engineering

Experiment Object:-
Write a program to implement singly linked list

Jaipur Engineering College and Research Centre


Shri Ram ki Nangal, via Sitapura RIICO
Tonk Road, Jaipur-302 022, Rajasthan

EXPERIMENT NO:-6

Object: - Write a program to implement singly linked list


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

(ECE/LAB MANUAL/3EC8A COMPUTER PROGRAMMING LAB-I) Page 22


Jaipur Engineering College and Research Centre Department of Electronics &
Communication Engineering

MIDDLE

1. Enter info of the node after which new node to be inserted


2. Read x
3. p = start
4. Repeat step 5 until p Æ info < > x
5. p = p Æ next
6. t Æ next = p Æ next
7. p Æ next = t
8. Return

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
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

(ECE/LAB MANUAL/3EC8A COMPUTER PROGRAMMING LAB-I) Page 23


Jaipur Engineering College and Research Centre Department of Electronics &
Communication Engineering

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;
}
/* 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;
}

(ECE/LAB MANUAL/3EC8A COMPUTER PROGRAMMING LAB-I) Page 24


Jaipur Engineering College and Research Centre Department of Electronics &
Communication Engineering

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;
/*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;
}

(ECE/LAB MANUAL/3EC8A COMPUTER PROGRAMMING LAB-I) Page 25


Jaipur Engineering College and Research Centre Department of Electronics &
Communication Engineering

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)
{
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);
}

(ECE/LAB MANUAL/3EC8A COMPUTER PROGRAMMING LAB-I) Page 26


Jaipur Engineering College and Research Centre Department of Electronics &
Communication Engineering

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");

}
}
}
}

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:

1. What is singly linked list?

(ECE/LAB MANUAL/3EC8A COMPUTER PROGRAMMING LAB-I) Page 27


Jaipur Engineering College and Research Centre Department of Electronics &
Communication Engineering

2. What are the differences between array and linked list?

LAB-MANUAL
(II Year III SEM ECE)

Lab Name : Computer Programming Lab-I

Lab Code : 3EC8A

Branch : Electronics & Communication Engineering

(ECE/LAB MANUAL/3EC8A COMPUTER PROGRAMMING LAB-I) Page 28


Jaipur Engineering College and Research Centre Department of Electronics &
Communication Engineering

Experiment Object:-

Write a program to implement double linked list

Jaipur Engineering College and Research Centre


Shri Ram ki Nangal, via Sitapura RIICO
Tonk Road, Jaipur-302 022, Rajasthan

EXPERIMENT NO:-7

Object: - 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 pÆnext = t

(ECE/LAB MANUAL/3EC8A COMPUTER PROGRAMMING LAB-I) Page 29


Jaipur Engineering College and Research Centre Department of Electronics &
Communication Engineering

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 {
int data;
int key;

(ECE/LAB MANUAL/3EC8A COMPUTER PROGRAMMING LAB-I) Page 30


Jaipur Engineering College and Research Centre Department of Electronics &
Communication Engineering

struct node *next;


struct node *prev;
};
//this link always point to first Link
struct node *head = NULL;
//this link always point to last Link
struct node *last = NULL;
struct node *current = NULL;
//is list empty
bool isEmpty(){
return head == NULL;
}
int length(){
int length = 0;
struct node *current;

for(current = head; current != NULL; current = current->next){


length++;
}

return length;
}
//display the list in from first to last
void displayForward(){
//start from the beginning
struct node *ptr = head;

//navigate till the end of the list


printf("\n[ ");

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;

//navigate till the start of the list


printf("\n[ ");

while(ptr != NULL){

//print data
printf("(%d,%d) ",ptr->key,ptr->data);

//move to next item

(ECE/LAB MANUAL/3EC8A COMPUTER PROGRAMMING LAB-I) Page 31


Jaipur Engineering College and Research Centre Department of Electronics &
Communication Engineering

ptr = ptr ->prev;


printf(" ");
}

printf(" ]");
}

//insert link at the first location


void insertFirst(int key, int data){
//create a link
struct node *link = (struct node*) malloc(sizeof(struct node));
link->key = key;
link->data = data;

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;

//point first to new first link


head = link;
}
//insert link at the last location
void insertLast(int key, int data){
//create a link
struct node *link = (struct node*) malloc(sizeof(struct node));
link->key = key;
link->data = data;

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;

//if only one link


if(head->next == NULL){

(ECE/LAB MANUAL/3EC8A COMPUTER PROGRAMMING LAB-I) Page 32


Jaipur Engineering College and Research Centre Department of Electronics &
Communication Engineering

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;

//if only one link


if(head->next == NULL){
head = NULL;
}else {
last->prev->next = NULL;
}

last = last->prev;

//return the deleted link


return tempLink;
}
//delete a link with given key
struct node* delete(int key){
//start from the first link
struct node* current = head;
struct node* previous = NULL;

//if list is empty


if(head == NULL){
return NULL;
}
//navigate through list
while(current->key != key){
//if it is last node

if(current->next == NULL){
return NULL;
}else {
//store reference to current link
previous = current;

//move to next link


current = current->next;
}
}
//found a match, update the link
if(current == head) {
//change first to point to next link

(ECE/LAB MANUAL/3EC8A COMPUTER PROGRAMMING LAB-I) Page 33


Jaipur Engineering College and Research Centre Department of Electronics &
Communication Engineering

head = head->next;
}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;

//if list is empty


if(head == NULL){
return false;
}
//navigate through list
while(current->key != key){

//if it is last node


if(current->next == NULL){
return false;
}else {
//move to next link
current = current->next;
}
}

//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);

(ECE/LAB MANUAL/3EC8A COMPUTER PROGRAMMING LAB-I) Page 34


Jaipur Engineering College and Research Centre Department of Electronics &
Communication Engineering

insertFirst(5,40);
insertFirst(6,56);
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();

printf("\nList , after delete key(4) : ");


delete(4);
displayForward();
}
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

Viva Questions:

1. What is doubly linked list?


2. What the differences are between singly doubly linked list?

(ECE/LAB MANUAL/3EC8A COMPUTER PROGRAMMING LAB-I) Page 35


Jaipur Engineering College and Research Centre Department of Electronics &
Communication Engineering

LAB-MANUAL
(II Year III SEM ECE)

Lab Name : Computer Programming Lab-I

Lab Code : 3EC8A

Branch : Electronics & Communication Engineering

(ECE/LAB MANUAL/3EC8A COMPUTER PROGRAMMING LAB-I) Page 36


Jaipur Engineering College and Research Centre Department of Electronics &
Communication Engineering

Experiment Object:-

write a program to implement queue using array and linked list

Jaipur Engineering College and Research Centre


Shri Ram ki Nangal, via Sitapura RIICO
Tonk Road, Jaipur-302 022, Rajasthan

EXPERIMENT NO:-8

Object: - 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

(ECE/LAB MANUAL/3EC8A COMPUTER PROGRAMMING LAB-I) Page 37


Jaipur Engineering College and Research Centre Department of Electronics &
Communication Engineering

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

Viva Questions:

1. What is queue?

LAB-MANUAL
(II Year III SEM ECE)

Lab Name : Computer Programming Lab-I

Lab Code : 3EC8A

Branch : Electronics & Communication Engineering

(ECE/LAB MANUAL/3EC8A COMPUTER PROGRAMMING LAB-I) Page 38


Jaipur Engineering College and Research Centre Department of Electronics &
Communication Engineering

Experiment Object:-
Write a program to implement stack using array and linked list

Jaipur Engineering College and Research Centre


Shri Ram ki Nangal, via Sitapura RIICO
Tonk Road, Jaipur-302 022, Rajasthan

EXPERIMENT NO:-10

Object: - 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”

(ECE/LAB MANUAL/3EC8A COMPUTER PROGRAMMING LAB-I) Page 39


Jaipur Engineering College and Research Centre Department of Electronics &
Communication Engineering

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
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;

(ECE/LAB MANUAL/3EC8A COMPUTER PROGRAMMING LAB-I) Page 40


Jaipur Engineering College and Research Centre Department of Electronics &
Communication Engineering

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();
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 */

(ECE/LAB MANUAL/3EC8A COMPUTER PROGRAMMING LAB-I) Page 41


Jaipur Engineering College and Research Centre Department of Electronics &
Communication Engineering

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()
{
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()
{

(ECE/LAB MANUAL/3EC8A COMPUTER PROGRAMMING LAB-I) Page 42


Jaipur Engineering College and Research Centre Department of Electronics &
Communication Engineering

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;
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

(ECE/LAB MANUAL/3EC8A COMPUTER PROGRAMMING LAB-I) Page 43


Jaipur Engineering College and Research Centre Department of Electronics &
Communication Engineering

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?

LAB-MANUAL
(II Year III SEM ECE)

Lab Name : Computer Programming Lab-I


Lab Code : 3EC8A
Branch : Electronics & Communication Engineering

(ECE/LAB MANUAL/3EC8A COMPUTER PROGRAMMING LAB-I) Page 44


Jaipur Engineering College and Research Centre Department of Electronics &
Communication Engineering

Experiment Object:-
Write a program to merge two sorted array

Jaipur Engineering College and Research Centre


Shri Ram ki Nangal, via Sitapura RIICO
Tonk Road, Jaipur-302 022, Rajasthan

EXPERIMENT NO:-11

Object: - Write a program to merge two sorted array

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

(ECE/LAB MANUAL/3EC8A COMPUTER PROGRAMMING LAB-I) Page 45


Jaipur Engineering College and Research Centre Department of Electronics &
Communication Engineering

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>

void merge(int [], int, int [], int, int []);

int main() {
int a[100], b[100], m, n, c, sorted[200];

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


scanf("%d", &m);

printf("Input %d integers\n", m);


for (c = 0; c < m; c++) {
scanf("%d", &a[c]);
}

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


scanf("%d", &n);

printf("Input %d integers\n", n);


for (c = 0; c < n; c++) {
scanf("%d", &b[c]);
}

merge(a, m, b, n, sorted);

printf("Sorted array:\n");

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


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

return 0;
}

void merge(int a[], int m, int b[], int n, int sorted[]) {


int i, j, k;

(ECE/LAB MANUAL/3EC8A COMPUTER PROGRAMMING LAB-I) Page 46


Jaipur Engineering College and Research Centre Department of Electronics &
Communication Engineering

j = k = 0;

for (i = 0; i < m + n;) {


if (j < m && k < n) {
if (a[j] < b[k]) {
sorted[i] = a[j];
j++;
}
else {
sorted[i] = b[k];
k++;
}
i++;
}
else if (j == m) {
for (; i < m + n;) {
sorted[i] = b[k];
k++;
i++;
}
}
else {
for (; i < m + n;) {
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?

(ECE/LAB MANUAL/3EC8A COMPUTER PROGRAMMING LAB-I) Page 47

You might also like