C++
1. Please use attached script for your reference to create the node
structure and a linked list class, say LinkedList, that has the
following basic member methods:
constructor, destructor//IMPORTANT, display(),
add_node().
2. Please implement the following additional member methods:
Please feel free to change T with any data type
you'd like to use for your node stricture's data type.
-- addFirst(T data) // Adds an node with data at the beginning of
the list
-- pop() // Removes the first node of the list. Note: Don't forget
to delete/re-allocate the dynamic memory
-- contains(T data) // return true or false if a node contains the
data exists in the list
-- set(int index, T data) // update the data of the index'th node
in the list with data.
-- size() // returns the number of nodes in the list
-- merge(LinkedList linkedlist) //merge this object with linkedlist
object. In other words, add all nodes in linkedlist to this
object.
Extra Credits:
-- insert(int index, T data) // Adds a node after the node with the
'index' number. Note, you will need to check if the node with the
index number exists.
-- remove( int index) // Removes a node after the node with the
'index' number. Note, you will need to check if the node with the
index number exists. Note: Don't forget to delete/re-allocate the
dynamic memory
3. in main() // write some test code
#include <iostream>
using namespace std;
struct Node
{
int data;
Node *link;
};
class linked_list
{
private:
Node *head,*current;
public:
//constructor
linked_list()
{
head = NULL;//the head
pointer
current = NULL;//acts as
the tail of the list
}
//destructor - IMPORTANT
~linked_list() {
current = head;
while( current != NULL ) {//the loop stops when both current and
head are NULL
head = head->link;
delete current;
current = head;
}
}
void add_node(int n)// to add a node at the end
of the list
{
Node *tmp = new
Node;
tmp->data = n;
tmp->link =
NULL;
if(head == NULL)
{
head = tmp;
current = tmp;
}
else
{
current->link = tmp;
current = current->link;
}
}
void display() // to display all nodes in the
list
{
Node *tmp;
tmp = head;
while (tmp !=
NULL)
{
cout << tmp->data << endl;
tmp = tmp->link;
}
}
};
int main()
{
linked_list a;
a.add_node(1);
a.add_node(2);
a.display();
return 0;
}
#include <iostream>
using namespace std;
struct Node
{
int data;
Node *link;
};
class linked_list
{
private:
Node *head,*current;
public:
//constructor
linked_list()
{
head = NULL;//the head pointer
current = NULL;//acts as the tail of the list
}
//destructor - IMPORTANT
~linked_list() {
current = head;
while( current != NULL )
{//the loop stops when both current and head are NULL
head = head->link;
delete current;
current = head;
}
}
void add_node(int n)// to add a node at the end of the list
{
Node *tmp = new Node;
tmp->data = n;
tmp->link = NULL;
if(head == NULL)
{
head = tmp;
current = tmp;
}
else
{
current->link = tmp;
current = current->link;
}
}
void display() // to display all nodes in the list
{
Node *tmp;
tmp = head;
while (tmp != NULL)
{
cout << tmp->data << endl;
tmp = tmp->link;
}
}
void addFirst(int n)// to add a node at the start of the list
{
Node *tmp = new Node;
tmp->data = n;
tmp->link = head;
head = tmp;
if(head->link == NULL)
current = head;
}
void pop()
{
if(head != NULL)
{
Node *tmp = head;
head = head->link;
delete(tmp);
if(head == NULL)
current = NULL;
}
}
bool contains(int data)
{
Node *curr = head;
while(curr != NULL)
{
if(curr->data == data)
return true;
curr = curr->link;
}
return false;
}
void set(int index, int data)
{
if(index >=0 && index < size())
{
if(index == 0)
head->data = data;
else
{
Node *curr = head;
int i=0;
while(i < index)
{
curr = curr->link;
i++;
}
curr->data = data;
}
}
}
int size()
{
Node *curr = head;
int count=0;
while(curr != NULL)
{
count++;
curr =curr->link;
}
return count;
}
void merge(const linked_list &linkedlist)
{
Node *curr = linkedlist.head;
while(curr != NULL)
{
add_node(curr->data);
curr = curr->link;
}
}
void insert(int index, int data)
{
if(index >=0 && index <= size())
{
if(index == 0)
addFirst(data);
else if(index == size())
add_node(data);
else
{
int i=0;
Node *curr = head;
Node *prev = NULL;
Node *tmp = new Node;
tmp->data = data;
tmp->link = NULL;
while(i < index )
{
prev = curr;
curr = curr->link;
i++;
}
prev->link = tmp;
tmp->link = curr;
}
}
}
void remove( int index)
{
if(index >=0 && index < size())
{
if(index == 0)
{
pop();
}else
{
int i=0;
Node *curr = head;
while(i < index-1)
{
curr = curr->link;
i++;
}
Node *tmp = curr->link;
curr->link = tmp->link;
if(curr->link == NULL)
current = curr;
delete(tmp);
}
}
}
};
int main() {
linked_list a;
a.add_node(1);
a.add_node(2);
a.addFirst(12);
a.add_node(5);
cout<<"List : "<<endl;
a.display();
cout<<"Size : "<<a.size()<<endl;
if(a.contains(3))
cout<<" list contains 3 "<<endl;
else
cout<<" list doesn't contains 3 "<<endl;
if(a.contains(2))
cout<<" list contains 2 "<<endl;
else
cout<<" list doesn't contains 2 "<<endl;
a.set(1,4);
cout<<"List : "<<endl;
a.display();
a.insert(2,7);
cout<<"List : "<<endl;
a.display();
a.pop();
cout<<"List : "<<endl;
a.display();
a.remove(2);
cout<<"List : "<<endl;
a.display();
linked_list b;
b.addFirst(34);
b.addFirst(28);
b.add_node(5);
cout<<"List A : "<<endl;
a.display();
cout<<"List B : "<<endl;
b.display();
cout<<"B merged into A : "<<endl;
a.merge(b);
a.display();
return 0;
}
//end of program
Output:

C++ 1. Please use attached script for your reference to create the node structure and a...
C++ 1. Please use attached script for your reference to create the node structure and a linked list class, say LinkedList, that has the following basic member methods: constructor, destructor//IMPORTANT, display(), add_node(). 2. Please implement the following additional member methods: Please feel free to change T with any data type you'd like to use for your node stricture's data type. -- addFirst(T data) // Adds an node with data at the beginning of the list -- pop() //...
c++ modify the attached unsorted linked list class into a sorted linked list class #include <iostream> using namespace std; template<class T> struct Node { T data;//data field Node * next;//link field Node(T data) { this->data = data; } }; template<class T> class linked_list{ private: Node<T> *head,*current; public: linked_list(){//constructor, empty linked list head = NULL; current = NULL; } ~linked_list(){ current = head; while(current != NULL) { ...
In C++, change this code to a circular linked list #include <iostream> using namespace std; struct node { string data; node *next; }; class linked_list { private: node *head,*tail; public: linked_list() { head = NULL; tail = NULL; } void add_node(string n) { node *tmp = new node; tmp->data = n; tmp->next = NULL; if(head == NULL) { head = tmp; tail = tmp; } else { tail->next = tmp; tail = tail->next; } } void display() { while (tmp...
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,...
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);...
RE-POSTED - Computer Science staff, I need this question
answered. It will determine a pass or a fail. Its very imortant
that this and the other questions posted are answered 100% Thank
you.
13 - Template C++ Advance
Please Only answer assignment if your code is 100%. When
pasteing your code use text so I may copy and paste into visual
studio. The code and questions must be answered 100% correct and
works. Thank you.
Programming Assignment
Convert the int...
Your task is to complete the following function/functions: 1. Given a position in the linked list, delete the node at that position.(Silver problem - Mandatory ) 2. Print the sum of all negative elements in the linked list.(Gold problem) If you want, you can refer to the the previous recitation manual (which was on Linked Lists) to implement node deletion. #include <iostream> using namespace std; //----------- Define Node --------------------------------------------------- struct Node{ int key; Node *next; }; //----------- Define Linked List...
Consider a Linked List program with the following class: typedef int datatype; struct node { datatype data; node *tail; }; class LinkedList{ private: node *head; node *current;public: //constructors LinkedList(); LinkedList(int i); //destructor ~LinkedList(); bool start(); //sets list postion to header bool nextNode(); //increments to next node in list int getCurrent(); //returns data from current node void insertNode(int i); //inserts node after current node //then sets current node to new node bool deleteNode();//deletes currentnode void deleteAll(); //deletes all nodes };...
Using the provided Linked List template, add the following recursive functions and demonstrate them in a separate cpp file. Write a recursive function to print the list in order. Write a recursive function to print the list in reverse order. Write a recursive function to print every other node in the list in order. Write a recursive function to return the number of nodes in the list. Write a Boolean function that implements the recursive version of sequential search. THIS...
Problem 2: based on java.util.LinkedList class, create MyStack class that should have the methods: push(), pop(), peek(), size(), isEmpty(). my linkedlist class is: public class Lab8_problem1 { private Node head, tail; private int size; public Lab8_problem1() { this.head = null; this.tail = null; this.size = 0; } //Insert a node at specifc Location public void insertAt(int value, int index) { Node newNode = new Node(); newNode.data = value; if(head == null){ head = newNode; tail = newNode; head.next...