Question

In C++ - Learn how to implement linked lists Part 1 Node and Linked List Class...

In C++

- Learn how to implement linked lists

Part 1 Node and Linked List Class (50 pts):
Create node with public properties: Block type block and block ptr next. Create a linked list class that uses the node you generated without an add or delete method with a head and optional tail and counter. Make a driver that generates a node to test your implementation.

Part 2 Add Method (30 pts):
Create an add method in your linked list class which will add nodes to the end of the list. Append to your driver function a way to test your add function.

Part 3 Find Method (10 pts)
Create a find method in linked list class that will find a node based on its id from the block, if the node is found return the block object, if not return NULL. Add code to your driver that checks for an existing node and a non-existing node.

Part 4 Delete Method (10 pts)
Create delete method, that will find and delete a node from the linked list. If the node is removed the method return 0, and if the node doesn't exist return -1. Add to your driver a few lines that checks the delete methods.

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

****This requires some effort so please drop a like if you are satisfied with the solution****

I have satisfied all the requirements of the question and I'm providing the screenshots of code and output for your reference...

Code:

#include<iostream>
#include<conio.h>
#include<stdlib.h>
using namespace std;
class Node
{
public:
int data;
Node* next;
};
class LinkedList:public Node
{

Node *head,*tail;
public:
LinkedList()
{
head=NULL;
tail=NULL;
}
void insert(int n);
void del(int n);
void display();
Node* search();
};
void LinkedList::insert(int n)
{
Node *temp;
temp=new Node;
temp->data=n;
temp->next=NULL;
if(head==NULL)
{
head=temp;
tail=head;
}

else
{
tail->next=temp;
tail=temp;
}
}
void LinkedList::del(int n)
{
Node *curr=head,*temp=head;
int flag=0;
if(temp==NULL)
{
cout<<"\nList is Empty";
}
else{
   while(temp!=NULL)
   {
   if(temp->data==n){
       if(temp==head){
           flag=1;
           head=temp->next;
           temp->next=NULL;
           break;
               }
               else{
                   flag=1;
                   curr->next=temp->next;
                   temp->next=NULL;
                   break;
               }
           }
           curr=temp;
   temp=temp->next;
   }
   }
   if(flag==0)
       cout<<"\nElement not found in list";
}
void LinkedList::display()
{
Node *temp=head;
if(temp==NULL)
{
cout<<"\nList is Empty";
}
while(temp!=NULL)
{
cout<<temp->data;
if(temp->next)
   cout<<"-->";
temp=temp->next;
}
cout<<endl;
}
Node* LinkedList::search()
{
int n,position=0;
int flag=0;
if(head==NULL)
{
cout<<"LinkedList is Empty";
return 0;
}
cout<<"\nEnter the Value to be Searched:";
cin>>n;
Node *temp;
temp=head;
while(temp!=NULL)
{
position++;
if(temp->data==n)
{
flag=1;
cout<<"Element "<<n<<" is Found at "<<position<<" Position"<<endl;;
return temp;
}
temp=temp->next;
}
if(flag==0)
{
return NULL;
}
}
int main()
{
LinkedList l;
Node *temp;
l.insert(2);
l.insert(5);
l.insert(99);
l.insert(10);
l.insert(23);
cout<<"List after insertion:"<<endl;
l.display();
if(l.search()==NULL)
{
   cout<<"\nElement not found in the list";
   }
   cout<<"\nDeleting 10 from list"<<endl;
   l.del(10);
   l.display();
   cout<<"\nDeleting 5 from list"<<endl;
   l.del(5);
   l.display();
return 0;
}

Output Screenshot:

F:AUntitled1.exe List after insertion: 2-->5--99- ->10- - >23 Enter the Value to be Searched: 5 Element 5 is Found at 2 Posit

Code Screenshot:

