Please fill in the code to reverse a linked list. IN C++
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
using namespace std;
/* Link list node */
struct Node {
int data;
// your code here
};
/* Function to reverse the linked list */
static void reverse(struct Node** head_ref)
{
// your code here
}
/* Function to push a node */
void push(struct Node** head_ref, int new_data)
{
// your code here
}
/* Function to print linked list */
void printList(struct Node* head)
{
struct Node* temp = head;
while (temp != NULL) {
cout << temp->data << " ";
temp = temp->next;
}
}
/* Driver program to test above function*/
int main()
{
/* Start with the empty list */
struct Node* head = NULL;
push(&head, 10);
push(&head, 20);
push(&head, 15);
push(&head, 85);
cout << "Forward list\n";
printList(head);
reverse(&head);
cout << "\nReversed list \n";
printList(head);
} Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. If not, PLEASE let me know before you rate, I’ll help you fix whatever issues. Thanks
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
using namespace std;
/* Link list node */
struct Node {
int data;
Node* next;
};
/* Function to reverse the linked list */
static void reverse(struct Node** head_ref)
{
//taking a reference to head node
Node* curr=*head_ref;
//initializing two Node* variables to NULL
Node* prev=NULL;
Node* next=NULL;
//looping until curr is NULL
while(curr!=NULL){
//storing next of curr in next
next=curr->next;
//setting prev as new next node of prev
curr->next=prev;
//assigning curr to prev
prev=curr;
//and setting next as new curr
curr=next;
}
//finally, setting prev as new head node
*head_ref=prev;
}
/* Function to push a node */
void push(struct Node** head_ref, int new_data)
{
//creating new node, setting data
Node* node=new Node();
node->data=new_data;
//if head is null, adding as head node
if(*head_ref==NULL){
*head_ref=node;
}else{
//taking reference to head
Node* current=*head_ref;
//looping until current is the last node
while(current->next!=NULL){
current=current->next;
}
//adding node as the next of current
current->next=node;
}
}
/* Function to print linked list */
void printList(struct Node* head)
{
struct Node* temp = head;
while (temp != NULL) {
cout << temp->data << " ";
temp = temp->next;
}
}
/* Driver program to test above function*/
int main()
{
/* Start with the empty list */
struct Node* head = NULL;
push(&head, 10);
push(&head, 20);
push(&head, 15);
push(&head, 85);
cout << "Forward list\n";
printList(head);
reverse(&head);
cout << "\nReversed list \n";
printList(head);
}
/*OUTPUT*/
Forward list
10 20 15 85
Reversed list
85 15 20 10
Please fill in the code to reverse a linked list. IN C++ #include <stdio.h> #include <stdlib.h>...
Please fill in this code to reverse a linked list: (written in C/C++) #include #include #include using namespace std; /* Link list node */ struct Node { int data; // your code here }; /* Function to reverse the linked list */ static void reverse(struct Node** head_ref) { // your code here } /* Function to push a node */ void push(struct Node** head_ref, int new_data) { // your code here } /* Function to print linked list */ void...
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...
C LANGUAGE I just need the void push() function, which inserts new node to the front of the list #include<stdio.h> #include<stdlib.h> typedef struct node { int data; struct node *next; } Node; //Creating head a as a global Node* Node *head; /* Given a node prev_node, insert a new node after the given prev_node */ void insertAfter (Node * prev_node, int new_data) { /*1. check if the given prev_node is NULL */ if (prev_node == NULL) { printf ("the given...
Deleting multiples of a given integer from a linked list: #include <stdio.h> #include <stdlib.h> #include <assert.h> #define MAX 10000 typedef struct node_tag { int v; // data struct node_tag * next; // A pointer to this type of struct } node; // Define a type. Easier to use. node * create_node(int v) { node * p = malloc(sizeof(node)); // Allocate memory assert(p != NULL); // you can be nicer // Set the value in the node. p->v = v; p->next...
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...
C++ NEED HELP WITH MY REVERSE STRING FUNCTION IN LINK LIST A function Reverse, that traverses the linked list and prints the reverse text to the standard output, without changing the linked list. ( Pass the linked list by value, you have the freedom to create a doubly linked list that is a copy of the original list in your program before you call the function) #include "pch.h" #include <iostream> #include <string.h> #include <string> using namespace std; #define MAX 512...
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) { ...
Using C, I need help debugging this program. I have a few error messages that I'm not sure how to fix. Here is the code I have: /* * C Program to Print a Linked List in Reverse Order */ #include <stdio.h> #include <stdlib.h> struct node { int num; struct node *next; }; int main() { struct node *p = NULL; struct node_occur *head = NULL; int n; printf("Enter data into the list\n"); create(&p); printf("Displaying the nodes in the list:\n");...
C LINKED LIST #include <stdlib.h> #include <stdio.h> //Struct for a polynomial term (double linked list object) typedef struct Polynomial t_Polynomial; typedef struct Term { // coefficient of the polynomial term associated with the object int coef; // power of the polynomial term associated with the object int power; // pointer to the next object (to facilitate a doubly linked list) struct Term *next; // pointer to the previous object (to facilitate a doubly linked list) struct Term...
***CODE MUST BE IN C++*** Using the linked list in "basic linked list" which has a STRUCTURE for each node, write FUNCTION which starts at the head and outputs the value for each node until the last node is reached. Note: your function should work with the structure that is in this program. Please do not use the example which is for a class, but this example canbe helkpful. Also note that the function must work no matter how many nodes...