Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Start with the provided linked list program, next make changes to handle a doubly linked list, you are free to refer to any web source,

Start with the provided linked list program, next make changes to handle a doubly linked list, you are free to refer to any web source, but your final program should be a modified version of the provided linked list program.

A// declaring functions prototypes

#include

#include

using namespace std;

struct node {

int data;

struct node *next;

};

struct node *start = NULL;

struct node *create_ll(struct node *);

struct node *display(struct node *);

struct node *remove_beg(struct node *);

struct node *remove_list(struct node *);

struct node *remove_end(struct node *);

struct node *sort_list(struct node *);

struct node *remove_sorted(struct node *);

struct node *insert_sorted(struct node *);

int main (){

int option;

do {

cout << " Main Menu: ";

cout << " 1: Create a List";

cout << " 2: Display a List";

cout << " 3: Remove from the beginning of the List";

cout << " 4: Remove the whole list";

cout << " 5: Remove from the end of the list";

cout << " 6: Sort List";

cout << " 7: Remove from a Sorted List";

cout << " 8: Insert into a Sorted List";

cout << " 9: Exit";

cout << " Enter your option : ";

cin >> option;

switch (option) {

case 1:

start = create_ll(start);

cout << " LINKED LIST CREATED";

break;

case 2:

start = display(start);

break;

case 3:

start = remove_beg(start);

break;

case 4:

start = remove_list(start);

break;

case 5:

start = remove_end(start);

break;

case 6:

start = sort_list(start);

break;

case 7:

start = remove_sorted(start);

break;

case 8:

start = insert_sorted(start);

break;

}

}while (option != 9);

return 0;

}

struct node *create_ll(struct node *start) {

struct node *new_node;

int num;

cout << " Enter -1 to end";

cout << " Enter the data : ";

cin >> num;

while (num != -1) {

new_node = (node *) operator new (sizeof (node));

new_node->data=num;

if (start==NULL) {

new_node->next = NULL;

start = new_node;

} else {

new_node->next = start;

start = new_node;

}

cout << " Enter the data : ";

cin >> num;

}

return start;

}

struct node *display(struct node *start) {

struct node *ptr;

ptr = start;

cout << " ";

while (ptr != NULL) {

cout << ptr->data << " ";

ptr = ptr->next;

}

return start;

}

struct node *remove_beg(struct node *start) {

struct node *ptr;

if (start != NULL) {

ptr = start;

start = start->next;

delete ptr;

}

return start;

}

struct node *remove_list (struct node *start) {

struct node *ptr;

if (start != NULL ) {

ptr=start;

while(ptr != NULL) {

cout << " " << ptr->data << " is removed ";

start = remove_beg(ptr);

ptr = start;

}

}

return start;

}

struct node *remove_end(struct node *start){

struct node *ptr, *preptr;

if (start != NULL) {

ptr=start;

preptr=NULL;

while (ptr->next != NULL) {

preptr = ptr;

ptr = ptr->next;

}

if (preptr != NULL) {

preptr->next = NULL;

delete ptr;

} else {

start = remove_beg(ptr);

}

}

return start;

}

struct node *sort_list (struct node *start) {

struct node *ptr1, *ptr2;

int temp;

if (start != NULL) {

ptr1 = start;

while (ptr1->next != NULL) {

ptr2 = ptr1->next;

while(ptr2 != NULL) {

if (ptr1->data > ptr2->data){

temp = ptr1->data;

ptr1->data = ptr2->data;

ptr2->data = temp;

}

ptr2=ptr2->next;

}

ptr1=ptr1->next;

}

}

return start;

}

struct node *remove_sorted(struct node *start) {

struct node *ptr, *preptr;

int val;

bool matched;

cout << " Enter the value of the node which has to be deleted : ";

cin >> val;

if (start != NULL) {

ptr = start;

preptr=NULL;

while (ptr->data != val && ptr->next != NULL) {

preptr=ptr;

ptr = ptr->next;

}

if (ptr->data == val) {

if (preptr != NULL){

preptr->next = ptr->next;

delete ptr;

} else {

start = remove_beg(ptr);

}

}else {

cout << " no match ";

}

}

return start;

}

struct node *insert_sorted(struct node *start){

struct node *new_node, *ptr, *preptr;

int num;

cout << " Enter the data : ";

cin >> num;

new_node = (node *) operator new (sizeof (node));

new_node->data = num;

if (start != NULL ) {

ptr= start;

while (ptr->data < num) {

preptr = ptr;

ptr = ptr->next;

if (ptr == NULL) {

break;

}

}

if (ptr == NULL) {

preptr->next = new_node;

new_node->next = NULL;

} else if (ptr == start) {

new_node->next = start;

start = new_node;

} else {

new_node->next = ptr;

preptr->next = new_node;

}

} else {

start = new_node;

start->next=NULL;

}

return start;

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Medical Image Databases

Authors: Stephen T.C. Wong

1st Edition

1461375398, 978-1461375395

More Books

Students also viewed these Databases questions

Question

Discuss the key people management challenges that Dorian faced.

Answered: 1 week ago

Question

How fast should bidder managers move into the target?

Answered: 1 week ago