Question

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 the data for all of the nodes.

3. Write a function to delete a node from the linked list. The node to delete is passed into the function. For example a call to delete(5) will delete the fifth node in the list. The function returns true if the delete operation is performed successfully, otherwise it returns false.

3. In the main program, call the insert function 10 times to create a linked list with 10 nodes. Call print() to make sure the list is created successfully. Ask the user to enter a node number to delete. Call delete(node_number) to delete the node.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

// header files

#include <iostream>

using namespace std;

// define class

class LinkedList

{

protected:

  // declare structure for the list

  struct ListNode

  {

    // declare value in node

    double value;

    // point to the next node

    ListNode *next;

    ListNode(double value = 0, ListNode *next = NULL)

    {

      this->value = value;

      this->next = next;

    }

  };

  // list head pointer

  ListNode *head;

public:

  // linked list operations

  void insert(double num);

  void printNode();

  void deleteNode(int pos);

  bool memberOf(double x);

  LinkedList(const LinkedList &);

  ~LinkedList();

  LinkedList()

  {

    head = NULL;

  }

};

// copy constuctor

LinkedList::LinkedList(const LinkedList & otherList)

{

  head = NULL;

  // point to new node

  ListNode *newNode;

  // to move through in other list

  ListNode *nodePtr;

  // to move through developing list

  ListNode *tempPtr;

  // validate that other list is empty

  if (!otherList.head)

    return;

  // let nodePtr point to the list to be copied

  nodePtr = otherList.head;

  // allocate a new node anod store

  head = new ListNode();

  head->value = nodePtr->value;

  head->next = NULL;

  // move onto next mode in the list to be copied

  nodePtr = nodePtr->next;

  // let tempPtr point to the list to be developed

  tempPtr = head;

  // copy the remaining list

  while (nodePtr != NULL)

  {

    newNode = new ListNode;

    newNode->value = nodePtr->value;

    newNode->next = NULL;

    tempPtr->next = newNode;

    tempPtr = newNode;

    nodePtr = nodePtr->next;

  }

}

// add a new element to the end of the list

void LinkedList::insert(double number)

{

  if (head == NULL)

    head = new ListNode(number);

  else

  {

    // list is not empty

    ListNode *nodePtr = head;

    //appendNode new node to the head of the list

    head = new ListNode(number, nodePtr);

  }

}



// printNode outputs a sequence of all values

void LinkedList::printNode()

{

  ListNode *nodePtr = head;

  cout << "List: ";

  if (head == NULL)

  {

    cout << "List is empty" << endl;

  }

  while (nodePtr)

  {

    // printNode value in current node

    cout << nodePtr->value << " ";

    nodePtr = nodePtr->next;

  }

}

// destrtor deallocates the memory used by the list.

LinkedList::~LinkedList()

{

  ListNode *nodePtr = head;

  while (nodePtr != NULL)

  {

    // keep track of node to be deleted

    ListNode *garbage = nodePtr;

    // move on to next node

    nodePtr = nodePtr->next;

    // delete the garbage node

    delete garbage;

  }

}


// remove method

void LinkedList::deleteNode(int pos)

{

  // to move through the list

  ListNode *nodePtr;

  // to point to the previous node

  ListNode *previousNode;

  int i = 1;

  // condition if list is empty

  if (!head)

  {

    cout << "\nERROR: Cannot delete, list is empty";

    return;

  }

  // position nodePtr at the head of list

  nodePtr = head;

  // to insert at first

  if (pos == 1)

  {

    nodePtr = head->next;

    delete head;

    head = nodePtr;

  }

  else

  {

    while (nodePtr != NULL && i < pos)

    {

      previousNode = nodePtr;

      nodePtr = nodePtr->next;

      i++;

    }

    if (nodePtr)

    {

      previousNode->next = nodePtr->next;

      delete nodePtr;

    }

    else

      cout << "\nFailed to delete position " << pos << " not found";

  }

  nodePtr = head;

  while (nodePtr)

  {

    // display the value in the node

    cout << nodePtr->value << " -> ";


    //move to the next node

    nodePtr = nodePtr->next;

  }

  cout << "Null";

}

// main function

int main()

{

  // declare a variable of chain list

  LinkedList info;


for(int i = 1; i<=10 ; ++i)

info.insert(i*i);

  info.printNode();

  double x;

  int pos;

  cout << "\nPlease enter element position to be deleted: ";

  cin >> pos;

  info.deleteNode(pos);

  return 0;

}




