Question

Exercise-1:15 points Develop a node class and a singly list class. The node class should have two state variables namely data
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code for the above problem :

#include<iostream>
using namespace std;
class Node
{
public:
int data;
Node *nextNode;
};
class SinglyList
{
private:
Node* head;
public:
SinglyList() // contructor to initialize head pointer to null
{ head=NULL; }

void StartInsert(int new_data)
{
Node* newNode;
newNode = new Node;
newNode->data = new_data;
newNode->nextNode = head;
head = newNode;
}
void MiddleInsert(int new_data)
{
if(head==NULL) // if no node then insert a new node
{
Node* newNode;
newNode = new Node;
newNode->data = new_data;
newNode->nextNode = NULL;
head = newNode;
}
else
{
int length=0;
Node *temp_pointer = head;
while(temp_pointer!=NULL) // calculating total length
{
length++;
temp_pointer = temp_pointer->nextNode;
}
int middle;
if(length%2==0) // calculating middle
middle = length/2;
else
middle = (length+1)/2;

temp_pointer = head;
while(middle>1) // Traversing while we reach the middle
{
middle--;
temp_pointer = temp_pointer->nextNode;
}
Node* newNode; // Inserting new node at middle
newNode = new Node;
newNode->data = new_data;
newNode->nextNode = temp_pointer->nextNode;
temp_pointer->nextNode = newNode;

}

}

void EndInsert(int new_data)
{
Node* newNode;
newNode = new Node;
newNode->data = new_data;
newNode->nextNode = NULL; // since inserted at last, next node will always be NULL
if(head==NULL)
head = newNode;
else
{
Node *temp_pointer = head;
while(temp_pointer->nextNode!=NULL)
temp_pointer = temp_pointer->nextNode;
temp_pointer->nextNode = newNode;
}

}
void Delete(int del_position)
{
if(head==NULL)
return;
Node *temp_pointer = head;
if(del_position ==1)
{
head = temp_pointer->nextNode;
}
else
{
for(int i=1;temp_pointer!=NULL && i<del_position-1;i++)
temp_pointer = temp_pointer->nextNode;
}
if(temp_pointer==NULL || temp_pointer->nextNode==NULL) // If user enter position more than number of nodes
{ cout<<"Invalid position\n"; return; }

Node *next_pointer = temp_pointer->nextNode->nextNode;
temp_pointer->nextNode = next_pointer;
}

void Reverse() // function to reverse a linked list
{
Node *current_pos = head;
Node *previous_pos = NULL;
Node *next_pos = NULL;
while(current_pos!=NULL) //traversing till end and modifying the nextNode pointers to point at previous node
{
next_pos = current_pos->nextNode;
current_pos->nextNode = previous_pos; // pointing current's nextNode to previous ie. reversing
previous_pos = current_pos; // previous_pos keeps moving forward
current_pos = next_pos; // current_pos also keeps moving forward and is ahead by previous by one.
}
                       head = previous_pos;
}

void Traverse() // to display all the nodes
{
Node *temp_pointer = head;
while(temp_pointer!=NULL) // printer till the pointer reaches null
{
cout<<temp_pointer->data<<" ";
temp_pointer = temp_pointer->nextNode;
}
cout<<endl<<endl;
}
};

int main()
{
SinglyList obj,obj1;
int number;
for(int i=1;i<=5;i++) //Inserting 5 nodes using using for loop and StartInsert function
obj.StartInsert(i);
cout<<"Displaying Linked List made using Start Insert(Inserted elements 1-5 will be printed in reverse order as inserted) : \n" ;
obj.Traverse();

cout<<"Insert a number to be inserted at middle : ";
cin>>number;
obj.MiddleInsert(number);

cout<<"After inserting element at middle, the new Linked List is as follows : \n";
obj.Traverse();

for(int i=5;i<=10;i++) //Inserting 5 nodes using using for loop and EndInsert function
obj1.EndInsert(i);
cout<<"Displaying new Linked List made using End Insert(Inserted elements 5-10 will be printed in order as inserted ) : \n" ;
obj1.Traverse();

int position;
cout<<"Enter the position(starting from 1) of node to be deleted : ";
cin>>position;
obj1.Delete(position);
obj1.Traverse();

cout<<"After reversing the new Linked List , the list looks as follows : \n";
obj1.Reverse();
obj1.Traverse();
}
  

INPUT :

1. For StartInsert method :- Used for loop to insert element 1,2,3,4,5

2. For MiddleInsert method :- Used previous list and user inputs a number.

3. For EndInsert method :- Used for loop to insert element 5,6,7,8,9,10

4. For Delete method :- User inputs a position to be deleted(Position start from 1)

5. Reverse and Traverse method : No inputs required from user.

Following screenshot shows all inputs and functions used :

SinglyList obj,objl: int number: for (int 1=l ; i<=5 i++) //Inserting 5 nodes using using for loop and StartInsert function o

OUTPUT :

1. Start Insert : Linked list with data 1,2,3,4,5 is displayed. Since it was inserted using StartInsert method, the result would be in reverse order.

2. Middle Insert: User enters 6 and 6 gets inserted at 4th position(middle)

3. End Insert : Linked list with data 5,6,7,8,9,10 is displayed. Since it was inserted using EndInsert method, the result is displayed in same order as inserted.

4. Delete : User enters 4. So 4th position ie. 8 gets deleted from the list.

5. Reverse : The remaining list gets reversed.

$./2.out Displaying Linked List made using Start Insert (Inserted elements 1-5 will be printed in reverse order as inserted)

Hope that helped.

Thanks.

Add a comment
Know the answer?
Add Answer to:
Exercise-1:15 points Develop a node class and a singly list class. The node class should have two...
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
  • Exercise-2: 15 points Develop a node class and a doubly list class. The node class should have tw...

    Exercise-2: 15 points Develop a node class and a doubly list class. The node class should have two state variables namely data and nextNode. The doubly list class should contain the following methods: Middlelnsert- insert a node somewhere in the middle of the list Startinsert-insert a node at start of the Linked list Endinsert- insert a node at the end of the Linked list Delete-delete a node Traverse-prints all the node's data Reverse-reverses the linked list . . Note: Choose...

  • Build a singly linked list with 10 noes. The data are "This is my first project...

    Build a singly linked list with 10 noes. The data are "This is my first project in Data structure and algorithm". First node is "This" second "is" and so on. Build insert function, insert "the " before "Data" and delete function, delete "my" before "first". Search "in" and output it location and then append "C++" and output the linked list again. In writing this program use menu which includes ”1. Create; 2. Insert; 3. Delete; 4. Append; 5. Search; 6....

  • Write a Python function to implement the quick sort algorithm over a singly linked list. The...

    Write a Python function to implement the quick sort algorithm over a singly linked list. The input of your function should be a reference pointing to the first node of a linked list, and the output of your function should also be a reference to the first node of a linked list, in which the data have been sorted into the ascending order. (You may use the LinkedQueue class we introduced in the lecture directly in your program.)

  • BELOW IS THE CODE I ALREADY HAVE Linked List - Delete Modify Lab 5 and include...

    BELOW IS THE CODE I ALREADY HAVE Linked List - Delete Modify Lab 5 and include the method to delete a node from the Linked List. In summary: 1) Add the method Delete 2) Method call to delete, with a number that IS in the list 3) Method call to delete, with a number that is NOT in the list - Be sure to include comments - Use meaningful identifier names (constants where appropriate) import java.io.*; 1/ Java program to...

  • [C++] Create three functions for a singly linked list: - Function 1: Insert a string into...

    [C++] Create three functions for a singly linked list: - Function 1: Insert a string into the linked list - Function 1: Insert a node after a given node (Node* curNodeptr, Node* newNodePtr) - Function 2: Delete the node passed to it by a pointer, it will take in the head and curPtr '(Node*, Node*)' struct Node{ string data; Node *next; };

  • linked list operation /*************************************************************************************** This function creates a new node with the information give as a...

    linked list operation /*************************************************************************************** This function creates a new node with the information give as a parameter and looks for the right place to insert it in order to keep the list organized ****************************************************************************************/ void insertNode(string first_name, string last_name, string phoneNumber) { ContactNode *newNode; ContactNode *nodePtr; ContactNode *previousNode = nullptr; newNode = new ContactNode; /***** assign new contact info to the new node here *****/ if (!head) // head points to nullptr meaning list is empty { head = newNode;...

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

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

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

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

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