Вы находитесь на странице: 1из 38

G.

H Raisoni College of Engineering, Nagpur


(An Autonomous Institute Under Act 1956)

Department of First Year

Lab Manual

Subject
Data Structure
Session:-2018-2019
Semester:-I
G. H. Raisoni College of Engineering, Nagpur
(An Autonomous Institute under UGC Act 1956)
Department of First Year
Data Structures Practical List
Subject Name: Data Structure
Year/Semester: I/I
Sr. No. Name of Experiment CO
1 Introduction to Project & Requirement Gathering. CO1
2 Project Table Identifications: Table Names, Table Attributes, CO1
Primary Keys, Foreign Keys, Integrity Constraints, Data Types,
Referencing
3 Data Entry: Designing of database Schema, Data Entry on to CO1
Tables, Alteration of table structure of alteration of scheme based
on requirement. Data validation against use cases and test cases
4 Implementation of Array CO2
5 Implementation of Singly Linked List for insertion, deletion CO3
operation.
6 Implementation of Singly Linked List for Searching, sorting CO3
operation.
7 Implementation of Stack on the required data storage. CO4
8 Implementation of Queue on the required data storage. CO4
9 Implementation of Efficient Sorting techniques to sort data. CO4
10 Implementation of Efficient Searching techniques to search on run CO4
time requirement data.

Faculty: Dr. U. N. Shrawankar


Dr. Sugandha Singh HOD, IT
Prof. P. S. Ghatode
Prof. G. Khekare
Prof. Anup Date
Prof. S. Wazalwar
PRACTICAL No : 1
Project Requirement and Information Gathering
Aim: To Study Project Requirement and Information Gathering .
Theory:
FOOD ORDER SYSTEM designed primarily for use in the food delivery. This system will
allow hotels and restaurants to increase scope of business by reducing the labor cost
involved. The system also allows to quickly and easily manage an online menu which
customers can browse and use to place orders with just few clicks. Restaurant employees
then use these orders through an easy to navigate graphical interface for efficient
processing.
Introduction
It is known globally that, in today‟s market, it is extremely difficult to start a new
small-scale business and live-through the competition from the well-established and settled
owners. In fast paced time of today, when everyone is squeezed for time, the majority of
people are finicky when it comes to placing a food order. The customers of today are not
only attracted because placing an order online is very convenient but also because they
have visibility into the items offered, price and extremely simplified navigation for the
order.
System presents an interactive and up-to-date menu with all available options in an easy to
use manner. Customer can choose one or more items to place an order which will land in
the Cart. Customer can view all the order details in the cart before checking out. At the end,
customer gets order confirmation details. Once the order is placed it is entered in the
database and retrieved in pretty much real time. This allows Restaurant Employees to
quickly go through the orders as they are received and process all orders efficiently and
effectively with minimal delays and confusion.
“Online Food Order System, originally is designed for small scale business like College
Cafeterias, Fast Food restaurant or Take-Out,but this system is just as applicable in any
food delivery industry.
The main advantage of my system is that it greatly simplifies the ordering process for both
the customer and the restaurant and also greatly lightens the load on the restaurant‟s end, as
the entire process of taking orders is automated.

Anticipated Benefits are:


1. This will minimize the number of employees at the back of the counter.
2. The system will help to reduce labor cost involved.
3. The system will be less probable to make mistake, since it‟s a machine.
4. This will avoid long queues at the counter due to the speed of execution and number of
optimum screens to accommodate the maximum output
Product Perspective:
The Online Food Order System application is a -based system. It can be accessed using IE
10.0 and above, Fire Fox 31 and above and Google Chrome.
System Model =>

The structure of the system can be divided into 3 main logical components:
 Ordering System- provides the functionality for customers to place their order and
supply necessary details.
 Order Retrieval System-This is a final logical component. Allows restaurant to keep
track of all orders placed. This component takes care of order retrieving and displaying
order information.

Ordering System Module


This module provides the functionality for customers to place their order and supply
necessary details. Users of the system, namely restaurant customers, must be provided the
following functionality:
 Create an account.
 Manage their account.

 Log in to the system.


 Navigate the restaurant‟s menu.
 Select an item from the menu.
 Add an item to their current order.
 Review their current order.
 Remove an item/remove all items from their current order.
 Provide payment details.
 Place an order.
 Receive confirmation in the form of an order number.
 View order placed.

Order Retrieval System Module


This is the most simplest module out of all 3 modules. It is designed to be used only
by restaurant employees, and provides the following functions:
 Retrieve new orders from the database.
 Display the orders in an easily readable, graphical way
Implementation
Hardware/Software Interface:
This section lists the minimum hardware and software requirements needed to run the
system efficiently.
Hardware Interface:

 Pentium Processor
 60 MB of free hard-drive space
 128 MB of RAM

Software Interface:

 Operating System: Windows (XP/Vista/7 or above)


 Progamming Languages: C

Activity Diagram :

This section lists the activity diagram and describes the flow of the activities in the system.
A detailed description is then given after the figure for each activity. Figure # 3 provides
the overview of the activity of the Online Food Order System application.
All users of the system, are provided with below menu options:

Home, Menu, My Cart, User Account, AboutUs and Contact

Ordering System Module

Customers of the Ordering system will interact with the application through an easy to use
top
navigation menu.

 “Home” menu option: allows the users to see all food items offered with nice images as
well as
select an item to place an order.

 “Menu”menu option: a „Drop-Down‟ menu, allows users to see all food items per
category. Item
can then be added to the cart using a single button click.

 “My Cart (x)”menu option:


- Allows users to see details of the items placed in cart. Details include Item #, Product
Name,
Product Image, Product Description, Quantity, Unit Price, Total per item and final total
of the order. It also allows „Update‟ and „Delete‟ an item using single button click. User
can then use a „Proceed to checkout‟ button to proceed further.

- Once, Check Out button is selected, user will be prompted for the Sign In/Sign Up
process if not logged in else user will be presented with a simple “Payment
Information” form. User will be asked to provide all required details in displayed text
boxes and make appropriate Dropdown selections. Then, all this information can be
saved using a „Save‟ button.

- User will then be presented with a “Review Order” page, which will display Payment
Information along with Order details to review. User can then use a „Check Out‟ button
toplace an order.

- Once order is placed, user will be presented with appropriate Order confirmation
success/failure message.

 “MyAccount”: a “Drop Down” menu will display the user orders, Sign In and Sign Out
option.

Order Retrieval System Module

The application will automatically fetch new orders from the database at regular intervals
and display the order numbers.

 Under “MyAcoount‟ menu a customer will be able to see only his/her order whereas a
Restaurant Employee or an Admin can see all users orders.
 To view the details of an order, the user must click on that order number, which will
display all order details This structure can intuitively be expanded and collapsed to
display only the desired information.
PRACTICAL No : 2 and 3
Aim- Project Table Identifications: Table Names, Table Attributes, Primary Keys,
Foreign Keys, Integrity Constraints, Data Types, Referencing
Commands:
create table course (
cnum varchar(5) not null, cname varchar(40) not null, primary key (cnum) )

insert into course values ('CS448', 'Introduction to Databases')

create table professor (


pnum integer not null, pname varchar(20) not null, office varchar(10) not null, dept
varchar(30) not null, primary key (pnum) )

insert into professor values (1, 'Weddell, Grant', 'DC3346', 'Computer Science')
insert into professor values (2, 'Ilyas, Ihab', 'DC3348', 'Computer Science')

create table student ( snum integer not null, sname varchar(20) not null, year integer not
null, primary key (snum) )

insert into student values (1, 'Jones, Fred', 4)

create table class ( cnum varchar(5) not null, term varchar(5) not null, section integer not
null, pnum integer not null, primary key (cnum, term, section), foreign key (cnum)
references course (cnum), foreign key (pnum) references professor (pnum) )

insert into class values ('CS448', 'S2006', 1, 1)

insert into class values ('CS448', 'S2006', 2, 1)

create table enrollment ( snum integer not null, cnum varchar(5) not null, term varchar(5)
not null, section integer not null, primary key (snum, cnum, term, section), foreign key
(snum) references student (snum), foreign key (cnum, term, section) references class
(cnum, term, section) )

insert into enrollment values (1, 'CS448', 'S2006', 2)

create table mark ( snum integer not null, cnum varchar(5) not null, term varchar(5) not
null, section integer not null, grade integer not null, primary key (snum, cnum, term,
section), foreign key (snum, cnum, term, section) references enrollment (snum, cnum,
term, section) )
create table schedule ( cnum varchar(5) not null, term varchar(5) not null, section integer
not null, day varchar(10) not null, time varchar(5) not null, room varchar(10) not null,
primary key (cnum, term, section, day, time), foreign key (cnum, term, section) references
class (cnum, term, section) )

insert into schedule values ('CS448', 'S2006', 1, 'Monday', '09:30', 'MC4063')


insert into schedule values ('CS448', 'S2006', 1, 'Wednesday', '09:30', 'MC4063')
insert into schedule values ('CS448', 'S2006', 1, 'Friday', '09:30', 'MC4063')
insert into schedule values ('CS448', 'S2006', 2, 'Monday', '11:30', 'MC4058')
insert into schedule values ('CS448', 'S2006', 2, 'Wednesday', '11:30', 'MC4058')
insert into schedule values ('CS448', 'S2006', 2, 'Friday', '11:30', 'MC4058')
PRACTICAL No : 4
Aim-Implementation of Character Array

Aim: Write a program to implement Login module of the project using character
Array.

Program:
char password[10]={"codechamp"};
void main()
{

char d[25]="Password Protected";


char ch,pass[10];
int i=0,j;
clrscr();
gotoxy(10,4);
for(j=0;j<20;j++)
{
delay(50);
printf("*");
}
for(j=0;j<20;j++)
{
delay(50);
printf("%c",d[j]);
}
for(j=0;j<20;j++)
{
delay(50);
printf("*");
}
gotoxy(10,10);
gotoxy(15,7);

printf("\nEnter Password:");

while(ch!=13)
{
ch=getch();

if(ch!=13 && ch!=8){


putch('*');
pass[i] = ch;
i++;
}
}
pass[i] = '\0';
if(strcmp(pass,password)==0)
{

printf("Password match");
gotoxy(17,10);

printf("Press any key to countinue.....");


}
else
{

OUTPUT:-
PRACTICAL No: 5
Implementation of Linked List

Aim: To write a program to implement the basic operations on Singly Linked List

Algorithm:

1. Start
2. Read the value of ch
3. If ch =1 then read item of number
set n as malloc to size, set n of into as num and next as list
4. If ch=2 to check if list =0 then print “Empty list”
else if n is list then delete the node n
5. If ch=3 then read position value and set n as malloc size and read num value and
store in n.
6. If ch =4 then check list as NULL or not. If NULL then print “ Empty list”
Else check I not equal to pos then print info of p then delete the node p
7. If ch=5 then set n and allocate malloc size and read num.
Else next not as Null then n as next of p
8. If ch=6 then check pos Null and print “Empty list”
Else delete the node p
9. stop

Logical Description

The singly-linked list is the most basic of all the linked data structures. A singly-linked list
is simply a sequence of dynamically allocated objects, each of which refers to its successor
in the list. Despite this obvious simplicity, there are myriad implementation variations.

Program Code
struct node
{
int data;
struct node *link;
}*head;
typedef struct node node;
void maintain(void);
void create(node *);
void insertion(node *);
void deletion(node *);
void display(node *);
int count=0;
#include"stdio.h"
#include"alloc.h"
main()
{
head=malloc(sizeof(node));
create(head);
maintain();
getch();
return;
}
void maintain()
{
int n;
clrscr();
printf(" MAINTANENCE\n\n1. Insertion\n2. Deletion\n3. Display\n4. Exit");
printf("\n\nYour choice: ");
scanf("%d",&n);
switch(n)
{
case 1:
{
insertion(head);
break;

case 2:
{
deletion(head);
break;
}
case 3:
{
display(head);
break;
}

case 4:
{
exit();
}
}
}

void create(node *p)


{
int i;
node *q;
clrscr();
printf("\nCreation of Linked List\n");
printf("\nEnter the data('0' to terminate)\n");
scanf("%d",&i);
p->data=i; count++;
scanf("%d",&i);
while(i!=0)
{
q=malloc(sizeof(node));
q->data=i; count++;
p->link=q;
scanf("%d",&i);
if(i!=0)
{
p=malloc(sizeof(node));
p->data=i; count++;
q->link=p;
}
else
{
q->link=NULL;
return;
}
scanf("%d",&i);
}
p->link=NULL;
maintain();
}
void insertion(node *p)
{
int n,m,i=1;
node *new,*next,*tail;
clrscr();
while(i==1)
{
clrscr();
p=head;
printf("\tINSERTION\n\n1. Insertion at first\n2. Insertion at last\n3. Insertion
inbetween\n\nYour choice: ");
scanf("%d",&n);

printf("\nEnter the data: ");


scanf("%d",&i);
new=malloc(sizeof(node));
new->data=i;
switch(n)
{
case 1:
{
new->link=head;
head=new;
count++; break;
}
case 2:
{
while(p->link!=NULL)
p=p->link;
p->link=new;
new->link=NULL;
count++; break;
}
case 3:
{
printf("\nEnter the location: ");
scanf("%d",&n);
if(n<2 || n>=count)
{
clrscr();
printf("Sorry!!! Location doesn't fall within the limit");
break;
}
m=1;
while(m<n-1)
{
p=p->link;
m++;
}
next=p->link;
p->link=new;
new->link=next;
count++; break;
}
}
printf("\n\nDo you want to continue insertion(1/0): ");
scanf("%d",&i);
}
maintain();
}
void deletion(node *p)
{
int n,m,i=1;
node *next,*tail;
clrscr();
while(i==1)
{
clrscr();
p=head;
printf("\tDELETION\n\n1. Deletion at first\n2. Deletion at last\n3. Deletion
inbetween\n\nYour choice: ");
scanf("%d",&n);
switch(n)
{
case 1:
{
head=head->link;
count--; break;
}
case 2:
{
while((p->link)->link!=NULL)
p=p->link;
p->link=NULL;
count--; break;
}
case 3:
{
printf("\nEnter the location: ");
scanf("%d",&n);
if(n<2 || n>=count)
{
clrscr();
printf("Sorry!!! Location doesn't fall within the limit");
break;
}
m=1;
while(m<n-1)
{
p=p->link;
m++;
}
next=p->link;
p->link=next->link;
count--; break;
}
}
printf("\n\nDo you want to continue deletion(1/0): ");
scanf("%d",&i);
}
maintain();
}
void display(node *p)
{
clrscr();
printf("\nCreated List");
printf("\n\nTotal no of elements %d\n",count);
while(p!=NULL)
{
printf("%d ",p->data);
p=p->link;
}
getch();
maintain();
}
OUTPUT

Creation of Linked List

Enter the data('0' to terminate)


23467890

MAINTANENCE

1. Insertion
2. Deletion
3. Display
4. Exit

Your choice: 1

INSERTION

1. Insertion at first
2. Insertion at last
3. Insertion inbetween

Your choice: 1

Enter the data: 1


Created List
Total no of elements 7
12346789

Do you want to continue insertion(1/0):


PRACTICAL No : 6
Implementation of sorting and Searching

Program for Sorting:


#include<stdio.h>
#include<stdlib.h>

/* structure for a node */


struct Node
{
int data;
struct Node *next;
};

/* Function to insert a node at the beginning of a linked list */


void insertAtTheBegin(struct Node **start_ref, int data);

/* Function to bubble sort the given linked list */


void bubbleSort(struct Node *start);

/* Function to swap data of two nodes a and b*/


void swap(struct Node *a, struct Node *b);

/* Function to print nodes in a given linked list */


void printList(struct Node *start);

int main()
{
int arr[] = {12, 56, 2, 11, 1, 90};
int list_size, i;

/* start with empty linked list */


struct Node *start = NULL;

/* Create linked list from the array arr[].


Created linked list will be 1->11->2->56->12 */
for (i = 0; i< 6; i++)
insertAtTheBegin(&start, arr[i]);

/* print list before sorting */


printf("\n Linked list before sorting ");
printList(start);

/* sort the linked list */


bubbleSort(start);

/* print list after sorting */


printf("\n Linked list after sorting ");
printList(start);

getchar();
return 0;
}

/* Function to insert a node at the beginning of a linked list */


void insertAtTheBegin(struct Node **start_ref, int data)
{
struct Node *ptr1 = (struct Node*)malloc(sizeof(struct Node));
ptr1->data = data;
ptr1->next = *start_ref;
*start_ref = ptr1;
}

/* Function to print nodes in a given linked list */


void printList(struct Node *start)
{
struct Node *temp = start;
printf("\n");
while (temp!=NULL)
{
printf("%d ", temp->data);
temp = temp->next;
}
}

/* Bubble sort the given linked list */


void bubbleSort(struct Node *start)
{
int swapped, i;
struct Node *ptr1;
struct Node *lptr = NULL;
/* Checking for empty list */
if (start == NULL)
return;

do
{
swapped = 0;
ptr1 = start;

while (ptr1->next != lptr)


{
if (ptr1->data > ptr1->next->data)
{
swap(ptr1, ptr1->next);
swapped = 1;
}
ptr1 = ptr1->next;
}
lptr = ptr1;
}
while (swapped);
}

/* function to swap data of two nodes a and b*/


void swap(struct Node *a, struct Node *b)
{
int temp = a->data;
a->data = b->data;
b->data = temp;
}

Output:

Linked list before sorting


90 1 11 2 56 12
Linked list after sorting
1 2 11 12 56 90
Program for Searching:
//code to implement binary search
// on Singly Linked List

#include<stdio.h>
#include<stdlib.h>

struct Node
{
int data;
struct Node* next;
};

Node *newNode(int x)
{
struct Node* temp = new Node;
temp->data = x;
temp->next = NULL;
return temp;
}

// function to find out middle element


struct Node* middle(Node* start, Node* last)
{
if (start == NULL)
return NULL;

struct Node* slow = start;


struct Node* fast = start -> next;

while (fast != last)


{
fast = fast -> next;
if (fast != last)
{
slow = slow -> next;
fast = fast -> next;
}
}

return slow;
}

// Function for implementing the Binary


// Search on linked list
struct Node* binarySearch(Node *head, int value)
{
struct Node* start = head;
struct Node* last = NULL;

do
{
// Find middle
Node* mid = middle(start, last);

// If middle is empty
if (mid == NULL)
return NULL;

// If value is present at middle


if (mid -> data == value)
return mid;

// If value is more than mid


else if (mid -> data < value)
start = mid -> next;

// If value is less than mid.


else
last = mid;

} while (last == NULL ||


last -> next != start);

// value not present


return NULL;
}

// Driver Code
int main()
{
Node *head = newNode(1);
head->next = newNode(4);
head->next->next = newNode(7);
head->next->next->next = newNode(8);
head->next->next->next->next = newNode(9);
head->next->next->next->next->next = newNode(10);
int value = 7;
if (binarySearch(head, value) == NULL)
printf("Value not present\n");
else
printf("Present");
return 0;
}

Output:
Input : Enter value to search : 7
Output : Found

Input : Enter value to search : 12


Output : Not Found
PRACTICAL No : 7
Implementation of Stack

1. Start
2. Declare an one dimensional array variable
3. Using Switch case statement select one appropriate operation
a. PUSH:
i. Read an element
ii. Store it at the top of the stack
iii. Increment the TOS
b. POP
i. Read the element from the top of the stack
ii. Display it
iii. Decrement the TOS
c. PEEK
i. Read the element from the top of the stack
ii. Display it
4. Stop

Logical Description

The program implements the Stack using one dimensional array. Initially an array variable
is declared, and variable TOS is to point the top of the stack is also declared. PUSH
operation is done by reading an element from the user and storing it at TOS position. POP
operation retrieves the value at TOS and removes the element; TOS now points the next
element. PEEK operation retrieves the element but it is not deleted from the stack.

Program Code
/*Stack Using Arrays*/
#include<stdio.h>
#include<conio.h>
#define SIZE 4
int a[SIZE],top=0,ch=1,i,n;
void display();
void create();
void push();
void pop();
void peek();
void main()
{
clrscr();
printf("\n\t\tStack Using Arrays");
printf("\n\t\t------------------");
printf("\n\t\t 1.Create");
printf("\n\t\t 2.Push");
printf("\n\t\t 3.Pop");
printf("\n\t\t 4.Peek");
printf("\n\t\t 5.Exit\n");
do
{
printf("\t\tEnter Your Choice :");
scanf("%d",&ch);
switch(ch)
{
case 1: create();break;
case 2: push();break;
case 3: pop();break;
case 4: peek();break;
default: printf("\t\tExit");
break;
}
}while(ch!=5);
getch();
}
void create()
{
if(top==0)
{
printf("\t\tNumber of Element:");
scanf("%d",&n);
if(n>SIZE)
printf("\t\tSize of Stack is Large\n");
else
{
printf("\t\tPushing Element :");
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
top=n;
display();
}
}
else
{
printf("\tStack Already Exists");
}}
void push()
{
if(top==SIZE)
printf("\t\tStack Overflow\n");
else
{
printf("\t\tPushing Element :");
scanf("%d",&a[++top]);
display();
}}
void pop()
{
if(top==0)
printf("\t\tStack Underflow\n");
else
{
printf("\t\tPop Element :%d\n",a[top--]);
display();
}}
void display()
{
if(top==0)
printf("\t\tStack is Empty");
else
{
printf("\t\tStack Contains :");
for(i=top;i>=1;i--)
printf("%d ",a[i]);
}
printf("\n");
}
void peek()
{
if(top==0)
printf("\t\tStack Empty\n");
else
printf("\t\tTop Element is %d\n",a[top]);
}
OUTPUT:-

IMPLEMENTATION OF STACK USING ARRAYS

Choose the option

1. Push
2. Pop
3. Peek
4. Display
5. Exit

Enter your option: 1


Enter the element to be pushed into the stack: 12

Choose the option

1. Push
2. Pop
3. Peek
4. Display
5. Exit

Enter your option: 1


Enter the element to be pushed into the stack: 15

Choose the option

1. Push
2. Pop
3. Peek
4. Display
5. Exit
Enter your option: 1
Enter the element to be pushed into the stack: 34
Choose the option

1. Push
2. Pop
3. Peek
4. Display
5. Exit
Enter your option: 4
The elements in the stack are: 12
15
34
Choose the option

1. Push
2. Pop
3. Peek
4. Display
5. Exit

Enter your option: 2

The Popped value is: 34

Choose the option

1. Push
2. Pop
3. Peek
4. Display
5. Exit

Enter your option: 4


The elements in the stack are: 12
15
Choose the option

1. Push
2. Pop
3. Peek
4. Display
5. Exit

Enter your option: 3


The Popped value is: 15
PRACTICAL No : 8
Implementation of Queue

Aim: To write a program to create a Queue using one dimensional Array

Algorithm:

1. Start
2. Read ch and n value
3. check (ch!=4) and if (rear =n) then read value
4. Assign value to q(rear) and increment the rear, else Display as queue if full
5. if (front=rear) then print front value of queue and increment front
Else Display as queue is empty
6. if (rear=0) then assign front to I and write q[i].
Else Display as queue is empty
7. Go to step 3
8. stop

Logical Description
A collection of items in which only the earliest added item may be accessed. Basic
operations are add (to the tail) or enqueue and delete (from the head) or dequeue. Delete
returns the item removed. Also known as "first-in, first-out" or FIFO.

Formal Definition: It is convenient to define delete or dequeue in terms of remove and a


new operation, front. The operations new(), add(v, Q), front(Q), and remove(Q) may be
defined with axiomatic semantics as follows.

1. new() returns a queue


2. front(add(v, new())) = v
3. remove(add(v, new())) = new()
4. front(add(v, add(w, Q))) = front(add(w, Q))
5. remove(add(v, add(w, Q))) = add(v, remove(add(w, Q)))

Program Code

#include<stdio.h>
#include<conio.h>
void main()
{
int q[50],i,j,n,op,front,rear,no;
clrscr();
printf("Enter the size of the queue:");
scanf("%d",&n);
printf("QUEUE OPERATIONS:\n1.Insert\n2.Delete\n3.Display\n4.Exit\n");
printf("Enter the operation to be performed:");
scanf("%d",&op);
front=0;rear=-1;
while(op!=4){
switch(op)
{
case 1:{
if(rear==n-1)
printf("\nQueue is full!\n");
else
{
printf("Enter the element to be inserted:");
scanf("%d",&no);
q[++rear]=no;
printf("\n%d is inserted!",no);
}
break; }
case 2: {
if(front==0 && rear==-1)
printf("\nQueue is empty!");
else
{
printf("\n%d is deleted!",q[front]);
q[front]=0;
for(j=front;j<rear;j++)
q[j]=q[j+1];
rear=rear-1;
}
break; }
case 3: {
printf("\nQueue is:\n");
for(i=front;i<=rear;i++)
printf("%d\t",q[i]);
break; }
}
printf("\nEnter the operation to be performed:");
scanf("%d",&op);
}
getch(); }
OUTPUT

Enter the size of the queue:3


QUEUE OPERATIONS:
1.Insert
2.Delete
3.Display
4.Exit
Enter the operation to be performed:1
Enter the element to be inserted:12

12 is inserted!
Enter the operation to be performed:1
Enter the element to be inserted:32

32 is inserted!
Enter the operation to be performed:3

Queue is:
12 32
Enter the operation to be performed:2

12 is deleted!
Enter the operation to be performed:3

Queue is:
32
Enter the operation to be performed:2

32 is deleted!
Enter the operation to be performed:2

Queue is empty!
Enter the operation to be performed:3

Queue is:

Enter the operation to be performed: 4


PRACTICAL No : 09
Implementation of Efficient Sorting techniques to sort data.

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

quick sort
#include<stdio.h>
#include<conio.h>
#define max 100 int
a[max],n,i,l,h; void
main()
{
void input(void);
input();
getch();
}

void input(void)
{
void output(int a[],int n);
void quick_sort(int a[],int l,int h); printf("How many
elements in the array : "); scanf("%d",&n);
printf("\n");
printf("Enter the elemennts : \n");
for(i=0;i<=n-1;i++)
{
scanf("%d",&a[i]);
}
l=0;
h=n-1;
quick_sort(a,l,h);
printf("Sorted Array :\n "); output(a,n);
}
void quick_sort(int a[],int l, int h)
{
int temp,key,low,high;
low=l;
high=h;
key=a[(low+high)/2]; do
{
while(key>a[low])
{
low++;
}
while(key<a[high])
{
high--;
}
if(low<=high)
{
temp=a[low];
a[low++]=a[high];
a[high--]=temp;
}
} while(low<=high);
if(l<high)
quick_sort(a,l,high);
if(low<h)
quick_sort(a,low,h);
}
void output(int a[],int n)
{
for(i=0;i<=n-1;i++)
{printf("%d\n",a[i]);}
}
Output of program:
PRACTICAL No : 10
Implementation of Efficient Searching techniques to Search on run time
requirement data.

Algorithm
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
linear search
#include<stdio.h>
#include<conio.h>
void main()
{
int a[100],n,i,item,loc=-1; clrscr();
printf("\nEnter the number of element:"); scanf("%d",&n);
printf("Enter the number:\n");
for(i=0;i<=n-1;i++)
{
scanf("%d",&a[i]);
}
printf("Enter the no. to be search\n"); scanf("%d",&item);
for(i=0;i<=n-1;i++)
{
if(item==a[i])
{
loc=i;
break;
}
}
if(loc>=0)
{ printf("\n%dis found in position%d",item,loc+1); else
printf("\nItem does not exits"); getch();
}
Algorithm To Search An Element Using Binary Search

1. low = 1,high = n
2. Repeat step 3 to 5 while low <= high
3. mid = (low + high)
4. If a[mid] = x
Print “ found at mid”
Return
5. If (a[mid] < x)
low = mid + 1
Else
High = mid – 1
6. Print “x not found”
7. Exit

//binary search
#include<stdio.h>
#include<conio.h> void main()
{
int a[100],i,loc,mid,beg,end,n,flag=0,item; clrscr();
printf("How many elements"); scanf("%d",&n);
printf("Enter the element of the array\n");
for(i=0;i<=n-1;i++)
{
scanf("%d",&a[i]);
}
printf("Enter the element to be searching\n");
scanf("%d",&item);
loc=0; beg=0; end=n-1;
while((beg<=end)&&(item!=a[mid]))
{
mid=((beg+end)/2);
if(item==a[mid])
{
printf("search is successfull\n"); loc=mid;
printf("position of the item%d\n",loc+1); flag=flag+1;
}
else
{
if(item<a[mid])
end=mid-1;
beg=mid+1;
}
if(flag==0)
{
printf("search is not successfull\n");
}
getch();
}

Output of program:

Data Structure Using C Lab Manual, Department of Information Technology, GHRCE,


Nagpur

Вам также может понравиться