Question

In: Computer Science

Modify the linked list code from class to work with strings. Insert the following food items...

Modify the linked list code from class to work with strings. Insert the following food items into the list and display the list. The items are: bread, noodles, milk, bananas, eggs. Insert them in that order. Display the list. Then delete milk and redisplay the list. Then insert ice cream and redisplay the list. Then append zucchini and redisplay the list.

c++

Solutions

Expert Solution

#include <iostream>
#include <string>
using namespace std;
 
// A linked list node
struct Node
{
   string data;
   struct Node *next;
};

//insert a new node in front of the list
void push(struct Node** head, string node_data)
{
   /* 1. create and allocate node */
   struct Node* newNode = new Node;
 
   /* 2. assign data to node */
   newNode->data = node_data;
 
   /* 3. set next of new node as head */
   newNode->next = (*head);
 
   /* 4. move the head to point to the new node */
   (*head) = newNode;
}

/* insert new node at the end of the linked list */
void append(struct Node** head, string node_data)
{
/* 1. create and allocate node */
struct Node* newNode = new Node;
 
struct Node *last = *head; /* used in step 5*/
 
/* 2. assign data to the node */
newNode->data = node_data;
 
/* 3. set next pointer of new node to null as its the last node*/
newNode->next = NULL;
 
/* 4. if list is empty, new node becomes first node */
if (*head == NULL)
{
*head = newNode;
return;
}
 
/* 5. Else traverse till the last node */
while (last->next != NULL)
last = last->next;
 
/* 6. Change the next of last node */
last->next = newNode;
return;
}
 
// display linked list contents
void displayList(struct Node *node)
{
   //traverse the list to display each node
   while (node != NULL)
   {
      cout<<node->data<<"-->";
      node = node->next;
   }
 
if(node== NULL)
cout<<"null"; 
} 

// delete node from linked list
void deleteNode(struct Node **head, string key)
{
    // Store head node
    struct Node* temp = *head, *prev;
 
    // If head node itself holds the key to be deleted
    if (temp != NULL && temp->data == key)
    {
        *head = temp->next;   // Changed head
        free(temp);               // free old head
        return;
    }
 
    // Search for the key to be deleted, keep track of the
    // previous node as we need to change 'prev->next'
    while (temp != NULL && temp->data != key)
    {
        prev = temp;
        temp = temp->next;
    }
 
    // If key was not present in linked list
    if (temp == NULL) return;
 
    // Unlink the node from linked list
    prev->next = temp->next;
 
    free(temp);  // Free memory
}
/* main program for linked list*/
int main() 
{ 
/* empty list */
struct Node* head = NULL; 
 
// insert bread, noodles, milk, bananas, eggs
push(&head, "bread"); 
push(&head, "noodles");  
push(&head, "milk");  
push(&head, "bananas");  
push(&head, "eggs");

cout<<"After inserting foods in linked list: "<<endl;
displayList(head);

//delete milk
deleteNode(&head,"milk");

cout<<"\n\nAfter deleting milk from linked list: "<<endl;
displayList(head);

//insert ice cream
push(&head, "ice cream");

cout<<"\n\nAfter inserting ice cream in linked list: "<<endl;
displayList(head);

//append zucchini
append(&head, "zucchini"); 

cout<<"\n\nAfter appending zucchini in linked list: "<<endl;
displayList(head);
 
return 0;
}

OUTPUT :

*******************PLEASE UPVOTE********************************


Related Solutions

Modify this linked list code to work with string. Insert the following items into the list...
Modify this linked list code to work with string. Insert the following items into the list and display the list. The items are: Pepsi, Coke, DrPepper, Sprite, Fanta. Insert them in that order. Display the list. Then delete DrPepper and redisplay the list. Then insert 7-UP and redisplay the list. Then append Water and redisplay the list. c++ ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ #include <iostream> using namespace std; class ListNode { public:     int value;     ListNode *next;     ListNode(int nodeValue) {       value...
Write a C++ code to insert the following numbers in two Linked Lists. Insert numbers of...
Write a C++ code to insert the following numbers in two Linked Lists. Insert numbers of first list in Linked List#1, and numbers of second list in Linked List#2. Do not insert both lists in a single Linked List. List#1. 5, 78, 45, 23, 11, 89, 10, 78, 6, 99, 876, 5, 67, 13 List#2. 5, 89, 688, 52, 557, 953, 5, 7, 55, 35, 89, 99, 99, 6, 557, 89, 5, 99, 6, 2, 45, 12, 7, 6, 94,...
Linked List: Complete the following code to create a linked list from an Array. After creating...
Linked List: Complete the following code to create a linked list from an Array. After creating the list, display the elements of the linked list iteratively. Write two others function called as RDisplayTailRecursion(first) and RDisplayTailRecursion(first) which will print elements of the linked list using the tail and head recursions respectively. #include <stdio.h> #include <stdlib.h> struct Node { }*first=NULL; void create(int A[], int n) { for(i=1; i<n; i++) { } } void Display(struct Node*p) { while(p!=NULL) { } } void RDisplayTailRecursion...
Explain and demonstrate a Linked List by adding following items into a Linked List: 10, 30,...
Explain and demonstrate a Linked List by adding following items into a Linked List: 10, 30, 15, 25 (show your work, you may write on paper and upload if you prefer)
In Python, I've created a Node class for implementing a singly linked list. My Code: class...
In Python, I've created a Node class for implementing a singly linked list. My Code: class Node: def __init__(self,initdata): self.data = initdata self.next = None def getData(self): return self.data def getNext(self): return self.next def setData(self,newdata): self.data = newdata def setNext(self,newnext): self.next = newnext class SinglyLinkedList: def __init__(self): self.head = None def add(self,key): addkey = Node(key) addkey.setNext(self.head) self.head = addkey Now the question is: Create an append method that is O(1) by modifying the constructor of the SinglyLinkedList class by adding...
java Modify doubly Linked List code to include following index (rank) based access operations int RetrieveAt(int...
java Modify doubly Linked List code to include following index (rank) based access operations int RetrieveAt(int index) void DeleteAt(int index) void Swap(int index, int index) index starts at 0 (just like array's subscript) If the index is out of bound, RetrieveAt returns 0, DeleteAt does nothing, do nothing in Swap. Write your own testing program to test the modified class -----------------------------------------DLinkedlist.java---------------------------- public class DLinkedList { private class Node { String data; Node next; Node prev; public Node(String s) { data...
Using the singly linked list code as a base, create a class that implements a doubly...
Using the singly linked list code as a base, create a class that implements a doubly linked list. A doubly linked list has a Previous link so you can move backwards in the list. Be sure the class is a template class so the user can create a list with any data type. Be sure to test all the member functions in your test program. c++
Java Write a menu driven program that implements the following linked list operations : INSERT (at...
Java Write a menu driven program that implements the following linked list operations : INSERT (at the beginning) INSERT_ALPHA (in alphabetical order) DELETE (Identify by contents, i.e. "John", not #3) COUNT CLEAR
Write a code to implement a python queue class using a linked list. use these operations...
Write a code to implement a python queue class using a linked list. use these operations isEmpty • enqueue. • dequeue    • size Time and compare the performances of the operations ( this is optional but I would appreciate it)
Take the following code and modify the if(args >= 3) statement to work with a dynamic...
Take the following code and modify the if(args >= 3) statement to work with a dynamic amount of inputs. Example: ./cat file1 file2 file3 file4 filen-1 filen should output a text file that concatenates the files file1 to filen into one file #include <stdio.h> #include <stdlib.h> int main(int argc, char **argv) {    char ch;    if (argc ==1)    {        while((ch = fgetc(stdin)) != EOF) fputc(ch, stdout);    }    if (argc == 2)    {   ...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT