Question

Create a linked list of at least 15 different values that are prompted for and entered while the program is running. Appendin

0 0
Add a comment Improve this question Transcribed image text
Answer #1

C++ program :-

#include <bits/stdc++.h>
using namespace std;
  
// A linked list node
class Node
{
public:
int data;
Node *next;
};
  
void push(Node** head_ref, int new_data)
{
Node* new_node = new Node();
  
new_node->data = new_data;
  
new_node->next = (*head_ref);
  
(*head_ref) = new_node;
}

void deletelist(Node** head_ref)
{
Node* current = *head_ref;
Node* next;
  
while (current != NULL)
{
next = current->next;
free(current);
current = next;
}
*head_ref = NULL;
}
  
  
void insert(struct Node**head , int X)
{
   Node*temp = *head ;
   Node *t = new Node;
   //if list is empty
   if(*head==NULL)
   {
       *head = new Node;
       (*head)->data = X;
       (*head)->next = NULL;
   }
  
   else
   {
      
       if(X < temp->data) //start node
       {
           t = new Node;
           t->data = X;
           t->next = *head;
           *head = t;
       }
          
       else
       {
           while(temp->next != NULL and !(X < temp->next->data && X >= temp->data)) //as it is sorted so X must lie between the consecutive values or else at end
               temp=temp->next;
          
           if(temp->next == NULL) //X will go to end
           {
               temp->next = new Node;
               temp = temp->next;
               temp->data = X;
               temp->next = NULL;
           }
           else //X is inserted in between some nodes in list
           {
               t = new Node;
               t->data = X;
               t->next = temp->next; //make the new node's next as the next of current node because the 't' node will lie between consecutive nodes
               temp->next = t;
           }
       }
   }
}
  
void append(Node** head_ref, int new_data)
{
Node* new_node = new Node();
  
Node *last = *head_ref;
  
new_node->data = new_data;
  
new_node->next = NULL;
  
if (*head_ref == NULL)
{
*head_ref = new_node;
return;
}
  
while (last->next != NULL)
last = last->next;
  
last->next = new_node;
return;
}
  
void printList(Node *node)
{
while (node != NULL)
{
cout<<" "<<node->data;
node = node->next;
}
}
  
int main()
{
Node* head = NULL;

push(&head, 12);
push(&head, 54);
push(&head, 13);
push(&head, 12);
push(&head, 32);
push(&head, 26);
push(&head, 12);
push(&head, 8);
push(&head, 87);
push(&head, 34);
push(&head, 9);
push(&head, 63);
push(&head, 15);
push(&head, 76);
push(&head, 56);
push(&head, 10);
cout<<"The list :"<<endl;
printList(head);
  
append(&head, 26);
cout<<endl<<"The list after apending : "<<endl;
printList(head);
  
deletelist(&head);
  
cout<<endl<<"List is deleted "<<endl;
  
insert(&head, 12);
insert(&head, 54);
insert(&head, 13);
insert(&head, 12);
insert(&head, 32);
insert(&head, 26);
insert(&head, 12);
insert(&head, 8);
insert(&head, 87);
insert(&head, 34);
insert(&head, 9);
insert(&head, 63);
insert(&head, 15);
insert(&head, 76);
insert(&head, 56);
insert(&head, 10);
  
cout<<endl<<"The list after inserting values : "<<endl;
printList(head);
  
return 0;
}

Output:-

Execute Result... CPU Time: 0.ee sec(s), Memory: 3288 kilobyte (s) The list 10 56 76 15 63 9 34 87 8 12 26 32 12 13 54 12 The

