Question

THE ATTACHED PICTURES ARE THE ALGORITHMS of link list IN C PLEASE write the programs of these pictures in C++
while writing the programs make sure that your programs are correct and please do them separately and while doing them comment the EACH STEP I AM posting my question third time please do them PROPERLY and do them separately and comment EACH STEP

Deleting Last Node 1) P- head 2) while P.link<>NIL Q- P P-P.link 3) Q.link= NIL 4) Dispose(P) 5) Exit

Deletion Algorithm for deleting intermediate node Count =1 P-head while count<> N and P<>NIL Q-p Count Count+1 p- P.link IF P

Please do it properly label EACH STEP in EACH program of C++ please do not do it in C it is a humble request please do it properly  
this is the assignment please do it properly make sure that your answer is correct please do all steps and comment EACH STEP please it is a humble request

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

Last node deleting c++ code:

// CPP program to remove last node of
// linked list.
#include <iostream>
using namespace std;
  
/* Link list node */
struct Node {
int data;
struct Node* next;
};
  
/* Function to remove the last node   
of the linked list */
Node* removeLastNode(struct Node* head)
{
if (head == NULL)
return NULL;
  
if (head->next == NULL)
{
delete head;
return NULL;
}
  
// Find the second last node
Node* second_last = head;
while (second_last->next->next != NULL)
second_last = second_last->next;
  
// Delete last node
delete (second_last->next);
  
// Change next of second last
second_last->next = NULL;
  
return head;
}
  
// Function to push node at head
void push(struct Node** head_ref, int new_data)
{
struct Node* new_node = new Node;
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}
  
// Driver code
int main()
{
/* Start with the empty list */
Node* head = NULL;
  
/* Use push() function to construct
the below list 8 -> 23 -> 11 -> 29 -> 12 */
push(&head, 12);
push(&head, 29);
push(&head, 11);
push(&head, 23);
push(&head, 8);
  
head = removeLastNode(head);
for (Node* temp = head; temp != NULL; temp = temp->next)
cout << temp->data << " ";
  
return 0;
}

2. Deletion of intermediate node in c++

// C++ program to delete middle of a linked list
#include<bits/stdc++.h>
using namespace std;
  
/* Link list Node */
struct Node
{
int data;
struct Node* next;
};
  
// Deletes middle node and returns head of the
// modified list
struct Node* deleteMid(struct Node *head)
{
// Base cases
if (head == NULL)
return NULL;
if (head->next == NULL)
{
delete head;
return NULL;
}
  
// Initialize slow and fast pointers to reach
// middle of linked list
struct Node *slow_ptr = head;
struct Node *fast_ptr = head;
  
// Find the middle and previous of middle.
struct Node *prev; // To store previous of slow_ptr
while (fast_ptr != NULL && fast_ptr->next != NULL)
{
fast_ptr = fast_ptr->next->next;
prev = slow_ptr;
slow_ptr = slow_ptr->next;
}
  
//Delete the middle node
prev->next = slow_ptr->next;
delete slow_ptr;
  
return head;
}
  
// A utility function to print a given linked list
void printList(struct Node *ptr)
{
while (ptr != NULL)
{
cout << ptr->data << "->";
ptr = ptr->next;
}
cout << "NULL\n";
}
  
// Utility function to create a new node.
Node *newNode(int data)
{
struct Node *temp = new Node;
temp->data = data;
temp->next = NULL;
return temp;
}
  
/* Drier program to test above function*/
int main()
{
/* Start with the empty list */
struct Node* head = newNode(1);
head->next = newNode(2);
head->next->next = newNode(3);
head->next->next->next = newNode(4);
  
cout << "Gven Linked List\n";
printList(head);
  
head = deleteMid(head);
  
cout << "Linked List after deletion of middle\n";
printList(head);
  
return 0;
}

I had written these answer by self please thumbs up, and if you have any other query please feel free to ask in comment.

Add a comment
Know the answer?
Add Answer to:
THE ATTACHED PICTURES ARE THE ALGORITHMS of link list IN C PLEASE write the programs of...
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
  • Please answer all three parts. And show step-by-step answers for each part. Draw anything if necessary....

    Please answer all three parts. And show step-by-step answers for each part. Draw anything if necessary. And please don't copy other answers to be at risk being downvoted. Thank you. Question 1 (50 POINTS): Given a graph G and the Breadth First Search (BFS) and Depth First Search (DFS) traversal algorithms as follows: BFSG) 1 for each vertex u € G.V – {3} 1 2 u.color = WHITE 3 u.d = 0 4 un = NIL 3 5 S.color =...

  • 5.8 Sort Linked List Use mergersort to sort a linked list Do not use the following...

    5.8 Sort Linked List Use mergersort to sort a linked list Do not use the following libraries: algorithm, cmath. Do not load the values into a vector and sort them. The sort must be done with a linked list Example Two lists 6->3->1->2->4->5 will return a list with 1->2->3->4->5->6. Input There will be no input read in anymore. Going forward use the hard-coded input on the template and ensure the program works. There will be one compare output test based...

  • hw3 Data Structure: Please write programs and finish the question in C language: thank you (You...

    hw3 Data Structure: Please write programs and finish the question in C language: thank you (You can do both 5 & 6 if you know both, or you can do any one of these 2 questions that you know, thank you!) 5. Generate a function for clockwise rotation each node (either char type or int type) in doubly linked list by N places, e.g. given list = NULL<-Head<=>c<=>i<=>v<=>i>=>c->NULL, from function call rotate DLL(list, 3), the output will be like this...

  • Please use C programming to write the code to solve the following problem. Also, please use the i...

    Please use C programming to write the code to solve the following problem. Also, please use the instructions, functions, syntax and any other required part of the problem. Thanks in advance. Use these functions below especially: void inputStringFromUser(char *prompt, char *s, int arraySize); void songNameDuplicate(char *songName); void songNameFound(char *songName); void songNameNotFound(char *songName); void songNameDeleted(char *songName); void artistFound(char *artist); void artistNotFound(char *artist); void printMusicLibraryEmpty(void); void printMusicLibraryTitle(void); const int MAX_LENGTH = 1024; You will write a program that maintains information about your...

  • In C++ syntax please Write a program that implements and demonstrates a linked list using functions....

    In C++ syntax please Write a program that implements and demonstrates a linked list using functions. Your program should first dehne a node that stores an integer and then your program will include the following functions appendo- This function accepts the head pointer (by reference) of a linked list and an integer as it's only arguments. It then creates a node, stores the integer argument in the node, and adds it to the end of the list. findo-This function accepts...

  • Problem: Implement an interface that manipulates a list of strings. You will be provided with the...

    Problem: Implement an interface that manipulates a list of strings. You will be provided with the following files (see below): • StringList.h containing a class declaration, set up for a linked list representation. • Driver.cpp containing a main function you can use to test your implementation. You will be responsible for providing the StringList.cpp file, including the implementation of the StringList member functions (described below): StringList and ~StringList: creates an empty list, and deallocates all the nodes in the list,...

  • CSCI 2010 Lab11 Link-Lists Lab 11A Linked-Lists Preparation Create a Visual Studio C++ Project C...

    CSCI 2010 Lab11 Link-Lists Lab 11A Linked-Lists Preparation Create a Visual Studio C++ Project C2010Lab11A Add the following to the project. //LinkedList.cpp #include <cstdlib> #include "LinkedList.h" using namespace std; //--------------------------------------------------- //List Element Members //--------------------------------------------------- ListElement::ListElement(int d, ListElement * n) {    datum=d;    next=n; } int ListElement::getDatum () const {    return datum; } ListElement const* ListElement::getNext () const {    return next; } //--------------------------------------------------- //LinkedList Members //--------------------------------------------------- LinkedList::LinkedList () {    head=NULL; } void LinkedList::insertItem(int item) {    ListElement *currPtr = head;    ListElement *prevPtr =...

  • Template Deque Class (C++) In this assignment, we will use a given test menu for the template Deq...

    Template Deque Class (C++) In this assignment, we will use a given test menu for the template Deque Class (a Linked List based Double Ended Queue, Deque) that you have put together. Recommended Steps testDeque.cpp : // C++ implementation of doubly linked list Deque doubly linked list #include <bits/stdc++.h> using namespace std; class Timer { // To replace with the full timer class definition // inside this folder: LearnCpp9_18_timeSortArray.cpp }; // Node of a doubly linked list template<class T> class...

  • Instructions Part 1 - Implementation of a Doubly Linked List Attached you will find the code for ...

    Instructions Part 1 - Implementation of a Doubly Linked List Attached you will find the code for an implementation of a Singly Linked List. There are 3 files: Driver.java -- testing the List functions in a main() method. LinkNode.java -- Class definition for a Node, which is the underlying entity that stores the items for the linked list. SinglyLinkedList.java -- Class definition for the Singly Linked List. All the heavy lifting happens here. Task 1 - Review & Testing: Create...

  • Pleass write Java Program with comments. For this peoblem it supposed to create ur own list of po...

    Pleass write Java Program with comments. For this peoblem it supposed to create ur own list of points, then use that list to find the closest point. A list of points and the points have x and y values such as [(1,2),(3,5),(6,7),....] that’s just example. Implement the algorithm to meet the following requirements Define a class named Pair with data fields pl and p2 to represent two points and a method named getDistance) that returns the distance between the two...

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