Lab Manual
Lab Manual
Modular Programming
Function
Switch-case
Do-while
Functions:
C functions are basic building blocks in a program. All C
programs are written using functions to improve re-usability,
understandability and to keep track on them. You can learn below
concepts of C functions in this section in detail.
1. What is C function?
A large C program is divided into basic building blocks called C
function. C function contains set of instructions enclosed by { }
which performs specific operation in a C program. Actually,
Collection of these functions creates a C program.
2. Uses of C functions:
C functions are used to avoid rewriting same logic/code again
and again in a program.
3. Function Declaration:
A function is called as function( );
A function is defined as function( )
4. Syntax:
// declare the add function
int add (int, int);
Switch-case:
A switch statement allows a variable to be tested for equality
against a list of values. Each value is called a case, and the variable
being switched on is checked for each switch case.
Syntax:
The syntax for a switch statement in C programming language is
as follows:
switch(expression)
{
case constant-expression :
statement(s);
break; /* optional */
case constant-expression :
statement(s);
break; /* optional */
Do-while:
Unlike for and while loops, which test the loop condition at the
top of the loop, the do...while loop in C programming language
checks its condition at the bottom of the loop.
Syntax:
The syntax of a do...while loop in C programming language is:
do
{
statement(s);
}while( condition );
Notice that the conditional expression appears at the end of the
loop, so the statement(s) in the loop execute once before the condition
is tested.
If the condition is true, the flow of control jumps back up to do,
and the statement(s) in the loop execute again. This process repeats
until the given condition becomes false.
Lab # 01
Modular Programming
Program Code
#include<stdio.h>
#include<conio.h>
void myself( );
void table( );
void sumseries( );
void fact( );
main( )
{
int choice;
char check;
do
{
myself( );
printf("\nEnter 1 to calculate table of given no");
printf("\nEnter 2 to make series upto given no");
printf("\nEnter 3 to find factorial of given no");
scanf("%d",&choice);
switch(choice)
{
case 1:
table( );
break;
case 2:
sumseries( );
break;
case 3:
fact( );
break;
}
printf("\nDo you want to run again y/n");
check = getch( );
}
while(check=='y');
}
void fact( )
{
int i,c,fact=1;
printf("enter any digit =");
scanf("%d",&c);
for(i=1;i<=c;i++)
{
fact=fact*i;
}
printf("fact of n=%d",fact);
}
void table( )
{
int a,b,c,table;
printf("\nEnter the number=");
scanf("%d",&table);
for(c=1;c<=10;c++)
{
printf("\n%d*%d=%d",table,c,table*c);
}
}
void sumseries( )
{
int a,b,c;
printf("\nEnter the number=");
scanf("%d",&a);
for(b=1;b<=a;b++)
{
printf("\t%d",b);
}
}
void myself( )
{
printf("\n\t*******************");
printf("\n\tSOHAIB AMIR");
printf("\n\t2K15-EE-18");
printf("\n\tLAB # 01");
printf("\n\tMODULAR PROGRAMMING");
printf("\n\t MAAM SUMAYYA");
printf("\n\t*******************");
}
Lab # 01
Output
Lab # 01
Output
Program # 02
Implement Four Data Structure
Operations on Arrays
Arrays:
An array is a sequence of data item of homogeneous
value(same type).
Arrays are of two types:
1. One-dimensional arrays
2. Multi-dimensional arrays
Declaration of one-dimensional array:
Data_type array_name[array_size];
For example
Int age[5];
Here the name of array is age and the size is 5 i.e there are 5
items(elements) of array age. All the elements in the array are of
the same type (int, in this case).
Array Elements:
Size of array defines the number of elements in an array.
Each element of array can be accessed and used by user
according to the need of program. For Example:
int age[5];
Note that the first element is numbered 0 and so on. Here,
the size of array age times the size of integer because there are 5
elements.
Suppose, the starting address of age[0] is 2120d and the size
of integer be 4 bytes, then the next address(address of age[1] will
be 2124d, the address of age[2] will be 2128d and so on.
Initialization of one-dimensional array:
Arrays can be initialized at declaration time in the source
code as:
int age[5]={2,4,34,3,4};
It is not necessary to define the size of array during
initialization
int age[5]={2,4,34,3,4};
In this case, the compiler determines the size of array by
calculating the number of elements of an array.
Program # 02
Implement Four Data Structure
Operations on Arrays
Program Code
#include<stdio.h>
#include<conio.h>
void myself( );
void traversing( );
void search( );
void deletion( );
void insertion();
main( )
{
int choice;
char check;
do
{
myself( );
printf("\nEnter 1 to traverse an array");
printf("\nEnter 2 to search an element in an array");
printf("\nEnter 3 to delete element(s) from an array");
printf("\nEnter 4 to insert an element in an array");
printf("\nNumber entered by user=");
scanf("%d",&choice);
switch(choice)
{
case 1:
traversing( );
break;
case 2:
search( );
break;
case 3:
deletion( );
break;
case 4:
insertion( );
break;
}
printf("\nDo you want to run again(y/n)=");
scanf("%c",&check);
check=getch( );
}
while(check=='y');
}
void traversing( )
{
int arr[50];
int b,i,size;
printf("\nEnter size of array=");
scanf("%d",&size);
printf("\nEnter elements of array=");
for(b=0;b<size;b++)
{
scanf("\t%d",&arr[b]);
}
printf("\nThe array is=");
for(i=0;i<size;i++)
{
printf("\t%d",arr[i]);
}
getch( );
}
void search( )
{
int arr[50],i,size,n;
printf("\nEnter the size of array=");
scanf("%d",&size);
printf("\nEnter elements of array=");
for(i=0;i<size;i++)
{
scanf("%d",&arr[i]);
}
printf("\nEnter no to be found=");
scanf("%d",&n);
for(i=0;i<=4;i++)
{
if(arr[i]==n)
break;
else
continue;
}
(i<=4?printf("\nThe no is found at index=%d",i):printf("\nThe no not
found"));
getch( );
}
void deletion( )
{
int arr[50];
int b,c,i,size,index;
printf("\nEnter size of array=");
scanf("%d",&size);
printf("\nEnter elements of array\n");
for(b=0;b<size;b++)
{
scanf("\t%d",&arr[b]);
}
printf("\nEnter the deletion index=");
scanf("%d",&index);
printf("\nEnter the no of elements u want to delete=");
scanf("%d",&c);
for(i=index;i<size;i++)
{
arr[i]=arr[i+c];
}
printf("The new array is");
for(i=0;i<size-c;i++)
printf("\t%d",arr[i]);
getch( );
}
void insertion( )
{
int arr[50];
int a,i,size,index,value;
printf("\nEnter size of array=");
scanf("%d",&size);
printf("\nEnter elements of array\n");
for(a=0;a<size;a++)
{
scanf("\t%d",&arr[a]);
}
printf("\nEnter the insertion index=");
scanf("%d",&index);
printf("\nEnter the value to insert=");
scanf("%d",&value);
for(i=size;i>=index;i--)
{
arr[i+1]=arr[i];
}
arr[index]=value;
printf("\nThe new array is=");
for(i=0;i<size;i++)
{
printf("\t%d",arr[i]);
}
getch( );
}
void myself( )
{
printf("\n\t*******************");
printf("\n\tSOHAIB AMIR");
printf("\n\t2K15-EE-18");
printf("\n\tLAB # 01");
printf("\n\tMODULAR PROGRAMMING");
printf("\n\t MAAM SUMAYYA");
printf("\n\t*******************");
}
Program # 02
Implement Four Data Structure
Operations on Arrays
Output
Program # 03
Implement Four Data Structure
Operations on Structures
Structures:
A structure in the C programming language is a complex data
type declaration that defines a physically grouped list of variables of
different types to be placed under one name in a block of memory,
allowing the different variables to be accessed via a single pointer, or
the structure declared name which returns the same address.
Syntax of structure:
Keyword struct is used to create a structure.
struct structure_name
{
data_type member1;
data_type member2;
.
.
data_type memeber;
};
We can create the structure for a student as mentioned above as:
struct student
{
char name[50];
int roll_no;
float percentage;
};
This declaration above creates the derived data type structure
student
Structure variable declaration:
When a structure is defined, it creates a user-defined type but,
no storage is allocated. For the above structure of student, variable
can be declared as:
struct student
{
char name[50];
int cit_no;
float percentage;
};
Inside main function:
struct student s1, s2, s[20];
Another way of creating sturcture variable is:
struct student
{
char name[50];
int roll_no;
float percentage;
}s1 ,s2 ,s[20];
In both cases, 2 variables s1, s2 and array s having 20 elements of
type structure student are created.
Accessing members of a structure:
There following operator is used for accessing members of a
structure.
s2.percentage
Program # 03
Implement Four Data Structure
Operations on Structures
Program Code
#include<stdio.h>
#include<conio.h>
void myself();
void table();
void deletion();
void insertion();
void search();
int i;
struct record
{
int srno;
int rollno;
char name[5];
char gender;
int tmarks;
int obtmarks;
};
struct record r[5];
void main()
{
int choice;
char check;
clrscr();
printf("\nEnter roll no & obt marks alternatively");
for(i=1;i<6;i++)
{
r[i].srno=i;
scanf("%d",&r[i].rollno);
r[i].tmarks=60;
scanf("%d",&r[i].obtmarks);
}
strcpy(r[1].name,"AHMAD");
strcpy(r[2].name,"SAEED");
strcpy(r[3].name,"AMJAD");
strcpy(r[4].name,"SAQIB");
strcpy(r[5].name,"M.ALI");
table();
do
{
clrscr();
myself();
printf("\nEnter 1 to see the record");
printf("\nEnter 2 to search data");
printf("\nEnter 3 to perform deletion");
printf("\nEnter 4 to perform insertion");
printf("\nNo entered by user=");
scanf("%d",&choice);
switch(choice)
{
case 1:
table();
break;
case 2:
search();
break;
case 3:
deletion();
break;
case 4:
insertion();
break;
}
printf("\n\nDo you want to run again(y/n)=");
scanf("%c",&check);
check=getche();
getch();
}
while(check=='y');
}
void table()
{
printf("\n\nSerial no Roll no\tName\tGender\tT.Marks\t
Obt.Marks");
for(i=1;i<6;i++)
{
printf("\n %d\t\t%d\t%s\t M\t %d\t
%d",r[i].srno,r[i].rollno,r[i].name,r[i].tmarks,r[i].obtmarks);
}
}
void deletion()
{
int n;
printf("\nRecord before deletion");
table();
printf("\nEnter deletion serial no=");
scanf("%d",&n);
r[n].srno=0;
r[n].rollno=0;
strcpy(r[n].name,0);
strcpy(r[n].gender,0);
r[n].tmarks=0;
r[n].obtmarks=0;
clrscr();
printf("\nRecord after deletion");
table();
}
void insertion()
{
int a;
printf("\nRecord before insertion");
table();
printf("\nEnter insertion serial no=");
scanf("%d",&a);
clrscr();
printf("\nEnter roll no=");
scanf("%d",&r[a].rollno);
printf("\nEnter name=");
scanf("%s",&r[a].name);
printf("\nEnter obtained marks=");
scanf("%d",&r[a].obtmarks);
printf("\n\nRecord after insertion");
table();
}
void search()
{
int x;
printf("\nEnter the serial no=");
scanf("%d",&x);
for(i=1;i<6;i++)
{
if(x==i)
{
printf("\nData of entered serial no");
printf("\n\nSerial no Roll no\tName\tGender\tT.Marks\t
Obt.Marks");
printf("\n %d\t\t%d\t%s\t M\t %d\t
%d",r[i].srno,r[i].rollno,r[i].name,r[i].tmarks,r[i].obtmarks);
break;
}
else
continue;
}
}
void myself()
{
printf("\n\t\t*******************************");
printf("\n\t\tNAME\t:\tOMAIR ASHFAQ");
printf("\n\t\tROLL #\t:\t 2K13-EE-26");
printf("\n\t\tLAB #\t:\t 03");
printf("\n\t\tPROGRAM : IMPLEMENT FOUR DATA");
printf("\n\t\t STRUCTURE OPERATION");
printf("\n\t\t ON STRUCTURES");
printf("\n\t\t*******************************");
}
Program # 03
Implement Four Data Structure
Operations on Structures
Output
Program # 03
Implement Four Data Structure
Operations on Structures
Output
Program # 03
Implement Four Data Structure
Operations on Structures
Output
Program # 03
Implement Four Data Structure
Operations on Structures
Output
Program # 04
Implement Bubble Sort
Bubble Sort:
A sorting technique that is typically used for sequencing small
lists.
Types of bubble sort:
There are two types of bubble sort
1. Ascending order
2. Descending order
Execution:
It starts by comparing the first item to the second, the second to
the third and so on until it finds one item out of order. It then swaps
the two items and starts over. The process continues until no swaps
are necessary. A bubble sort always brings the highest value to the
right (if ascending) during the first pass through the list, the second
highest value to the second right (if ascending) most position during
the second pass, and so forth for each successive pass.
After each the number of comparison decreases by one.
Program # 04
Implement Bubble Sort
Program Code
#include<stdio.h>
#include<conio.h>
myinfo();
void intsorting( );
void charsorting( );
int arr[10],ch,s=0,i,b,a,t;
char again,ar[10];
int main( )
{
clrscr( );
myinfo( );
do
{
printf("\nEnter\n1 int sort\n2 char sort \n ");
scanf("%d",&ch);
if(ch==1)
intsorting( );
else
charsorting( );
printf("\n do u want to run again y/n \n");
again=getch( );
}
while(again=='y');
}
void intsorting( )
{
printf(" Enter integer araay of 10 elements\n");
for(i=0;i<10;i++)
{
scanf("%d",&arr[i]);
}
for(a=0;a<9;a++)
{
for (b=0;b<9-a;b++)
{
if(arr[b]>=arr[b];
{
t=arr[b];
arr[b]=arr[b+1];
arr[b+1]=t;
s=s+1;
}
}
if(s==0)
break;
}
printf(" \n Array after sorting \n");
for(i=0;i<10;i++)
{
printf("%d\t",arr[i]);
}
}
void char_sort()
{
printf(" Enter caracter array of 10 character\n");
for(i=0;i<10;i++)
{
scanf("%s",&ar[i]);
}
for(a=0;a<9;a++)
{
for(b=0;b<9-a;b++)
{
if(ar[b]>=ar[b+1])
{
t=ar[b];
ar[b]=ar[b+1];
ar[b+1]=t;
s=s+1;
}
}
if(s==0)
break;
}
printf(" \n Array after sorting \n");
for(i=0;i<10;i++)
{
printf("%c\t",ar[i]);
}
}
void myinfo( )
{
printf("\n\t\t*******************************");
printf("\n\t\tNAME\t:\tOMAIR ASHFAQ");
printf("\n\t\tROLL #\t:\t 2K13-EE-26");
printf("\n\t\tLAB #\t:\t 04");
printf("\n\t\tPROGRAM : IMPLEMENT BUBBLE SORT");
}
Program # 04
Implement Bubble Sort
Output
Program # 05
Implement Binary Search
Binary Search:
Binary search comparing element to be found with the element
in the middle of the list, this tell which half of the list contains the
element.
Complexity:
The complexity of binary search is
C(n)=log2n
where n is the total number of elements.
Although binary search algorithm is a very efficient algorithm, it
has some major drawbacks. Specially, the algorithm assumes that one
has direct access to the middle element in the list or a sublist. This
means the list must be stored in some type of array. Moreover it can
be applied on linked list.
Program # 05
Implement Binary Search
Program Code
#include<stdio.h>
#include<conio.h>
void bs( );
void sort( );
void myself( );
int arr[100];
int a,b,i,j,k,n,beg,end,mid;
void main( )
{
clrscr( );
myself( );
printf("\nEnter the size of array=");
scanf("%d",&n);
printf("\nEnter the elements of array");
for(i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}
printf("\nPress enter to sort");
getch( );
printf("\nThe sorted array is\n");
sort( );
for(i=0;i<n;i++)
{
printf("\t%d",arr[i]);
}
bs( );
getch( );
}
void sort( )
{
for(j=n;j>0;j--)
{
for(i=0;i<n-1;i++)
{
if(arr[i]>=arr[i+1])
{
b=arr[i];
arr[i]=arr[i+1];
arr[i+1]=b;
}
else
continue;
}
}
}
void bs( )
{
printf("\nEnter element to be found=");
scanf("%d",&a);
beg=0;
end=n-1;
while((arr[mid]!=a)&&(beg<=end))
{
mid=(beg+end)/2;
if(arr[mid]>a)
end=mid-1;
else
beg=mid+1;
}
if(a==arr[mid])
printf("\nElement found at index=%d",mid);
else
printf("\nElement not found");
getch( );
}
void myself( )
{
printf("\n\t\t********************************");
printf("\n\t\tNAME\t:\tOMAIR ASHFAQ");
printf("\n\t\tROLL #\t:\t 2K13-EE-26");
printf("\n\t\tLAB #\t:\t 05");
printf("\n\t\tPROGRAM :IMPLEMENT BINARY SEARCH");
printf("\n\t\t********************************");
}
Program # 05
Implement Binary Search
Output
Program # 05
Implement Binary Search
Output
Program # 06
Implementation of Matrix Operations
Multi-Dimensional Arrays:
C programming language allows multidimensional arrays. Here
is the general form of a multidimensional array declaration:
type name[size1][size2]...[sizeN];
For example, the following declaration creates a three dimensional
integer array:
int three dim[5][10][4];
Two-Dimensional Arrays:
The simplest form of the multidimensional array is the two-
dimensional array. A two-dimensional array is, in essence, a list of
one-dimensional arrays. To declare a two-dimensional integer array of
size x, y you would write something as follows:
int a[3][4] = {
{0, 1, 2, 3} , /* initializers for row indexed by 0 */
{4, 5, 6, 7} , /* initializers for row indexed by 1 */
{8, 9, 10, 11} /* initializers for row indexed by 2 */
};
The nested braces, which indicate the intended row, are optional.
The following initialization is equivalent to previous example:
#include<stdio.h>
#include<conio.h>
void myself( );
void view( );
void add( );
void sub ();
void scalarmul( );
void matrixmul( );
void trans( );
void deter( );
int arr[2][2];
int arr1[2][2]={11,23,13,20};
int arr2[2][2]={10,16,13,19};
int i,j,n,m,choice,detA,detB;
char check;
main( )
{
do
{
clrscr( );
myself( );
printf("\nEnter 1 to view matrices");
printf("\nEnter 2 to perform addition");
printf("\nEnter 3 to perform subtraction");
printf("\nEnter 4 to perform scalar multiplcation");
printf("\nEnter 5 to perform matrix multiplcation");
printf("\nEnter 6 to find transpose of matrices");
printf("\nEnter 7 t0 find determinant of matrices");
printf("\nNumber entered by user=");
scanf("%d",&choice);
switch(choice)
{
case 1:
view( );
break;
case 2:
add( );
break;
case 3:
sub( );
break;
case 4:
scalarmul( );
break;
case 5:
matrixmul( );
break;
case 6:
trans();
break;
case 7:
deter( );
break;
}
printf("Do you want to run again(y/n)=");
scanf("%c",&check);
check=getche( );
getch( );
}
while(check=='y');
}
void view( )
{
printf("\n Matrix A\t\t Matrix B\n");
for(j=0;j<2;j++)
{
for(i=0;i<2;i++)
{
printf(" %d",arr1[i][j]);
}
printf("\t\t");
for(i=0;i<2;i++)
{
printf(" %d",arr2[i][j]);
}
printf("\n");
}
getch( );
}
void add( )
{
view( );
for(j=0;j<2;j++)
{
for(i=0;i<2;i++)
{
arr[i][j]=arr1[i][j]+arr2[i][j];
}
}
printf("\nResultant matrix\n");
for(j=0;j<2;j++)
{
for(i=0;i<2;i++)
{
printf(" %d",arr[i][j]);
}
printf("\n");
}
getch( );
}
void sub( )
{
view( );
for(j=0;j<2;j++)
{
for(i=0;i<2;i++)
{
arr[i][j]=arr1[i][j]-arr2[i][j];
}
}
printf("\nSubtraction of matrix 2 from 1");
printf("\nResultant matrix\n");
for(j=0;j<2;j++)
{
for(i=0;i<2;i++)
{
printf(" %d",arr[i][j]);
}
printf("\n");
}
getch( );
}
void scalarmul( )
{
printf("\nEnter 1 to use matrix A");
printf("\nEnter 2 to use matrix B");
printf("\nNumber entered by user=");
scanf("%d",&m);
printf("\nEnter number to multiply=");
scanf("%d",&n);
if(m==1)
{
clrscr( );
myself( );
printf("\nMatrix A\n");
for(j=0;j<2;j++)
{
for(i=0;i<2;i++)
{
printf(" %d",arr1[i][j]);
}
printf("\n");
}
for(j=0;j<2;j++)
{
for(i=0;i<2;i++)
{
arr[i][j]=arr1[i][j]*n;
}
}
printf("\nResultant matrix\n");
for(j=0;j<2;j++)
{
for(i=0;i<2;i++)
{
printf(" %d",arr[i][j]);
}
printf("\n");
}
}
else
{
clrscr( );
myself( );
printf("\nMatrix B\n");
for(j=0;j<2;j++)
{
for(i=0;i<2;i++)
{
printf(" %d",arr2[i][j]);
}
printf("\n");
}
for(j=0;j<2;j++)
{
for(i=0;i<2;i++)
{
arr[i][j]=arr2[i][j]*n;
}
}
printf("\nResultant matrix\n");
for(j=0;j<2;j++)
{
for(i=0;i<2;i++)
{
printf(" %d",arr[i][j]);
}
printf("\n");
}
}
getch( );
}
void matrixmul( )
{
view( );
for(j=0;j<2;j++)
{
for(i=0;i<2;i++)
{
arr[i][j]=arr1[i][j]*arr2[i][j];
}
}
printf("\nResultant matrix\n");
for(j=0;j<2;j++)
{
for(i=0;i<2;i++)
{
printf(" %d",arr[i][j]);
}
printf("\n");
}
getch( );
}
void trans( )
{
view( );
printf("\nTranspose of matrices\n");
for(j=0;j<2;j++)
{
for(i=0;i<2;i++)
{
printf(" %d",arr1[j][i]);
}
printf("\t\t");
for(i=0;i<2;i++)
{
printf(" %d",arr2[j][i]);
}
printf("\n");
}
getch( );
}
void deter( )
{
view( );
detA=arr1[0][0]*arr1[1][1]-arr1[1][0]*arr1[0][1];
detB=arr2[0][0]*arr2[1][1]-arr2[1][0]*arr2[0][1];
printf("\nDeterminant of matrix A=%d\n",detA);
printf("\nDeterminant of matrix B=%d\n",detB);
getch();
}
void myself(
)
{
printf("\n\t\t****************************");
printf("\n\t\tNAME\t: OMAIR ASHFAQ");
printf("\n\t\tROLL #\t: 2K13-EE-26");
printf("\n\t\tLAB #\t:\t 06");
printf("\n\t\tPROGRAM : IMPLEMENTATION OF");
printf("\n\t\t MATRIX OPERATIONS");
printf("\n\t\t****************************");
}
Program # 06
Implementation of Matrix Operations
Output
Program # 06
Implementation of Matrix Operations
Output
Program # 06
Implementation of Matrix Operations
Output
Program # 06
Implementation of Matrix Operations
Output
Program # 06
Implementation of Matrix Operations
Output
Program # 06
Implementation of Matrix Operations
Output
Program # 06
Implementation of Matrix Operations
Output
Program # 06
Implementation of Matrix Operations
Output
Program # 07
Implement Four Data Structure
Operations on Linked List
Linked List:
One disadvantage of using arrays to store data is that arrays are
static structures and therefore cannot be easily extended or reduced to
fit the data set. Arrays are also expensive to maintain new insertions
and deletions. In this chapter we consider another data structure called
Linked Lists that addresses some of the limitations of arrays.
int link[10]={4,3,0,5,6,-1,1},start=2,repeat=0,i,node,ptr,ind=7;
char info[10]={'m','p','l','q','n','r','o'},item;
main( )
{
int choice;
char check;
do
{
clrscr( );
myself( );
printf("\nEnter 1 to traverse");
printf("\nEnter 2 to search");
printf("\nEnter 3 to perform deletion");
printf("\nEnter 4 to perform insertion");
printf("\nNumber entered by user=");
scanf("%d",&choice);
switch(choice)
{
case 1:
traverse( );
break;
case 2:
search( );
break;
case 3:
delet( );
break;
case 4:
insert( );
break;
}
printf("\n\nDo u want to run again(y/n)=");
check=getche( );
getch( );
}
while (check=='y');
}
void traverse( )
{
node=start;
printf("\n");
while(node!=-1)
{
printf("%c\t", info[node]);
node=link[node];
}
}
void search( )
{
printf("\nEnter the item to be searched=");
item=getche( );
start=2;
node=start;
while(node!=-1)
{
if(item==info[node])
{
printf("\n\nItem found at index=%d",node);
repeat++;
break;
}
node=link[node];
}
(repeat==0?printf("\nItem not found"):printf(" "));
}
void delet( )
{
printf("\nList before deletion");
traverse( );
printf("\n\nEnter item to be deleted=");
item=getche( );
start=2;
if(info[start]==item)
{
start=link[start];
printf("\nList after deletion");
traverse( );
}
else
{
ptr=link[start];
while(start!=-1)
{
if(info[ptr]==item)
{
link[start]=link[ptr];
break;
}
start=ptr;
ptr=link[start];
}
printf("\nList after deletion");
traverse( );
}
}
void insert( )
{
printf("\nList before insertion");
traverse( );
printf("\nEnter item to be inserted=");
item=getche( );
start=2;
if(info[start]==item)
{
info[ind]=item;
link[ind]=start;
start=ind;
printf("\nList after insertion\n");
traverse( );
}
else
{
ptr=link[start];
while(start!=-1)
{
if(item<info[ptr])
{
info[ind]=item;
link[ind]=link[start];
link[start]=ind;
break;
}
start=ptr;
ptr=link[start];
}
printf("\nList after insertion\n");
start=2;
traverse( );
}
ind=ind+1;
}
void myself( )
{
printf("\n\t\t*******************************");
printf("\n\t\tNAME\t:\tOMAIR ASHFAQ");
printf("\n\t\tROL #\t:\t 2K13-EE-26");
printf("\n\t\tLAB #\t:\t 07");
printf("\n\t\tPROGRAM : IMPLEMENT FOUR DATA");
printf("\n\t\t\t STRUCTURE OPERATION");
printf("\n\t\t\t ON LINKED LIST");
printf("\n\t\t*******************************");
}
Program # 07
Implement Four Data Structure
Operations on Linked List
Output
Program # 07
Implement Four Data Structure
Operations on Linked List
Output
Program # 07
Implement Four Data Structure
Operations on Linked List
Output
Program # 07
Implement Four Data Structure
Operations on Linked List
Output
Program # 08
Implement Data Structure
Operations on Stacks
Stacks:
A stack is a container of objects that are inserted and removed
according to the last-in first-out (LIFO) principle. In the pushdown
stacks only two operations are allowed: push the item into the stack,
and pop the item out of the stack. A stack is a limited access data
structure - elements can be added and removed from the stack only at
the top. push adds an item to the top of the stack, pop removes the
item from the top. A helpful analogy is to think of a stack of books;
you can remove only the top book, also you can add a new book on
the top.
Applications:
#include<stdio.h>
#include<conio.h>
#define Max 5
void myself( );
int stack[Max];
void push( );
int pop( );
void traverse( );
int is_empty( );
int top_element( );
int top=-1;
main( )
{
int element,choice;
char check;
do
{
clrscr( );
myself( );
printf("\nEnter 1 to push element");
printf("\nEnter 2 to pop element");
printf("\nEnter 3 to print top element");
printf("\nEnter 4 to check stack is empty");
printf("\nEnter 5 to traverse stack");
printf("\nNumber entered by user=");
scanf("%d",&choice);
switch(choice)
{
case 1:
push(element);
break;
case 2:
pop( );
break;
case 3:
top_element( );
break;
case 4:
is_empty( );
break;
case 5:
traverse( );
break;
}
printf("\nDo you want to run again(y/n)=");
check=getche( );
getch( );
}
while(check=='y');
}
void push(int value)
{
if(top==Max-1)
printf("\nError : Overflow");
else
{
printf("\nEnter the value to push=");
scanf("%d",&element);
}
top++;
stack[top]=value;
traverse( );
}
int pop( )
{
if(top==-1)
printf("\nError : Underflow");
else
{
element=pop( );
printf("\nElement poped from stack=%d",element);
}
traverse( );
int element;
if(top==-1)
return top;
element=stack[top];
top--;
return element;
}
void traverse( )
{
int d;
if(top==-1)
{
printf("\nStack is empty");
return;
}
printf("\nThere are %d elements in stack",top+1);
for(d=top;d>=0;d--)
printf("\n%d",stack[d]);
}
int is_empty( )
{
if(is_empty( ))
printf("\nThe stack is empty");
else
printf("\nThe satck is not empty");
if(top==-1)
return 1;
else
return 0;
}
int top_element( )
{
if(!is_empty( ))
{
element=top_element( );
printf("\nElement at the top of stack=%d",element);
}
else
printf("\nStack is empty");
return stack[top];
}
void myself( )
{
printf("\n\t\t**********************************");
printf("\n\t\tNAME\t: OMAIR ASHFAQ");
printf("\n\t\tROLL #\t: 2K13-EE-26");
printf("\n\t\tLAB #\t:\t 08");
printf("\n\t\tPROGRAM : IMPLEMENT DATA STRUCTURE");
printf("\n\t\t OPERATIONS ON STACKS");
printf("\n\t\t**********************************");
}
Program # 08
Implement Data Structure
Operations on Stacks
Output
Program # 08
Implement Data Structure
Operations on Stacks
Output
Program # 08
Implement Data Structure
Operations on Stacks
Output
Program # 08
Implement Data Structure
Operations on Stacks
Output
Program # 08
Implement Data Structure
Operations on Stacks
Output
Program # 09
Implement Data Structure
Operations on Queues
Queue:
Queue is an abstract data type in which the first element is
inserted from one end called Rear (also called tail), and the deletion
of existing element takes place from the other end called as Front
(also called head). This makes queue as FIFO data structure, which
means element inserted first will be removed first.
The process to add an element is called Enqueue and the
process of removal of an element is called Dequeue.
Rear Front
#include<stdio.h>
#include<conio.h>
int Q[5];
int element,i;
void myself( );
void insert( );
void del( );
void traverse( );
main( )
{
int choice;
char check;
do
{
clrscr( );
myself( );
printf("\nEnter 1 to insert element");
printf("\nEnter 2 to delete element");
printf("\nEnter 3 to traverse queue");
printf("\nNumber entered by user=");
scanf("%d",&choice);
switch(choice)
{
case 1:
insert( );
break;
case 2:
del( );
break;
case 3:
traverse( );
break;
}
printf("\n\nDo you want run again(y/n)=");
check=getche( );
getch( );
}
while(check=='y');
}
void insert( )
{
if(Q[4]!=0)
printf("\nError : Overflow");
else
{
printf("\nEnter the element to insert=");
scanf("%d",&element);
for(i=3;i>=0;i--)
{
Q[i+1]=Q[i];
}
Q[0]=element;
traverse( );
}
}
void del( )
{
if(Q[4]==0)
printf("\nError : Underflow");
else
{
printf("\nElement deleted from queue=%d",Q[4]);
for(i=4;i>=0;i--)
{
Q[i+1]=Q[i];
}
Q[0]=0;
traverse( );
}
}
void traverse( )
{
printf("\nThe queue is");
for(i=0;i<5;i++)
{
printf("\t%d",Q[i]);
}
}
void myself( )
{
printf("\n\t\t**********************************");
printf("\n\t\tNAME\t: OMAIR ASHFAQ");
printf("\n\t\tROLL #\t: 2K13-EE-26");
printf("\n\t\tLAB #\t:\t 09");
printf("\n\t\tPROGRAM : IMPLEMENT DATA STRUCTURE");
printf("\n\t\t OPERATIONS ON QUEUES");
printf("\n\t\t**********************************");
}
Program # 09
Implement Data Structure
Operations on Queues
Output
Program # 09
Implement Data Structure
Operations on Queues
Output
Program # 09
Implement Data Structure
Operations on Queues
Output
Program # 10
Implement Quick Sort
Quick Sort:
Quicksort, or partition-exchange sort, is a sorting
algorithm developed by Tony Hoare that, on average, makes n log n
comparisons to sort n items. In the worst case, it makes n2
comparisons, though this behavior is rare. Quicksort is often faster in
practice than other n log n algorithms. Additionally, quicksort's
sequential and localized memory references work well with a cache.
Quicksort is a comparison sort and, in efficient implementations, is
not a stable sort. Quicksort can be implemented with an in place
partitioning algorithm, so the entire sort can be done with only log n
additional space used by the stack during the recursion.
Algorithm:
Quicksort is a divide and conquer algorithm. Quicksort first
divides a large array into two smaller sub-arrays: the low elements
and the high elements. Quicksort can then recursively sort the sub-
arrays.
The steps are:
2. Reorder the array so that all elements with values less than the
pivot come before the pivot, while all elements with values
greater than the pivot come after it (equal values can go either
way). After this partitioning, the pivot is in its final position.
This is called the partition operation.
3. Recursively apply the above steps to the sub-array of elements
with smaller values and separately to the sub-array of elements
with greater values.
Program # 10
Implement Quick Sort
Program Code
#include<stdio.h>
#include<conio.h>
int size,i=1;
void display(int [ ]);
void quicksort(int [ ],int,int);
void myself();
void main( )
{
int a[20],i;
clrscr( );
printf("\nEnter the size of list=");
scanf("%d",&size);
printf("\nEnter the elements in list=");
for(i=0;i<size;i++)
scanf("%d",&a[i]);
clrscr( );
myself( );
printf("\n\nThe unsorted list\n");
display(a);
printf("\n\nPress enter to perform the operation");
getch( );
quicksort(a,0,size-1);
printf("\n\nThe sorted list\n");
display(a);
getch( );
}
void display(int a[ ])
{
int i;
printf("\n");
for(i=0;i<size;i++)
printf("\t%d",a[i]);
}
int partition(int a[ ],int lb,int ub)
{
int up,down;
int temp,pivot;
up=ub;
down=lb+1;
pivot=a[lb];
do
{
while((a[down]<pivot) && (down<=ub))
down++;
while((a[up]>pivot) && (up>lb))
up--;
if(down<up)
{
temp=a[down];
a[down]=a[up];
a[up]=temp;
}
}
while(down<up);
a[lb]=a[up];
a[up]=pivot;
return up;
}
void quicksort(int a[],int lb,int ub)
{
int j;
if(lb<ub)
{
i++;
j=partition(a,lb,ub);
quicksort(a,lb,j-1);
quicksort(a,j+1,ub);
}
}
void myself( )
{
printf("\n\t*******************");
printf("\n\tSOHAIB AMIR");
printf("\n\t2K15-EE-18");
printf("\n\tLAB # 01");
printf("\n\tMODULAR PROGRAMMING");
printf("\n\t MA'AM SUMAYYA");
printf("\n\t*******************");
}
Program # 10
Implement Quick Sort
Output