Add a comment
Know the answer?
Add Answer to:
Create a linked list of at least 15 different values that are prompted for and entered...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Please use C++ CS3358 Insert and delete a node Programming Project 2: The linked list -...

    Please use C++ CS3358 Insert and delete a node Programming Project 2: The linked list - Reference: chapter 18: Create an array of 15 student records that should not be sorted Create a liked list of 15 student record nodes. Each node is a node of one student record from the above unsorted array. The list of student records should be sorted by student ID. (Insert function without sort function to create a linked list.) (If you insert correctly, the...

  • #1. Single linked list - find... Extra info/hint? It's free For this problem, I have a...

    #1. Single linked list - find... Extra info/hint? It's free For this problem, I have a complete linked list program which supports the following commands from the user: insert, display, delete, average, find, insertBefore, insertAfter. insert allows them to insert a number into the current list, display will display the numbers in the current list etc. The program is complete except that I have removed the body of the method for find. Given the following class for the nodes in...

  • Write a function to implement linked list consisting of five nodes. Store the data in the...

    Write a function to implement linked list consisting of five nodes. Store the data in the nodes in the order given below. Also, write a function to display the data stored in the implemented linked list. in C++ programming George, Paul, Ross, Joe, Elaine, Robert1 Insert three nodes in between second node and third node in the linked list that you implemented in problem 1. Store the following data in the new (inserted) nodes in the following order. (You have...

  • In NetBeans Create a new Java Application to manage Linked Lists: (Note: Do not use java.util.LinkedList)...

    In NetBeans Create a new Java Application to manage Linked Lists: (Note: Do not use java.util.LinkedList) a) Create a DateTime class 1. Add day, month, year, hours, minutes as attributes 2. Add a constructor and a toString() method 3. Implement the Comparable interface, and add a CompareTo() method 4. Add methods to get and set all attributes. b) Add to MyLinkedList class the following methods: 1. Insert a Node to a particular position in the List 2. Insert a Node...

  • Write a MIPS assembly language program that uses dynamic memory allocation to create and manage a...

    Write a MIPS assembly language program that uses dynamic memory allocation to create and manage a linked list data structure. Gives the user the following options:                         1. To create and add the first node to a linked list.          2. To add a single node to the pre-existing linked list.             The list must already exist before a new node can be added.            The nodes should be maintained in ascending order based on the data value within the nodes...

  • Programming in C: I am trying to modify this linked list to be doubly linked list....

    Programming in C: I am trying to modify this linked list to be doubly linked list. I’m also trying to add a print in reverse function. I’m really struggling with how to change the insert function to doubly link the nodes without effecting the alphabetical sorting mechanism. Example of desired output: Enter your choice: 1 to insert an element into the list. 2 to delete an element from the list. 3 to end. ? 1 Enter a character: a The...

  • Write a java program implementing the Linked list. It should be on an small office who...

    Write a java program implementing the Linked list. It should be on an small office who has 5 employees. The program ask the user for ID, First name, Last name and what field the work in(eg: accounting, programmer, HR etc). Each employee (with all the information of that paticular employee) should be placed in one node in the program. The program should repeat and ask the user for all 5 employees information. Also when you display the Linked list it...

  • Using the following definition (List.h file) for a list, implement the member functions (methods) for the...

    Using the following definition (List.h file) for a list, implement the member functions (methods) for the List class and store the implementation in a List.cpp file. Use a doubly linked list to implement the list. Write the client code (the main method and other non-class methods) and put in file driver.cpp. file: List.h typedef int ElementType; class node{ ​ElementType data; ​node * next; node* prev; }; class List { public: List(); //Create an empty list bool Empty(); // return true...

  • Linked List in Java The data node should be modeled somewhat like this: class node{ int...

    Linked List in Java The data node should be modeled somewhat like this: class node{ int node iNum; node next; } Write a program that creates a linked list and loads it with the numbers 0 to 9. Start with an empty list and then use a "for loop" to fill it. Create a linked list class, a node class, etc. Routines like makeNode and findTail should be methods in the linked list class. Create a showList function to display...

  • Build a singly linked list with 10 noes. The data are "This is my first project...

    Build a singly linked list with 10 noes. The data are "This is my first project in Data structure and algorithm". First node is "This" second "is" and so on. Build insert function, insert "the " before "Data" and delete function, delete "my" before "first". Search "in" and output it location and then append "C++" and output the linked list again. In writing this program use menu which includes ”1. Create; 2. Insert; 3. Delete; 4. Append; 5. Search; 6....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT