0% found this document useful (0 votes)
28 views9 pages

DSA Pract04

Uploaded by

adityahtayade
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)
28 views9 pages

DSA Pract04

Uploaded by

adityahtayade
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/ 9

ITU324-Data Structures and Algorithms Laboratory

Practical No-4

AIM:-
A Vegetable and Fruit Mall wants to organize its vegetables and fruit products in a combination of
purchase pattern of customers (ascending and descending). Solve the problem by suggesting appropriate
data structures.

A singly linked list is a concrete data structure consisting of a sequence of nodes


Each node stores
 element
 link to the next node
ADVANTAGES
 Singly linked list can store data in non-contiguous locations. Thus there is no need
of compaction of memory, when some large related data is to be stored into the memory.
 Insertion and deletion of values is easier as compared to array, as no shifting of values is
involved.
 Dynamic structure (Mem. Allocated at run-time).
 We can have more than one datatype.
 Re-arrange of linked list is easy (Insertion-Deletion).
 It doesn’t waste memory.
LIMITATIONS
 Nodes can only be accessed sequentially. That means, we cannot jump to a particular node
directly.
 Because of the above disadvantage, binary search algorithm cannot be implemented on the
Singly linked list.
 There is no way to go back from one node to previous one. Only forward traversal is possible.

APPLICATIONS
 A primary advantage to a linked list as opposed to a vector is that random insertion
time is as simple as decoupling a pair of pointers and recoupling them to the new object

Department of Information Technology, GCoE, Amravati Winter 2022


ITU324-Data Structures and Algorithms Laboratory

 A vector, on the other hand generally reorganizes memory space on insertions, causing it
to be significantly slower
 A list is not as efficient, however, at doing things like adding on the end of the container,
due to the necessity to progress all the way through the list
Algorithm :- INSFIRST(INFO, LINK, START, AVAIL, ITEM)
This algorithm inserts ITEM as the first node in the list.
Step 1 : [OVERFLOW?]
If AVAIL = NULL, then :
Write : OVERFLOW, and Exit.
Step 2 : [Remove first node from AVAIL list.]
Set NEW := AVAIL and
AVAIL := LINK[AVAIL].
Step 3 :Set INFO[NEW] := ITEM.
[Copies new data into new node].
Step 4 : Set LINK[NEW] := START.
[New node now points to original first node.]
Step 5 : Set START := NEW.
[Changes START so it points to new node.]
Step 6 : Exit.
Algorithm:-INSLOC(INFO, LINK, START, AVAIL, LOC, ITEM)
This algorithm inserts ITEM so that ITEM follows the node with location LOC or inserts
ITEM as the first node when LOC = NULL.

I. STEP 1 : [OVERFLOW?]
IF AVAIL = NULL, THEN :
Write : OVERFLOW, and Exit.
Step 2 : [Remove first node from AVAIL list.]
Set NEW := AVAIL and AVAIL :=
LINK[AVAIL].
Step 3 : Set INFO[NEW] := ITEM.
[Copies new data into new node].
Step 4 : If LOC = NULL, then :

Department of Information Technology, GCoE, Amravati Winter 2022


ITU324-Data Structures and Algorithms Laboratory

[Insert as first node.]


Set LINK[NEW] := START and
START := NEW.
Else : [Insert after node with location LOC.]
Set LINK[NEW] := LINK[LOC]
and LINK[LOC] := NEW.
[End of If structure.]
Step 5 : Exit.
Algorithm:- DEL(INFO,LINK,START,AVAIL,LOC,LOCP)
Let LIST be a linked list in memory. Suppose we are given the location LOC of node N in
LIST. And ,suppose we are given the location LOCP of the node preceding N or, when N is
the first node ,we are given LOCP=NULL. The following algorithm deletes N from the list.
1.If LOCP=NULL, then:
Set START:=LINK [START]. [ Deletes first node].
else:
Set LINK[LOCP]:= LINK[LOC]. [ Deletes N node.]
[End of if structure]
2.[Return deleted node to the AVAIL list.]
set LINK[LOC]:=AVAIL and AVAIL:=LOC.
3.Exit.
Algorithm:-INSERT(INFO,LINK,START,AVAIL,ITEM)
This algorithm inserts ITEM into a sorted linked list.
1. [Use Procedure to find the location of the node preceding ITEM.]
Call FINDA(INFO,LINK,START,ITEM,LOC)
2. [Use Algorithm to insert ITEM after the node with location LOC].
Call INSLOC(INFO,LINK,START,AVAIL,LOC,ITEM).
3. Exit.
Procedure:-FINDA(INFO,LINK,START,ITEM,LOC)
This procedure finds the location LOC of the last node in a sorted list such that
INFO[LOC ]<ITEM or sets LOC=NULL.
1.[List empty?] If START=NULL, then:

Department of Information Technology, GCoE, Amravati Winter 2022


ITU324-Data Structures and Algorithms Laboratory

Set LOC:=NULL, and return.


2.[Special case ?] If ITEM< INFO[START], then: Set
LOC:=NULL, and return.
3.Set SAVE:=START and PTR:= LINK[START].
[Initializes pointers].
4.Repeat steps 5 and 6 while PTR!=NULL.
5. If ITEM< INFO[PTR]. then:
Set LOC:= SAVE, and return.
[End of if structure]
6.Set SAVE:=PTR and PTR:=LINK[PTR]
[End of Step 4 loop]
7.Set LOC:=SAVE.
8.Return.

OUTPUT:-
===================================================================
WELCOME TO FRUITS AND VEGTABLE MALL
====================================================================
!!!!!!!! Enter your choice !!!!!!!!!

-> 1 For Accept Vegetable data

-> 2 For Accept Fruits data

-> 3 For Display Sorted data

-> 4 For exit 1

Enter No. Of Items For Vegetable U Want To Enter 3

Enter name of vegtable :- 1 LADYFINGER

Enter quantity of LADYFINGER:= 30

Enter name of vegtable :- 2 SPINICH

Department of Information Technology, GCoE, Amravati Winter 2022


ITU324-Data Structures and Algorithms Laboratory

Enter quantity of SPINICH:= 10

Enter name of vegetable :- 3 POTATO

Enter quantity of POTATO:= 20

You Have Entered

LADYFINGER 30
SPINICH 10
POTATO 20

===================================================================
WELCOME TO FRUITS AND VEGTABLE MALL
====================================================================

!!!!!!!! Enter your choice !!!!!!!!!

-> 1 For Accept Vegetable data

-> 2 For Accept Fruits data

-> 3 For Display Sorted data

-> 4 For exit


2

Enter No. Of Items For Fruits U Want To Enter 3

Enter name of Fruit :-1 APPLE

Enter quantity APPLE:= 30

Enter name of Fruit :-2 MANGO

Enter quantity MANGO:= 10

Enter name of Fruit :-3 WATERMELON

Enter quantity WATERMELON:= 20

You Have Entered

APPLE 30

Department of Information Technology, GCoE, Amravati Winter 2022


ITU324-Data Structures and Algorithms Laboratory

MANGO 10
WATERMELON 20

===================================================================
WELCOME TO FRUITS AND VEGTABLE MALL
====================================================================
!!!!!!!! Enter your choice !!!!!!!!!

-> 1 For Accept Vegtable data

-> 2 For Accept Fruits data

-> 3 For Display Sorted data

-> 4 For exit


3
====================================================
VEGTABLE QUANTITY
====================================================
LADYFINGER 30

SPINICH 20

POTATO 10

====================================================

Student Source code:-


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

// Define the product structure


struct Product {
char name[100];
float price;
int quantity;
};

// Comparison function for ascending order


int compareAscending(const void *a, const void *b) {
return ((struct Product *)a)->price - ((struct Product *)b)->price;
}

// Comparison function for descending order

Department of Information Technology, GCoE, Amravati Winter 2022


ITU324-Data Structures and Algorithms Laboratory

int compareDescending(const void *a, const void *b) {


return ((struct Product *)b)->price - ((struct Product *)a)->price;
}

int main() {
int numVegetables, numFruits;

printf("Enter the number of vegetables: ");


scanf("%d", &numVegetables);

struct Product *vegetables = (struct Product *)malloc(numVegetables *


sizeof(struct Product));

printf("Enter vegetable details:\n");


for (int i = 0; i < numVegetables; i++) {
printf("Vegetable %d:\n", i + 1);
printf("Name: ");
scanf("%s", vegetables[i].name);
printf("Price: ");
scanf("%f", &vegetables[i].price);
printf("Quantity: \n");
scanf("%d", &vegetables[i].quantity);
}

printf("Enter the number of fruits: ");


scanf("%d", &numFruits);

struct Product *fruits = (struct Product *)malloc(numFruits * sizeof(struct


Product));

printf("Enter fruit details:\n");


for (int i = 0; i < numFruits; i++) {
printf("Fruit %d:\n", i + 1);
printf("Name: ");
scanf("%s", fruits[i].name);
printf("Price: ");
scanf("%f", &fruits[i].price);
printf("Quantity: ");
scanf("%d", &fruits[i].quantity);
}

// Sort the vegetable and fruit arrays


qsort(vegetables, numVegetables, sizeof(struct Product), compareAscending);
qsort(fruits, numFruits, sizeof(struct Product), compareAscending);

Department of Information Technology, GCoE, Amravati Winter 2022


ITU324-Data Structures and Algorithms Laboratory

printf("Vegetables in ascending order of price:\n");


for (int i = 0; i < numVegetables; i++) {
printf("%s - Price: %.2f, Quantity: %d\n", vegetables[i].name,
vegetables[i].price, vegetables[i].quantity);
}

printf("Fruits in ascending order of price:\n");


for (int i = 0; i < numFruits; i++) {
printf("%s - Price: %.2f, Quantity: %d\n", fruits[i].name,
fruits[i].price, fruits[i].quantity);
}

free(vegetables);
free(fruits);

return 0;
}

Output-
Enter the number of vegetables: 2
Enter vegetable details:
Vegetable 1:
Name: potato
Price: 30
Quantity:
6
Vegetable 2:
Name: tomato
Price: 20
Quantity:
6
Enter the number of fruits: 2
Enter fruit details:
Fruit 1:
Name: watermelon
Price: 30

Department of Information Technology, GCoE, Amravati Winter 2022


ITU324-Data Structures and Algorithms Laboratory

Quantity: 1
Fruit 2:
Name: apple
Price: 80
Quantity: 6
Vegetables in ascending order of price:
tomato - Price: 20.00, Quantity: 6
potato - Price: 30.00, Quantity: 6
Fruits in ascending order of price:
watermelon - Price: 30.00, Quantity: 1
apple - Price: 80.00, Quantity: 6

Viva Question:-
1. What is Linked List?
2. What is Singly Linked List?
3. How would you sort a linked list?
4. Whether Linked List is linear or Non-linear data structure?

Department of Information Technology, GCoE, Amravati Winter 2022

You might also like