Question

In: Computer Science

1- Function 1: to delete a node in the head of the list. 2- Function 2:...




1- Function 1: to delete a node in the head of the list.
2- Function 2: to delete a node in the end of the list.
3- Function 3: to delete a node in the middle of the list. Ask the user the value of the node to delete.
4- Test the three functions in the main() and display the new list after each delete.

#include <iostream>
using namespace std;
struct node {
int num;
node * nextptr;
}*head,*curnode;
node *newnode;
void addlist (int n);
void displayList(node *head);
void FindElement(int n);

void add_head();
void add_end ();
void add_before();
int main()
{
head = NULL;
curnode = NULL;
int n;
​cout <<"single Linked List : Add elements in a single linked list :"<< endl;
​cout<<"-----------------------------------------------------------"<< endl;​ ​
cout<<" Input the number of nodes : ";
cin>> n;
addlist ( n);
displayList(head);
FindElement(n);
add_head();
displayList(head);
add_end();
displayList(head);
add_before();
displayList(head);
return 0;
}
void addlist (int n)
{
int num, i;
if(n >= 1)
{// create the head then the other nodes
head = new node;
cout <<" Enter data for node 1 : ";
cin>> num;
head->num = num;
head->nextptr = NULL;
curnode = head;
for(i=2; i<=n; i++)
{ newnode = new node;
cout<<" Enter data for node "<< i << " :" ;
cin >>num;
newnode->num = num;
newnode->nextptr = NULL;​// next address of new node set as NULL
curnode->nextptr = newnode;​// previous node is linking with new node
curnode = newnode; ​​// previous node is advanced
} ​
}
}
  
void displayList(node *head)
{
node *cur;
int n = 1;
if(head == NULL)
{
cout<<" No data found in the List yet."<<endl;
}
else
{
cur = head;
cout<<"Data entered in the list are :" <<endl;
do {
cout<<" Data :" << n << " : " << cur->num <<endl;
cur = cur->nextptr;
n++;
}while(cur!= NULL);
}
}

void FindElement(int n)
{
node *curnode= NULL;
​int ctr=1;
​int FindElem;
​curnode=head;
​cout<<" Input the element you want to find : "<< endl;
cin>>FindElem;
​while(curnode!=NULL)
​{
​​if(curnode->num==FindElem)
​​​break;
​​else
​​​ctr++;
​​​curnode=curnode->nextptr;
​​​if (ctr==n+1)
​​​break;
​}
​if(ctr<=n)
​​cout<< " Element found at node "<< ctr<< endl;
​else
​​cout<<" This element does not exist in linked list."<< endl;
}

void add_head()
{
​int num, i;
newnode = new node;//start creation of the new node
cout <<" Enter data for node to add in the head : ";
cin>> num;
newnode->num = num;
newnode->nextptr = NULL; //End creation of the new node
​if(head == NULL) head=newnode;// if emplty list the new node will be the head
else{ ​newnode->nextptr= head;
​​head = newnode;
}
}

void add_end ()
{
​int num, i;
newnode = new node;//start creation of the new node
cout <<" Enter data for node to add in the End : ";
cin>> num;
newnode->num = num;
newnode->nextptr = NULL;//end creation of the new node
​if(head == NULL) head=newnode;// if emplty list the new node will be the head
else { // go to the end of the list
​​curnode=head;​
​​while(curnode->nextptr!=NULL)
{
curnode= curnode->nextptr;​
}
​​curnode-> nextptr= newnode;
}
}

void add_before()
{int num,m, find=0;
node *prevnode;
​newnode = new node;//start creation of the new node
cout <<" Enter data for node to add : ";
cin>> num;
newnode->num = num;
newnode->nextptr = NULL;//end creation of the new node
cout << "enter the number to add before it: ";
cin>> m;
if (head->num==m)// if we will add before head
{newnode->nextptr= head;
head=newnode;
}
else
{
curnode= head;
​while(curnode!=NULL)
​{​​if(curnode->num==m)
​​​{find=1;
​​​break;
​​​}
​​else
​​​{prevnode=curnode;
​​​curnode=curnode->nextptr;
​​​}
​}
​if(find==0)​cout<< " Element not found "<< endl;
​else
​{prevnode->nextptr=newnode;
​newnode->nextptr=curnode;
​}
​}​
}

Solutions

Expert Solution

CODE:

#include <iostream>

using namespace std;

struct node

{

    int num;

    node *nextptr;

} * head, *curnode;

node *newnode;

void addlist(int n);

void displayList(node *head);

