Question

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() // 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;
}


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

Please follow the below code carefully so as to understand it,

Source 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 taile pointer
}
//destructor
~linked_list()
{
current=head;
while(current!=NULL)
{
head=head->link;
delete current;
current=head;
}
}
void addFirst(int data)//to add the node at the front of the list
{
Node* tem=new Node;//creating a new node
tem->data=data; //assigning the data of the node to the given data
tem->link=NULL;
if(head==NULL)//if linked list is empty
{
//as only single node will be present both the head and current will point to that node
head=tem;
current=tem;
}
else
{
//as the nodemust be added at the front of the linked list its link must point
//to the present head and we change the present head as the new node
tem->link=head;
head=tem;
}
}
void pop()//removes the first node of the list
{
Node *tem=head;
head=head->link;//as we delete the first node we move the head to its link
delete tem; //this deletes the node to be deleted i.e. the initial head.

}
bool contains(int data)//return true or false if a node contains the data exists in the list
{
Node *tem=head;
while(tem!=NULL)
{
if(tem->data==data)
{
return true;
}
tem=tem->link;
}
return false;

}
Node** head_node()//to return the head node of the currrent linked list object
{
return &head;//returning address of head
}
void set(int index,int data)//update the data of the index'th node in the list with data
{
//here we are assuming index to start with 1.
Node *tem=head;
for(int i=1;i<index;i++)
{
tem=tem->link;//traversing the linked list until we reach the node at the required index
}
tem->data=data;//finally updating the data of the node
}
void merge(linked_list l)//merge this object with the linked list object
{
Node** head1=l.head_node();
while(*head1!=NULL)
{
add_node((*head1)->data);
*head1=(*head1)->link;
}
  
  
  
}
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 the nodes in the list
{
Node *tmp;
tmp=head;
while(tmp!=NULL)
{
cout<<tmp->data<<endl;
tmp=tmp->link;
}
}
int size()//returns the number of nodes in the linked list
{
Node* tmp=head;//initializing tmp to point to head
int count=0;//to keep track of number of nodes in the linked list
while(tmp!=NULL)
{
tmp=tmp->link;
count++;
}
return count;
}
void insert(int index,int data)//Adds a node after theh node with the index number
{

//as if linked list contains 4 ellements and
//index is specified as 5 we cannot add after that particular
//place as max value for index is count-1
int cnt=size();

if(index>cnt)
{
cout<<"index does not exist"<<endl;
return;
}
else if(index>=0){
//checking whether index>=0
if(index==0)//to add at the first position of the linked list
{
addFirst(data);//this function creates a new node with the given data and adds it at the front //of the linked list
}
else{
int id=index;
Node* tem=head;
while(index-1!=0)
{
  
tem=tem->link;//traversing to the ith index
index--;
}
  
Node* p=tem->link;
Node* newnode=new Node;

newnode->data=data;
newnode->link=p;//this node points the node which was initially pointed by the node at index
tem->link=newnode;//the index'th node points the current node

if(id==cnt)//as it gets added at the end of the linked list
{   
current=newnode;}
  
return;
}

}
}
void remove(int index)
{
Node* tem=head;
if(index>size()-1)//as there are only n nodes we cannot have a node after nth node to delete
//so we can give a maximum of n-1 to index where n is no of nodes in the linked list;
{
cout<<"index does not exist"<<endl;
return;

}
else if(index>=0)//checking whether index>=0
{
if(index==0)
{
pop();//if the firstnode to be popped we need to give index as 0
//hence we call pop as it ops the first node
}
else
{


for(int i=1;i<index;i++)
{
tem=tem->link;//traversing to the ith index;
}
//to del the index+1th index
if(index==size()-1)
current=tem;//as the indexth node becomes the last node in this case
Node *delnode=tem->link;
tem->link=(tem->link)->link;//to make the indexth node point to
//the node that is pointed by (i+1)th node
delete delnode;//finally deleting the node after the indexth node
}
}
}


};
int main()
{
linked_list a,b;
a.add_node(1);//adding element 1
  
a.add_node(2);//adding element 2
a.add_node(3);//adding element 3
b.add_node(4);//adding elemet 4
b.add_node(5);//adding element 5
a.display();//before merge
a.merge(b);
a.display();//after merge
a.insert(1,10);//inserting node with value 10 after 1rst index node
a.remove(4);//removing node after 4th node
a.display();
cout<<a.size();
cout<<a.contains(11);
cout<<a.contains(10);
//you can try variety of operations...


}

Please be free to ask in case of any queries,Thank you.

Add a comment
Know the answer?
Add Answer to:
C++ 1. Please use attached script for your reference to create the node structure and a...
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++ 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>...

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

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

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

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

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

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

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

    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(),...

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

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