#include<iostream> 1 #include<conio. h> 2 #include<stdlib.h> 3 using namespace std class Node 4 6E public: int data; Node* netemp->next NULL; if ( head==NULL) 31 32 33 head-temp; tail-head; } 34 35 36 37 38 else 39E tail->next-temp tail-temp; } } voi61 else flag 1; 62E 63 64 curr->next-temp- >next; temp->next NULL; break; 65 66 67 68 69 curr temp temp temp->next; 70 71 7291 L Node Linked List::search () 93E 92 int n,position=0; int flag 0; if ( head==NULL) 94 95 96 97E cout<<LinkedList is Empttemp temp->next } if (flag=0) 114 115 116 117E return NULL; 118 119 120 int main) 121 122E LinkedList 1; Node temp 1.insert (

Add a comment
Know the answer?
Add Answer to:
In C++ - Learn how to implement linked lists Part 1 Node and Linked List Class...
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
  • c++ Computational Complexity Create a singly linked list for storing positive integers. Each node will store...

    c++ Computational Complexity Create a singly linked list for storing positive integers. Each node will store one integer. For example, 12->3->5->777-111 is such a list. There are five nodes in this list. 12 is the head node and 111 is the tail node. (111 points to NULL.) Your linked list starts empty. It should support the following three operations: Add x to tail: Create a new node whose data field contains x. Append this node at the end of the...

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

  • Given a singly-linked list interface and linked list node class, implement the singly-linked list which has...

    Given a singly-linked list interface and linked list node class, implement the singly-linked list which has the following methods in Java: 1. Implement 3 add() methods. One will add to the front (must be O(1)), one will add to the back (must be O(1)), and one will add anywhere in the list according to given index (must be O(1) for index 0 and O(n) for all other indices). They are: void addAtIndex(int index, T data), void addToFront(T data), void addToBack(T...

  • I need this in C++. This is all one question Program 2: Linked List Class For...

    I need this in C++. This is all one question Program 2: Linked List Class For this problem, let us take the linked list we wrote in a functional manner in a previous assignment and convert it into a Linked List class. For extra practice with pointers we'll expand its functionality and make it a doubly linked list with the ability to traverse in both directions. Since the list is doubly linked, each node will have the following structure: struct...

  • 1.Implement recursive and iterative delete functions for linked lists. Node declaration of the linked list is...

    1.Implement recursive and iterative delete functions for linked lists. Node declaration of the linked list is given below. struct node { int info; struct node *next; }; typedef struct node node; You can assume that all the nodes in the linked list are distinct and each node appears in the list at most once. Prototype of the functions are given below. node *delete(node *head, int k) node *recursivedelete(node *head, int k) • delete deletes the node with info k from...

  • c++ only Program 2: Linked List Class For this problem, let us take the linked list...

    c++ only Program 2: Linked List Class For this problem, let us take the linked list we wrote in a functional manner in a previous assignment and convert it into a Linked List class. For extra practice with pointers we’ll expand its functionality and make it a doubly linked list with the ability to traverse in both directions.    Since the list is doubly linked, each node will have the following structure: struct Node { int number; Node * nextNode;...

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

  • 1. Create a node class and linked list class. 2. Add the following function from lecture...

    1. Create a node class and linked list class. 2. Add the following function from lecture to your linked list class addFirst(e) addLast (e) add(index, e) removeFirst removeLast remove(index) 3. Add the following methods to the linked list class. a. # Return the size of the list def getSize(self): b. # Clear the list */ def clear(self): c. # Return the element from this list at the specified index def get(self, index):

  • Design and implement your own linked list class to hold a sorted list of integers in...

    Design and implement your own linked list class to hold a sorted list of integers in ascending order. The class should have member function for inserting an item in the list, deleting an item from the list, and searching the list for an item. Note: the search function should return the position of the item in the list (first item at position 0) and -1 if not found. In addition, it should member functions to display the list, check if...

  • This is a c++ class utilizing class templates and linked lists. I need to implement the...

    This is a c++ class utilizing class templates and linked lists. I need to implement the following member function(s) to List.cpp. Node.hpp/cpp should be fine but if you feel like there needs to be a change for compilation or testing, feel free to do so but make sure to comment on why it was done. /** @pre assumes position is valid, if position is > item_count_ it returns an empty List, also assumes that operators <= and >= are defined...

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