void FindElement(int n);

void add_head();

void add_end();

void add_before();

void delete_head();

void delete_end();

void delete_value();

int main()

{

    head = NULL;

    curnode = NULL;

    int n;

    cout << "single Linked List : Add elements in a single linked list :" << endl;

    cout << "-----------------------------------------------------------" << endl;

    cout << " \n\nInput the number of nodes : ";

    cin >> n;

    addlist(n);

    displayList(head);

    FindElement(n);

    add_head();

    displayList(head);

    add_end();

    displayList(head);

    add_before();

    displayList(head);

    delete_head();

    displayList(head);

    delete_end();

    displayList(head);

    delete_value();

    displayList(head);

    return 0;

}

void addlist(int n)

{

    int num, i;

    if (n >= 1)

    { // create the head then the other nodes

        head = new node;

        cout << " Enter data for node 1 : ";

        cin >> num;

        head->num = num;

        head->nextptr = NULL;

        curnode = head;

        for (i = 2; i <= n; i++)

        {

            newnode = new node;

            cout << " Enter data for node " << i << " :";

            cin >> num;

            newnode->num = num;

            newnode->nextptr = NULL;

            // next address of new node set as NULL

            curnode->nextptr = newnode;

            // previous node is linking with new node

            curnode = newnode;

            // previous node is advanced

        }

    }

}

void displayList(node *head)

{

    cout << "\n\n";

    node *cur;

    int n = 1;

    if (head == NULL)

    {

        cout << " No data found in the List yet." << endl;

    }

    else

    {

        cur = head;

        cout << "Data entered in the list are :" << endl;

        do

        {

            cout << " Data :" << n << " :  " << cur->num << endl;

            cur = cur->nextptr;

            n++;

        } while (cur != NULL);

    }

}

void FindElement(int n)

{

    cout << "\n\n";

    node *curnode = NULL;

    int ctr = 1;

    int FindElem;

    curnode = head;

    cout << " Input the element you want to find : " << endl;

    cin >> FindElem;

    while (curnode != NULL)

    {

        if (curnode->num == FindElem)

            break;

        else

            ctr++;

        curnode = curnode->nextptr;

        if (ctr == n + 1)

            break;

    }

    if (ctr <= n)

        cout << " Element found at node " << ctr << endl;

    else

        cout << " This element does not exist in linked list." << endl;

}

void add_head()

{

    cout << "\n\n";

    int num, i;

    newnode = new node; //start creation of the new node

    cout << " Enter data for node to add in the head : ";

    cin >> num;

    newnode->num = num;

    newnode->nextptr = NULL; //End creation of the new node

    if (head == NULL)

        head = newnode; // if emplty list the new node will be the head

    else

    {

        newnode->nextptr = head;

        head = newnode;

    }

}

void add_end()

{

    cout << "\n\n";

    int num, i;

    newnode = new node; //start creation of the new node

    cout << " Enter data for node to add in the End : ";

    cin >> num;

    newnode->num = num;

    newnode->nextptr = NULL; //end creation of the new node

    if (head == NULL)

        head = newnode; // if emplty list the new node will be the head

    else

    { // go to the end of the list

        curnode = head;

        while (curnode->nextptr != NULL)

        {

            curnode = curnode->nextptr;

        }

        curnode->nextptr = newnode;

    }

}

void add_before()

{

    cout << "\n\n";

    int num, m, find = 0;

    node *prevnode;

    newnode = new node; //start creation of the new node

    cout << " Enter data for node to add : ";

    cin >> num;

    newnode->num = num;

    newnode->nextptr = NULL; //end creation of the new node

    cout << "enter the number to add before it: ";

    cin >> m;

    if (head->num == m) // if we will add before head

    {

        newnode->nextptr = head;

        head = newnode;

    }

    else

    {

        curnode = head;

        while (curnode != NULL)

        {

            if (curnode->num == m)

            {

                find = 1;

                break;

            }

            else

            {

                prevnode = curnode;

                curnode = curnode->nextptr;

            }

        }

        if (find == 0)

            cout << " Element not found " << endl;

        else

        {

            prevnode->nextptr = newnode;

            newnode->nextptr = curnode;

        }

    }

}

void delete_head(){

    cout<<"\n\nDeleting the head!";

    node *temp = head;

    head = head->nextptr;

    free(temp);

}

void delete_end(){

    cout << "\n\nDeleting the end!";

    node *temp = head;

    while(temp->nextptr->nextptr!=NULL){

        temp = temp->nextptr;

    }

    temp->nextptr = NULL;

}

void delete_value(){

    cout << "\n\nDeleting the value!";

    int m, find = 0;

    node *prevnode;

    newnode = new node; //start creation of the new node

    cout << " \nEnter data for node to delete : ";

    cin >> m;

    if (head->num == m) // if we will add before head

    {

        head = head->nextptr;

    }

    else

    {   

        prevnode = head;

        curnode = head->nextptr;

        while (curnode->num != m)

        {

            prevnode = curnode;

            curnode = curnode->nextptr;

        }

        prevnode->nextptr = curnode->nextptr;

    }

}

OUTPUT:


Related Solutions

Python 3 Function which takes the head Node of a linked list and sorts the list...
Python 3 Function which takes the head Node of a linked list and sorts the list into non-descending order. PARAM: head_node The head of the linked list RETURNS: The node at the head of the sorted linked list. ''' def sort(head_node): #Code goes here ''' Test code goes here '' ' if __name__ == '__main__':
The programming language is Python Instructions: Create a function that will delete a node in a...
The programming language is Python Instructions: Create a function that will delete a node in a Linked List based on position number. On below example, if you want to delete position #2, it will remove the Banana (arrangement of nodes below is Apple, Banana, Cherry, Grapes, Orange). myLinkedList = LinkedList() myLinkedList.append("Banana") myLinkedList.append("Cherry") myLinkedList.append("Grapes") myLinkedList.append("Orange") myLinkedList.prepend("Apple") myLinkedList.deleteByPositionNum(2) node = myLinkedList.head while node: print(node.value, " ") node = node.next_node You may start with the function head: def deleteByPositionNum(self, positionNum):
In C++, write a member method delete() that deletes a node from a linked list at...
In C++, write a member method delete() that deletes a node from a linked list at a random position. (It should first randomly generate that position. and then delete that node).
You are given a reference to the head node of a linked list that stores integers....
You are given a reference to the head node of a linked list that stores integers. Please print the minimum element in this linked list. The class ListNode.java contains the description of a single node in the linked list. It has a num field to store the integer number and a reference next that points to the next element in the list. The file MyList.class is a pre-defined java code, that creates a linked list. The file ListSmallest.java creates an...
You are given a reference to the head node of a linked list that stores integers....
You are given a reference to the head node of a linked list that stores integers. Please print the minimum element in this linked list. The class ListNode.java contains the description of a single node in the linked list. It has a num field to store the integer number and a reference next that points to the next element in the list. The file MyList.class is a pre-defined java code, that creates a linked list. The file ListSmallest.java creates an...
You are given a reference to the head node of a linked list that stores integers....
You are given a reference to the head node of a linked list that stores integers. Please print the minimum element in this linked list. The class ListNode.java contains the description of a single node in the linked list. It has a num field to store the integer number and a reference next that points to the next element in the list. The file MyList.class is a pre-defined java code, that creates a linked list. The file ListSmallest.java creates an...
You're given the pointer to the head node of a ordered linked list, an integer to...
You're given the pointer to the head node of a ordered linked list, an integer to add to the list. Write a function that inserts a number in the the list preserving its order. If the head pointer contains a null pointer that indicates an empty list. Function insertNode has the following parameters: head: a SinglyLinkedListNode pointer to the head of the list data: an integer value to insert as data in your new node Function prototype: SinglyLinkedListNode* insertNode(SinglyLinkedListNode* head,...
You are given a reference to the head node of a linked list that stores integers....
You are given a reference to the head node of a linked list that stores integers. Please print the minimum element in this linked list. The class ListNode.java contains the description of a single node in the linked list. It has a num field to store the integer number and a reference next that points to the next element in the list. The file MyList.class is a pre-defined java code, that creates a linked list. The file ListSmallest.java creates an...
You are given a reference to the head node of a linked list that stores integers....
You are given a reference to the head node of a linked list that stores integers. Please print the minimum element in this linked list. The class ListNode.java contains the description of a single node in the linked list. It has a num field to store the integer number and a reference next that points to the next element in the list. The file MyList.class is a pre-defined java code, that creates a linked list. The file ListSmallest.java creates an...
You are given a reference to the head node of a linked list that stores integers....
You are given a reference to the head node of a linked list that stores integers. Please print the minimum element in this linked list. The class ListNode.java contains the description of a single node in the linked list. It has a num field to store the integer number and a reference next that points to the next element in the list. The file MyList.class is a pre-defined java code, that creates a linked list. The file ListSmallest.java creates an...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT