Question

Linkedlist implementation in C++ The below code I have written is almost done, I only need...

Linkedlist implementation in C++

The below code I have written is almost done, I only need help to write the definition for delete_last() functio​n.

Language C++

// LinkedList.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <string>
#include <iostream>

using namespace std;

struct Node
{
int dataItem;//Our link list stores integers
Node *next;//this is a Node pointer that will be areference to next node in the list
};

class LinkedList
{
private:
Node *first;
public:
LinkedList();
void display();
void insert_first(int);
void insert_last(int);
int delete_first();
int delete_last();
void delete_item(int);
};

//Constructor creates an emptyt list
LinkedList::LinkedList()
{
first = NULL;

}

//Display method
void LinkedList::display()
{
Node *it = first;//This is a pointer for traversing
while (it != NULL)
{
  cout << it -> dataItem << "->";
  it = it->next;
}
cout << "null" << endl;
}

//insert_first() - inserts an item at the beginning
void LinkedList::insert_first(int x)
{
Node *temp = new Node;
temp->dataItem = x;
temp->next = first;
first = temp;
}

//insert_first() - inserts an item at the end
void LinkedList::insert_last(int x)
{
Node *temp = new Node;
temp->dataItem = x;
temp->next = NULL;
Node *ptr = first;//pointer for traversing
if (first == NULL)
{
  first = temp;
}
else
{
  while (ptr->next != NULL)
  {
   ptr = ptr->next;
  }
  ptr->next = temp;
}
}

/*Delete the first item in the list and return it.
If the list is empty, return NULL. (NULL is 0-not very
good since 0 could be in item */
int LinkedList::delete_first()
{
if (first == NULL) return NULL;

int x = first->dataItem;
first = first->next;
return x;
}

/*Delete the last item in the list and return it.
If the list is empty, return NULL. (NULL is 0-not very
good since 0 could be in item */
int LinkedList::delete_last()
{


}

/* Delete a given item from the list, if it is in the list.
   Delete the first one that is equal to the given.*/
void LinkedList::delete_item(int x)
{
//Two pointers for traversing
Node *ptr = first;
Node *pre = NULL;
while (ptr != NULL)
{
  if (ptr->dataItem == x)
  {
   break;
  }
  else
  {
   pre = ptr;
   ptr = ptr->next;
  }
   
}
if (pre == NULL)
{
  first = first->next;
}
else if (ptr != NULL)
{
  pre->next = ptr->next;
}
}

int main()
{
LinkedList myList;
cout << myList.delete_first() << endl;
//cout << myList.delete_last() << endl;
myList.insert_last(3);
myList.insert_first(5);
myList.insert_first(4);
myList.insert_first(10);
myList.insert_first(11);
myList.display();
myList.insert_last(13);
myList.display();
cout << myList.delete_first() << endl;
myList.display();
//cout << myList.delete_last() << endl;
myList.display();
myList.delete_item(31);
myList.display();
myList.delete_item(4);
myList.display();

for (;;);
return 0;
}

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

ANSWER: Here I write the delete last node funtion and it is working properly below is the code and output.

CODE:

//#include <stdafx.h>
#include <string>
#include <iostream>
using namespace std;
struct Node
{
int dataItem;//Our link list stores integers
Node *next;//this is a Node pointer that will be areference to next node in the list
};
class LinkedList
{
private:
Node *first;
public:
LinkedList();
void display();
void insert_first(int);
void insert_last(int);
int delete_first();
int delete_last();
void delete_item(int);
};
//Constructor creates an emptyt list
LinkedList::LinkedList()
{
first = NULL;
}
//Display method
void LinkedList::display()
{
Node *it = first;//This is a pointer for traversing
while (it != NULL)
{
cout << it -> dataItem << "->";
it = it->next;
}
cout << "null" << endl;
}
//insert_first() - inserts an item at the beginning
void LinkedList::insert_first(int x)
{
Node *temp = new Node;
temp->dataItem = x;
temp->next = first;
first = temp;
}
//insert_first() - inserts an item at the end
void LinkedList::insert_last(int x)
{
Node *temp = new Node;
temp->dataItem = x;
temp->next = NULL;
Node *ptr = first;//pointer for traversing
if (first == NULL)
{
first = temp;
}
else
{
while (ptr->next != NULL)
{
ptr = ptr->next;
}
ptr->next = temp;
}
}
/*Delete the first item in the list and return it.
If the list is empty, return NULL. (NULL is 0-not very
good since 0 could be in item */
int LinkedList::delete_first()
{
if (first == NULL) return NULL;
int x = first->dataItem;
first = first->next;
return x;
}
/*Delete the last item in the list and return it.
If the list is empty, return NULL. (NULL is 0-not very
good since 0 could be in item */
int LinkedList::delete_last()
{
//first check if firstNode is NULL or last node.
if(first == NULL)
return 0;
else if(first->next == NULL)
{
Node* tmp=first;
Node *temp = first;;
delete temp;
first = NULL;
return tmp->dataItem;
}
else
{
Node *one = first;
Node *two = first->next;
while(two->next != NULL){
two = two->next;
one = one->next;
}
one->next = NULL;
Node* tmp2=two;
delete two;
return tmp2->dataItem;
}
}
/* Delete a given item from the list, if it is in the list.
Delete the first one that is equal to the given.*/
void LinkedList::delete_item(int x)
{
//Two pointers for traversing
Node *ptr = first;
Node *pre = NULL;
while (ptr != NULL)
{
if (ptr->dataItem == x)
{
break;
}
else
{
pre = ptr;
ptr = ptr->next;
}

}
if (pre == NULL)
{
first = first->next;
}
else if (ptr != NULL)
{
pre->next = ptr->next;
}
}
int main()
{
LinkedList myList;
cout << myList.delete_first() << endl;
//cout << myList.delete_last() << endl;
myList.insert_last(3);
myList.insert_first(5);
myList.insert_first(4);
myList.insert_first(10);
myList.insert_first(11);
myList.display();
myList.insert_last(20);
myList.display();
cout << myList.delete_first() << endl;
myList.display();
cout << myList.delete_last() << endl;
myList.display();
myList.delete_item(31);
myList.display();
myList.delete_item(4);
myList.display();

for (;;);
return 0;
}

OUTPUT:

11-10-4->5-3-null 11-10-4->5-3-20-null 10-4->5->3->20-null 20 10-4->5->3->null 10->4->5->3->null 10-5-3->null

Add a comment
Know the answer?
Add Answer to:
Linkedlist implementation in C++ The below code I have written is almost done, I only need...
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
  • Q) Modify the class Linked List below to make it a Doubly Linked List. Name your...

    Q) Modify the class Linked List below to make it a Doubly Linked List. Name your class DoublyLinkedList. Add a method addEnd to add an integer at the end of the list and a method displayInReverse to print the list backwards. void addEnd(int x): create this method to add x to the end of the list. void displayInReverse(): create this method to display the list elements from the last item to the first one. Create a main() function to test...

  • C++ - I have a doubly linked list, but I haven't been able to get the...

    C++ - I have a doubly linked list, but I haven't been able to get the "reverse list" option in the code to work(It's option #in the menu in the program). I received this guidance for testing: Test 4 cases by entering (in this order) c,a,z,k,l,m This tests empty list, head of list, end of list and middle of list. Then delete (in this order) a,z,l. This tests beginning, end and middle deletes. This exhaustively tests for pointer errors. #include...

  • I have a C++ code that lets me enter, display and delete a student record. I...

    I have a C++ code that lets me enter, display and delete a student record. I need to implement a function that prints the average grade score of the students I input. Below is my code and a picture of how my code looks right now. #include<iostream> #include<stdlib.h> using namespace std; //Node Declaration struct node {    string name;    string id;    int score;    node *next;   }; //List class class list {        private:        //head...

  • Your task is to complete the following function/functions: 1. Given a position in the linked list,...

    Your task is to complete the following function/functions: 1. Given a position in the linked list, delete the node at that position.(Silver problem - Mandatory ) 2. Print the sum of all negative elements in the linked list.(Gold problem) If you want, you can refer to the the previous recitation manual (which was on Linked Lists) to implement node deletion. #include <iostream> using namespace std; //----------- Define Node --------------------------------------------------- struct Node{ int key; Node *next; }; //----------- Define Linked List...

  • Hi, I hope I can get some help with the following exercise in C++( CPP): 1.Write...

    Hi, I hope I can get some help with the following exercise in C++( CPP): 1.Write an additional method called push_back(int) that will add an integer to the end of the list. You can modify the provided code. 2.Modify the Node class and LinkedList class so that you can access your parent node (double linked-list). /* definition of the list node class */ class Node { friend class LinkedList; private: int value; Node *pNext; public: /* Constructors with No Arguments...

  • Fill the code in list.cpp app.cpp: #include "list.hpp" int main() {         LinkedList aList; bool success =...

    Fill the code in list.cpp app.cpp: #include "list.hpp" int main() {         LinkedList aList; bool success = false;         aList.evensFrontOddsEnd(3);         aList.evensFrontOddsEnd(10);         aList.evensFrontOddsEnd(6);         aList.evensFrontOddsEnd(9);         aList.evensFrontOddsEnd(17);         aList.evensFrontOddsEnd(12);         aList.printForward(); // output should be: 12 6 10 3 9 17         aList.printBackward(); // output should be: 17 9 3 10 6 12         success = aList.remove(17); if(success) cout << "17 Successfully Removed." << endl;         success = aList.remove(10); if(success) cout << "10 Successfully Removed." << endl; success = aList.remove(7); // 7 is not in the list, so success should...

  • I need help and have to get this done asap: Using the following source codes provided below, create recursive functions...

    I need help and have to get this done asap: Using the following source codes provided below, create recursive functions and methods in C++ to complete the following exercises: Print the list in forward order Print the list in reverse order Print the following three lines (in this order): "The first node contains <value in first node>" "The last node contains <value in last node>" "The first node contains <value in first node>" Print the sum of all the values...

  • I need help and have to get this done asap: Using the following source codes provided...

    I need help and have to get this done asap: Using the following source codes provided below, create recursive functions and methods in C++ to complete the following exercises: Print the list in forward order Print the list in reverse order Print the following three lines (in this order): "The first node contains <value in first node>" "The last node contains <value in last node>" "The first node contains <value in first node>" Print the sum of all the values...

  • I'm having trouble getting this program to compile and run. It is in C++ Language and...

    I'm having trouble getting this program to compile and run. It is in C++ Language and being run with CodeBlocks. Problem Statement: create and manage a linked list. Program will loop displaying a menu of user operations concerning the management of a linked list. Included will be:       H create link at head       R remove link at head       T create link at tail       K remove link at tail       I remove link at ID          S search...

  • #include <iostream> using namespace std; struct ListNode { float value; ListNode *next; }; ...

    #include <iostream> using namespace std; struct ListNode { float value; ListNode *next; }; ListNode *head; class LinkedList { public: int insertNode(float num); void deleteNode(float num); void destroyList(); void displayList(); LinkedList(void) {head = NULL;} ~LinkedList(void) {destroyList();} }; int LinkedList::insertNode(float num) { struct ListNode *newNode, *nodePtr = head, *prevNodePtr = NULL; newNode = new ListNode; if(newNode == NULL) { cout << "Error allocating memory for new list member!\n"; return 1; } newNode->value = num; newNode->next = NULL; if(head==NULL) { cout << "List...

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