=====================================================================
SEE OUTPUT


THANKS, PLEASE COMMENT if there is any concern.

Add a comment
Know the answer?
Add Answer to:
using C++, NOT C language 1. Write a function called insert() to insert a node to...
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
  • Codelab question! This is c++. Consider the following code: // Linked Lists: TRAVERSE struct ListNode {...

    Codelab question! This is c++. Consider the following code: // Linked Lists: TRAVERSE struct ListNode { int data; struct ListNode *next; }; Assume that a linked list has been created and head points to a sentinel node. A sentinel node is an empty data node in the beginning of the list. It sometimes holds a sentinel value. The use of sentinel nodes is a popular trick to simplify the insert and delete operations. You may also assume that the list...

  • Given the following linked list structure called node: struct node { int val; struct node *...

    Given the following linked list structure called node: struct node { int val; struct node * ptrNext; }; Assume we have a single list created from this structure with a head pointer called ptrFirst which is declared in the global scope. a. Write a complete C function called CountEven to count all the even values in this singly linked list of arbitrary number of nodes using an iterative (non-recursive) approach. The function takes as parameter the pointer to the starting...

  • [C++] Create three functions for a singly linked list: - Function 1: Insert a string into...

    [C++] Create three functions for a singly linked list: - Function 1: Insert a string into the linked list - Function 1: Insert a node after a given node (Node* curNodeptr, Node* newNodePtr) - Function 2: Delete the node passed to it by a pointer, it will take in the head and curPtr '(Node*, Node*)' struct Node{ string data; Node *next; };

  • 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...

  • C LANGUAGE I just need the void push() function, which inserts new node to the front...

    C LANGUAGE I just need the void push() function, which inserts new node to the front of the list #include<stdio.h> #include<stdlib.h> typedef struct node { int data; struct node *next; } Node; //Creating head a as a global Node* Node *head; /* Given a node prev_node, insert a new node after the given prev_node */ void insertAfter (Node * prev_node, int new_data) { /*1. check if the given prev_node is NULL */ if (prev_node == NULL) { printf ("the given...

  • Please slove all these questions in C language Write a function insert At Position N ()...

    Please slove all these questions in C language Write a function insert At Position N () for a singly-linked list that has the following declaration and precondition: int insert At Position N (strict node **p Head, int n, int new Data); Precondition: n > 0 and the list always has enough nodes to satisfy the position specified by n. The function should allocate memory for a new node, and initialize it with the new Data value. It should then insert...

  • ***CODE MUST BE IN C++*** Using the linked list in "basic linked list" which has a...

    ***CODE MUST BE IN C++*** Using the linked list in "basic linked list" which has a STRUCTURE for each node, write FUNCTION which starts at the head and outputs the value for each node until the last node is reached. Note: your function should work with the structure that is in this program. Please do not use the example which is for a class, but this example canbe helkpful.  Also note that the function must work no matter how many nodes...

  • Exercise-1:15 points Develop a node class and a singly list class. The node class should have two...

    Exercise-1:15 points Develop a node class and a singly list class. The node class should have two state variables namely data and nextNode. The singly list class should contain the following methods . Middlelnsert-insert a node somewhere in the middle of the list . Startinsert-insert a node at start of the Linked list Endinsert-insert a node at the end of the Linked list . Delete-delete a node Traverse-prints all the node's data Reverse -reverses the linked list Note: Choose appropriate...

  • Exercise-2: 15 points Develop a node class and a doubly list class. The node class should have tw...

    Exercise-2: 15 points Develop a node class and a doubly list class. The node class should have two state variables namely data and nextNode. The doubly list class should contain the following methods: Middlelnsert- insert a node somewhere in the middle of the list Startinsert-insert a node at start of the Linked list Endinsert- insert a node at the end of the Linked list Delete-delete a node Traverse-prints all the node's data Reverse-reverses the linked list . . Note: Choose...

  • C++ Assignment Project 1 - NodeList Building upon the the ListNode/List code I would like you...

    C++ Assignment Project 1 - NodeList Building upon the the ListNode/List code I would like you to extend the interface of a list to have these member functions as well. struct ListNode { int element; ListNode *next; } Write a function to concatenate two linked lists. Given lists l1 = (2, 3, 1)and l2 = (4, 5), after return from l1.concatenate(l2)the list l1should be changed to be l1 = (2, 3, 1, 4, 5). Your function should not change l2and...

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