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

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

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

  • CSCI 2010 Lab11 Link-Lists Lab 11A Linked-Lists Preparation Create a Visual Studio C++ Project C...

    CSCI 2010 Lab11 Link-Lists Lab 11A Linked-Lists Preparation Create a Visual Studio C++ Project C2010Lab11A Add the following to the project. //LinkedList.cpp #include <cstdlib> #include "LinkedList.h" using namespace std; //--------------------------------------------------- //List Element Members //--------------------------------------------------- ListElement::ListElement(int d, ListElement * n) {    datum=d;    next=n; } int ListElement::getDatum () const {    return datum; } ListElement const* ListElement::getNext () const {    return next; } //--------------------------------------------------- //LinkedList Members //--------------------------------------------------- LinkedList::LinkedList () {    head=NULL; } void LinkedList::insertItem(int item) {    ListElement *currPtr = head;    ListElement *prevPtr =...

  • Please rewrite this function using recursive function #include using namespace std; struct Node { char ch;...

    Please rewrite this function using recursive function #include using namespace std; struct Node { char ch; Node* next; }; class LinkedList { Node* head; public: LinkedList(); ~LinkedList(); void add(char ch); bool find(char ch); bool del(char ch); friend std::ostream& operator<<(std::ostream& out, LinkedList& list); }; LinkedList::LinkedList() { head = NULL; } LinkedList::~LinkedList() { Node* cur = head, * tmp; while (cur != NULL) { tmp = cur->next; delete cur; cur = tmp; } } void LinkedList::add(char ch) { Node* cur = head,...

  • 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). #include #include using namespace std; typedef int Type; enum Boolean { False = 0, True }; class Item { friend class SLList; public: Type getVal()...

  • I need help with todo line please public class LinkedList { private Node head; public LinkedList()...

    I need help with todo line please public class LinkedList { private Node head; public LinkedList() { head = null; } public boolean isEmpty() { return head == null; } public int size() { int count = 0; Node current = head; while (current != null) { count++; current = current.getNext(); } return count; } public void add(int data) { Node newNode = new Node(data); newNode.setNext(head); head = newNode; } public void append(int data) { Node newNode = new Node(data);...

  • a Java code Complete the provided code by adding a method named sum() to the LinkedList...

    a Java code Complete the provided code by adding a method named sum() to the LinkedList class. The sum() method should calculate the sum of all of the positive numbers stored in the linked list. The input format is the number of items in the list, followed by each of the items, all separated by spaces. Construction of the linked list is provided in the template below. The output should print the sum of the positive values in the 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
ADVERTISEMENT