Question

In: Computer Science

Assume that a singly linked list is implemented with a header node, but no tail node,...

Assume that a singly linked list is implemented with a header node, but no tail node, and that it maintains only a pointer to the header node. Write a class in C++ that includes methods to
a. return the size of the linked list
b. print the linked list
c. test if a value x is contained in the linked list
d. add a value x if it is not already contained in the linked list
e. remove a value x if it is contained in the linked list

Solutions

Expert Solution

CODE IN C++ :

#include<iostream>
using namespace std;
class Node{
public:
int data;
Node*next;
Node(int data)
{
this->data=data;
this->next=NULL;
}

};
int size_linkedlist(Node*head) //function to find the size of the given linked list
{
if(head==NULL)
{
return 0;
}
Node*temp=head;
int size=0;
while(temp->next!=NULL)
{
temp=temp->next;
size++;
}
return size+1;
}
void print(Node* head) //function to print the linked list
{
if(head==NULL)
{
cout<<-1;
}else{
Node*temp=head;
while(temp->next!=NULL)
{
cout<<temp->data<<" " ;
temp=temp->next;
}
cout<<temp->data;
}
}
bool test(Node*head,int x)//function to test if a number is present in the linked list or not
{
Node*temp=head;
while(temp->next!=NULL)
{
if(temp->data==x)
{
return true;
}
}
return false;
}
void add(Node *head,int x) //function to add a node to the linked list
{
Node*temp=head;
while(temp->next!=NULL)
{
if(temp->data==x)
{
cout<<"Number is already there";
}else
{
temp=temp->next;
}
}
Node* addnode=new Node(x);
temp->next=addnode;
return;


}
void remove(Node*head,int x) //function to remove a node from a linked list
{
Node*temp=head;
while(temp->next!=NULL)
{
if(temp->data==x)
{
Node*temp2=head;
while(temp2->next->data!=x)
{
temp2=temp2->next;
}
temp2->next=temp->next;
return;

}else{
temp = temp->next;
}
}
}

int main()
{
Node*head=new Node(1);
Node*second=new Node(2);
Node*third=new Node(3);
Node*fourth=new Node(4);
head->next=second;
second->next=third;
third->next=fourth;
int size=size_linkedlist(head);
cout<<"The size of linked list is : "<<size<<endl;
print(head);
cout<<endl;
remove(head,3);
print(head);
cout<<endl;
add(head,5);
print(head);

}

OUTPUT SNIPPET:


Related Solutions

Assume that a singly linked list is implemented with a header node, but no tail node,...
Assume that a singly linked list is implemented with a header node, but no tail node, and that it maintains only a pointer to the header node. Write a class that includes methods to a. return the size of the linked list b. print the linked list c. test if a value x is contained in the linked list d. add a value x if it is not already contained in the linked list e. remove a value x if...
8. Assume you have a singly linked list with no tail pointer. Implement removeTail(). Raise an...
8. Assume you have a singly linked list with no tail pointer. Implement removeTail(). Raise an exception of the method is called on an empty list. template<typename Object> class LinkedList { private: class Node { Object data; Node* next; }; Node *head; public: LinkedList() : head(nullptr) {} Object removeTail(Object data); }; 9. What are iterators? What purpose do they serve? 10. What does it mean to invalidate an iterator? 11. Explain the diļ¬€erence between separate chaining and open addressing in...
I've provided a Node class that implements a node of a simple singly-linked list (with .value...
I've provided a Node class that implements a node of a simple singly-linked list (with .value and .next fields), and an empty LinkedList class. Your task is to implement LinkedList.sort(l), where given the node l as the head of a singly-linked list, LinkedList.sort(l) sorts the nodes in the list into ascending order according to the values in the .value field of each node. Your implementation should do an in-place update of the list. It is ok to use a simple...
Create a program that implements a singly linked list of Students. Each node must contain the...
Create a program that implements a singly linked list of Students. Each node must contain the following variables: Student_Name Student_ID In main(): Create the following list using addHead(). The list must be in the order shown below. Student_ID Student_Name 00235 Mohammad 00662 Ahmed 00999 Ali 00171 Fahad Print the complete list using toString() method. Create another list using AddTail(). The list must be in the order shown below. Print the complete list using toString() method. Delete head note from both...
Create a program that implements a singly linked list of Students. Each node must contain the...
Create a program that implements a singly linked list of Students. Each node must contain the following variables: Student_Name Student_ID In main(): Create the following list using addHead(). The list must be in the order shown below. Student_ID Student_Name 00235 Mohammad 00662 Ahmed 00999 Ali 00171 Fahad Print the complete list using toString() method. Create another list using AddTail(). The list must be in the order shown below. Student_ID Student_Name 00236 Salman 00663 Suliman 00998 Abdulrahman Print the complete list...
In Python, I've created a Node class for implementing a singly linked list. My Code: class...
In Python, I've created a Node class for implementing a singly linked list. My Code: class Node: def __init__(self,initdata): self.data = initdata self.next = None def getData(self): return self.data def getNext(self): return self.next def setData(self,newdata): self.data = newdata def setNext(self,newnext): self.next = newnext class SinglyLinkedList: def __init__(self): self.head = None def add(self,key): addkey = Node(key) addkey.setNext(self.head) self.head = addkey Now the question is: Create an append method that is O(1) by modifying the constructor of the SinglyLinkedList class by adding...
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...
I was supposed to conver a singly linked list to a doubly linked list and everytime...
I was supposed to conver a singly linked list to a doubly linked list and everytime I run my program the output prints a bunch of random numbers constantly until I close the console. Here is the code. #include <stdio.h> #include <stdlib.h> #include <string.h> #include <stdbool.h> struct node { int data; struct node *next; struct node *prev; }; //this always points to first link struct node *head = NULL; //this always points to last link struct node *tail = NULL;...
You are given a singly linked list. Write a function to find if the linked list...
You are given a singly linked list. Write a function to find if the linked list contains a cycle or not. A linked list may contain a cycle anywhere. A cycle means that some nodes are connected in the linked list. It doesn't necessarily mean that all nodes in the linked list have to be connected in a cycle starting and ending at the head. You may want to examine Floyd's Cycle Detection algorithm. /*This function returns true if given...
Java program to implement circular linked list. public class CircularLinkedList { private Node tail; private int...
Java program to implement circular linked list. public class CircularLinkedList { private Node tail; private int size; public CircularLinkedList() { tail= null; size = 0; } public int size(){ return size; } public boolean isEmpty() { return size==0; } //if list is not empty return the first element public E first() { if (isEmpty()) return null; //code here return 0; } //if list not empty return last element public E last() { if (isEmpty()) return null; return tail.getElement(); } /*...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT