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

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

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

  • Python question. i have to start from an empty linked list, using the method addNodeEnd() to...

    Python question. i have to start from an empty linked list, using the method addNodeEnd() to add the nodes containing the values (3*i+5)%17, where i is from 0 to 10. Then print the values of all the nodes in this linked list to the screen. This is the code that i created right here and i need help checking if i made any mistakes thanks! The code is below: class Node: def __init__(self, data): self.data = data self.next = None...

  • Linked List in Java The data node should be modeled somewhat like this: class node{ int...

    Linked List in Java The data node should be modeled somewhat like this: class node{ int node iNum; node next; } Write a program that creates a linked list and loads it with the numbers 0 to 9. Start with an empty list and then use a "for loop" to fill it. Create a linked list class, a node class, etc. Routines like makeNode and findTail should be methods in the linked list class. Create a showList function to display...

  • Linked Lists: Suppose you have a doubly linked list with both head and tail pointers, that...

    Linked Lists: Suppose you have a doubly linked list with both head and tail pointers, that stores integers. Implement a non-recursive function that takes a linked list, searches for an integer, and removes the node with the first occurrence of that integer and also removes the node directly after it regardless of value . This function will return to address of the resulting list. You ca n assume that there will be at least three nodes, and if there is...

  • Design and implement your own linked list class to hold a sorted list of integers in ascending order. The class should h...

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

  • python Programming assignment: Let's think about doubly-linked lists. Define a class ListNode2, with three attributes: item,...

    python Programming assignment: Let's think about doubly-linked lists. Define a class ListNode2, with three attributes: item, left, and rightL. Left link points to the previous node in the list, right link points to the next node in the list. You can also add the display method to this class (like we did it in class for the ListNode class). Then test your class. For example, create a linked list of 5 values: 34,1, 23, 7, and 10. Display it. Then...

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