A vegetable shop owner maintains the data in a doubly linked list. Write a menu-driven program that maintains a doubly linked list. Each vegetables node must contain the features: unique id, date of inclusion in stock, vegetable name, shape, raw eating/cooking feature, weight, price per kilogram. Execute all the following operations and attach screenshots of outputs along with code typed/written (code screenshot will not be allowed) in the submission pdf file. 1. Inclusion of 2 different type of vegetables. 2. Display data of all vegetables in stock nodes. 3. Delete a node based on searching criteria – i) stock inclusion date is 10 days ago and ii) if a customer buys a vegetable and weight of that vegetable in stock has reached to zero. 4. Inclusion of two new vegetables (same or different). 5. Display data of all vegetables in stock.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
struct node {
int info;
int P;
struct node *next;
};
struct node *head = NULL;
struct node *cur = NULL;
void Display() {
struct node *ptr = head;
printf("\n[ ");
while(ptr != NULL) {
printf("(%d,%d) ",ptr->P,ptr->info);
ptr = ptr->next;
}
printf(" ]");
}
void include(int P, int info) {
struct node *url = (struct node*) malloc(sizeof(struct node));
url->P = P;
url->info = info;
url->next = head;
head = url;
}
struct node* delF() {
struct node *tempLink = head;
head = head->next;
return tempLink;
}
bool isEmpty() {
return head == NULL;
}
int len() {
int len = 0;
struct node *cur;
for(cur = head; cur != NULL; cur = cur->next) {
len++;
}
return len;
}
struct node* find(int P) {
struct node* cur = head;
if(head == NULL) {
return NULL;
}
while(cur->P != P) {
if(cur->next == NULL) {
return NULL;
} else {
cur = cur->next;
}
}
return cur;
}
struct node* delete(int P) {
struct node* cur = head;
struct node* previous = NULL;
if(head == NULL) {
return NULL;
}
while(cur->P != P) {
if(cur->next == NULL) {
return NULL;
} else {
previous = cur;
cur = cur->next;
}
}
if(cur == head) {
head = head->next;
} else {
previous->next = cur->next;
}
return cur;
}
void sort() {
int i, j, k, tempK, tempData;
struct node *cur;
struct node *next;
int size = len();
k = size ;
for ( i = 0 ; i < size - 1 ; i++, k-- ) {
cur = head;
next = head->next;
for ( j = 1 ; j < k ; j++ ) {
if ( cur->info > next->info ) {
tempData = cur->info;
cur->info = next->info;
next->info = tempData;
tempK = cur->P;
cur->P = next->P;
next->P = tempK;
}
cur = cur->next;
next = next->next;
}
}
}
void rev(struct node** head_ref) {
struct node* prev = NULL;
struct node* cur = *head_ref;
struct node* next;
while (cur != NULL) {
next = cur->next;
cur->next = prev;
prev = cur;
cur = next;
}
*head_ref = prev;
}
void main() {
include(1,10);
include(2,20);
include(3,30);
include(4,1);
include(5,40);
include(6,56);
printf("Original List: ");
Display();
while(!isEmpty()) {
struct node *temp = delF();
printf("\nDeleted value:");
printf("(%d,%d) ",temp->P,temp->info);
}
printf("\nList after deleting all items: ");
Display();
include(1,10);
include(2,20);
include(3,30);
include(4,1);
include(5,40);
include(6,56);
printf("\nRestored List: ");
Display();
printf("\n");
struct node *foundLink = find(4);
if(foundLink != NULL) {
printf("Element found: ");
printf("(%d,%d) ",foundLink->P,foundLink->info);
printf("\n");
} else {
printf("Element not found.");
}
delete(4);
printf("List after deleting an item: ");
Display();
printf("\n");
foundLink = find(4);
if(foundLink != NULL) {
printf("Element found: ");
printf("(%d,%d) ",foundLink->P,foundLink->info);
printf("\n");
} else {
printf("Element not found.");
}
printf("\n");
sort();
printf("sorted list: ");
Display();
rev(&head);
printf("\nReversed list: ");
Display();
}
Comments
Leave a comment