DSA Pract04
DSA Pract04
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.
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
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 :
OUTPUT:-
===================================================================
WELCOME TO FRUITS AND VEGTABLE MALL
====================================================================
!!!!!!!! Enter your choice !!!!!!!!!
LADYFINGER 30
SPINICH 10
POTATO 20
===================================================================
WELCOME TO FRUITS AND VEGTABLE MALL
====================================================================
APPLE 30
MANGO 10
WATERMELON 20
===================================================================
WELCOME TO FRUITS AND VEGTABLE MALL
====================================================================
!!!!!!!! Enter your choice !!!!!!!!!
SPINICH 20
POTATO 10
====================================================
int main() {
int numVegetables, numFruits;
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
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?