Question

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

  1. Write a function to implement linked list consisting of six 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

use these as names as nodes George, Paul, Ross, Joe, Elaine, Robert1.

  1. 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 to write a function for insertion operation. in C++ programming)

use these three nodes to insert Davis1, Davis2, Davis3

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

--------------------------------------------------------------------------------------------------------------------------------

#include<iostream>
#include<malloc.h>
#include<string.h>
using namespace std;
struct node//structure for representing linked list node
{
   char a[10];
   struct node *next;
};
struct node *insert(struct node *first)//inserting function
{
   struct node *n,*n1,*n2,*n3,*n4,*n5;
   first=(struct node *)malloc(sizeof(struct node));//allocating space
   strcpy(first->a,"George");//storing data in node
   n1=(struct node *)malloc(sizeof(struct node));
   first->next=n1;
   strcpy(n1->a,"Paul");
   n2=(struct node *)malloc(sizeof(struct node));
   n1->next=n2;
   strcpy(n2->a,"Ross");
   n3=(struct node *)malloc(sizeof(struct node));
   n2->next=n3;
   strcpy(n3->a,"Joe");
   n4=(struct node *)malloc(sizeof(struct node));
   n3->next=n4;
   strcpy(n4->a,"Elaine");
   n5=(struct node *)malloc(sizeof(struct node));
   n4->next=n5;
   strcpy(n5->a,"Robert1");
   n5->next=NULL;
   return first;
}
struct node *insert1(struct node *first)//inserting function
{
   struct node *n,*n1,*n2,*n3,*n4;
   n=first->next;
   n1=first->next->next;
   n2=(struct node *)malloc(sizeof(struct node));
   n->next=n2;
   strcpy(n2->a,"Davis1");
   n3=(struct node *)malloc(sizeof(struct node));
   n2->next=n3;
   strcpy(n3->a,"Davis2");
   n4=(struct node *)malloc(sizeof(struct node));
   n3->next=n4;
   strcpy(n4->a,"Davis3");
   n4->next=n1;
   return first;
}

void show(struct node * first)//function to show the data
{
   struct node *temp=first;
   while (temp!=NULL)//traversing entire linkedlist
   {
       cout<<temp->a<<"\n";//printing data present in every node.
       temp=temp->next;
   }
   cout<<"\n";
}

int main()
{
   int n;
   struct node *first=NULL,*temp;
   int i=0;
   first=insert(first);//calling insertion function.
   cout<<"----------------first inserted nodes-------------------------\n";
   show(first);//displaying data.
   first=insert1(first);//calling insertion function.
   cout<<"----------------after inserting 3 more nodes nodes-------------------------\n";
   show(first);//displaying data.
   return 0;
}

--------------------------------------------------------------------------------------------------------------------------------

Add a comment
Know the answer?
Add Answer to:
Write a function to implement linked list consisting of six nodes. Store the data in the...
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
  • 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...

  • implement a doubly-linked list in C. Each node in the linked list should contain a string,...

    implement a doubly-linked list in C. Each node in the linked list should contain a string, a pointer to the previous node (or NULL), and a pointer to the next node (or NULL). The nodes should be sorted by their strings. struct node_t { char* str; struct node_t* prev; struct node_t* next; } To maintain the doubly-linked list, you should keep a pointer to the head node of the list (or NULL if the list is empty), and a pointer...

  • Write a Python function to implement the quick sort algorithm over a singly linked list. The...

    Write a Python function to implement the quick sort algorithm over a singly linked list. The input of your function should be a reference pointing to the first node of a linked list, and the output of your function should also be a reference to the first node of a linked list, in which the data have been sorted into the ascending order. (You may use the LinkedQueue class we introduced in the lecture directly in your program.)

  • true or false A linked list is a list of items, called nodes, in which the...

    true or false A linked list is a list of items, called nodes, in which the order is determined by the address, called of the nodes 1. 2. The pointer to a linked list-that is, the pointer to the first node in the list-is stored in a separate location called the head or first. 3. A linked list is a dynamic data structure.

  • A linked list is constructed of nodes described by the following structure: struct node{ char data;...

    A linked list is constructed of nodes described by the following structure: struct node{ char data; struct node *next; }; Assume a linked list containing a sentinel node is constructed from the above nodes. Write a function named "count"-prototyped as int count(struct node*sent)- that accepts a pointer to the sentinel node; counts the number of data (non-sentinel) nodes containing the character 'A'; and returns that count as the function value.

  • Assume we have a linked list with 12 nodes pointed to by FIRST. Write a function...

    Assume we have a linked list with 12 nodes pointed to by FIRST. Write a function that deletes the first node. Write a function that deletes the 7th node. Write a function that deletes the last node.

  • using C++, NOT C language 1. Write a function called insert() to insert a node to...

    using C++, NOT C language 1. Write a function called insert() to insert a node to the beginning of a linked list. The data is passed into the function. For example insert(8) will insert a node at the beginning of the list with the number 8 in the data field. Each node in the linked list is a ListNode struct as discussed in class. 2. Write a function called print() that will traverse the entire linked list and print out...

  • Create a linked list of at least 15 different values that are prompted for and entered...

    Create a linked list of at least 15 different values that are prompted for and entered while the program is running. Appending a node attaches that node to the end of the list while inserting a node places it in order maintaining a sorted list. Create a menu driven program where you have the options to appended, insert, display, and delete a node. Display the list after first appending data showing it in no specific order, delete all nodes and...

  • C++ Linked List Implementation Motivation As we discussed in class, the data structures that you use...

    C++ Linked List Implementation Motivation As we discussed in class, the data structures that you use to implement your program can have a profound impact on it's overall performance. A poorly written program will often need much more RAM and CPU time then a well-written implementation. One of the most basic data structure questions revolves around the difference between an array and a linked list. After you finish this assignment you should have a firm understanding of their operation. Problem...

  • Implement a simple Doubly Linked List using nodes. Functions to include: an insert in a sorted...

    Implement a simple Doubly Linked List using nodes. Functions to include: an insert in a sorted list, and a delete function, including constructor. Please include comments to make the code traceable and understandable

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