Question

In: Computer Science

// C++ Doubly linked list class clinknode{ friend class DList; public:     clinknode(){next = prev=0;data =0;}...

// C++ Doubly linked list

class clinknode{

friend class DList;

public:

    clinknode(){next = prev=0;data =0;}

protected:

    clinknode *next;

    clinknode *prev;

    int data;

}

class DList{

public:

DList(){first = last = 0;}

void insertFront(int key);

bool found(int key){ return search(key)!=0;}

void deleteAll(int key);

// other list functions...

private:

clinknode *search(int);

clinknode *first;

clinknode *last;

}

(20 points) Write the code for the deleteAll (int key) member function. It takes a key value and deletes all of the data in the list with that value. Make sure to preserve the integrity of the list. If no member with that value is in the list, this function should do nothing.

Solutions

Expert Solution

#include <iostream>

using namespace std;

class clinknode{

friend class DList;

public:

clinknode(){next = prev=0;data =0;}
clinknode(int value){next = prev=0;data=value;}
protected:

clinknode *next;

clinknode *prev;

int data;

};

class DList{

public:

DList(){first = last = 0;}

//implemented for testing purpose

void insertFront(int key)
{
clinknode *node = new clinknode(key);
if( first == 0)
{
first = node;
}
else
{
node->next = first;
first->prev = node;
first = node;
}   
  
}

bool found(int key){ return search(key)!=0;}

void deleteAll(int key)
{
clinknode *temp;
clinknode *prev = NULL;
clinknode *next;

for (temp = first; temp != NULL; temp = next) {
next = temp->next;

if (temp->data != key) {
prev = temp;
continue;
}

if (prev != NULL)
prev->next = next;
else
first = next;

delete temp;
}
}

//added this function for testing purpose

void display()
{
clinknode *a;
cout << "Values in list" << endl;
a=first;
while(a) {
cout <<a->data
<<endl;
a=a->next;
}
}
// other list functions...

private:

clinknode *search(int);

clinknode *first;

clinknode *last;

};


int main()
{
DList lt;
  
lt.insertFront(10);
lt.insertFront(20);
lt.insertFront(10);
lt.insertFront(5);
lt.insertFront(10);
lt.display();
lt.deleteAll(10);
lt.display();
lt.deleteAll(30);
lt.display();
return 0;
}

output:

Values in list                                                                                                                   

10                                                                                                                               

5                                                                                                                                

10                                                                                                                               

20                                                                                                                               

10                                                                                                                               

Values in list                                                                                                                   

5                                                                                                                                

20                                                                                                                               

Values in list                                                                                                                   

5                                                                                                                                

20


Related Solutions

Consider the following definition of a doubly linked-list: class LinkedList{ public: LinkedList():head(0), tail(0){} ~LinkedList(); void reverse();...
Consider the following definition of a doubly linked-list: class LinkedList{ public: LinkedList():head(0), tail(0){} ~LinkedList(); void reverse(); //reverses the order of elements in the linked list void insert(int value); private: struct Node{ int data; Node* next; Node* prev; }; Node* head; Node* tail; //Add your helper function here that recursively reverses the order of elements in the linked list }; Write the declaration of a helper function in the class provided above that recursively reverses the order of elements in the...
Write in C++: create a Doubly Linked List class that holds a struct with an integer...
Write in C++: create a Doubly Linked List class that holds a struct with an integer and a string. It must have append, insert, remove, find, and clear.
Develop a C++ "doubly" linked list class of your own that can hold a series of...
Develop a C++ "doubly" linked list class of your own that can hold a series of signed shorts Develop the following functionality: Develop a linked list node struct/class You can use it as a subclass like in the book (Class contained inside a class) You can use it as its own separate class Your choice Maintain a private pointer to a node class pointer (head) Constructor Initialize head pointer to null Destructor Make sure to properly delete every node in...
Using doubly linked list in c++ with class constructor: DNode(){    song = Song();    prev...
Using doubly linked list in c++ with class constructor: DNode(){    song = Song();    prev = NULL;    next = NULL; } DNode(string s, string a, int lenmin, int lensec){    song = Song(s,a,lenmin,lensec);    prev = NULL;    next = NULL; } for each node. Write the method: void moveUp(string t); This method moves a song up one in the playlist. For instance, if you had the following: Punching in a Dream, The Naked And Famous................3:58 Harder To...
so the assigment is is a data strucutre using c++ to make a doubly linked list...
so the assigment is is a data strucutre using c++ to make a doubly linked list that will be able to do math mostly addition and multiplication, subratction and division is extra and would be nice. so the program is going to to open files and read them via a argumentmanager.h in a linux server try not to worry to much about this part just getting the program to work. i was able to complete part of the given code...
Can you make this singular linked list to doubly linked list Create a Doubly Linked List....
Can you make this singular linked list to doubly linked list Create a Doubly Linked List. Use this to create a Sorted Linked List, Use this to create a prioritized list by use. Bring to front those links recently queried. -----link.h------ #ifndef LINK_H #define LINK_H struct Link{ int data; Link *lnkNxt; }; #endif /* LINK_H */ ----main.cpp---- //System Level Libraries #include <iostream> //I/O Library using namespace std; //Libraries compiled under std #include"Link.h" //Global Constants - Science/Math Related //Conversions, Higher Dimensions...
A circular doubly-linked list .(a) In a circular doubly-linked list, there is no front or end;...
A circular doubly-linked list .(a) In a circular doubly-linked list, there is no front or end; the nodes form a full circle. Instead of keeping track of the node at the front, we keep track of a current node instead. Write a class for a circular doubly-linked list using the attached Job class as your node objects. It should have: • A private instance variable for the current node • A getCurrent() method that returns a reference to the current...
Author code /** * LinkedList class implements a doubly-linked list. */ public class MyLinkedList<AnyType> implements Iterable<AnyType>...
Author code /** * LinkedList class implements a doubly-linked list. */ public class MyLinkedList<AnyType> implements Iterable<AnyType> { /** * Construct an empty LinkedList. */ public MyLinkedList( ) { doClear( ); } private void clear( ) { doClear( ); } /** * Change the size of this collection to zero. */ public void doClear( ) { beginMarker = new Node<>( null, null, null ); endMarker = new Node<>( null, beginMarker, null ); beginMarker.next = endMarker; theSize = 0; modCount++; } /**...
Python class DLLNode: """ Class representing a node in the doubly linked list implemented below. """...
Python class DLLNode: """ Class representing a node in the doubly linked list implemented below. """ def __init__(self, value, next=None, prev=None): """ Constructor @attribute value: the value to give this node @attribute next: the next node for this node @attribute prev: the previous node for this node """ self.__next = next self.__prev = prev self.__value = value def __repr__(self): return str(self.__value) def __str__(self): return str(self.__value) def get_value(self): """ Getter for value :return: the value of the node """ return self.__value...
Using the singly linked list code as a base, create a class that implements a doubly...
Using the singly linked list code as a base, create a class that implements a doubly linked list. A doubly linked list has a Previous link so you can move backwards in the list. Be sure the class is a template class so the user can create a list with any data type. Be sure to test all the member functions in your test program. c++
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT