Linked List Sheet 2020 Answer
Linked List Sheet 2020 Answer
Q2-You’re given the pointer to the head node of a linked list. Change the Nextpointers of the
nodes so that their order is reversed. The head pointer given may be null meaning that the
initial list is empty.
Input Format
You have to complete the Node* Reverse(Node* head) method which takes one argument - the
head of the linked list.
Q3- Write a function that returns the maximum integer value of a linked without affecting
the original linked list.
Answer:
int maxInt(struct node *head)
{ int max=0;
while(head!=NULL)
{if(max<head->data)
max=head->data;
head=head->next;
1
Data Structure- Sheet 1 2020
return max;
Q4-Given an array, print the Next Greater Element (NGE) for every element. The Next
greater Element for an element x is the first greater element on the right side of x in
array. Elements for which no greater element exist, consider next greater element as -1.
Example: For the input array {4, 5, 2, 25}, the next greater elements for each element are as
follows.
4 → 5, 5 → 25, 2 → 25, 25 → -1
stack
Q5-Write a C++ Method to: Given a list of unique integers is stored in a Doubly Linked List(
in no particular order). Given a pointer to the first node in the list, delete the node containing
the integer x, and return a pointer to the first node.
Answer:
void removeDuplicateNode() {
struct node *current, *index, *temp; //Node current will point to head
return; }
else {
if(current->data == index->data) {
index->previous->next = index->next;
if(index->next != NULL)
index->next->previous = index->previous;
2
Data Structure- Sheet 1 2020
temp = NULL; }
Q6-Suppose that p is a pointer to a node in a linked list, and *p is not the tail node. What are
the steps to removing the node after *p? Use one short English sentence for each step.
Answer:
1. define a temporary pointer to Node and assign the value in p->link to the temporary
pointer.
2. assign the value in data member next of the temporary pointer to the data member next of
p.
3. delete node pointed to by the temporary pointer.
In C++ the code will look the following way
1) Node *temp = p->next;
2) p->next = temp->next;
3) delete temp;
Q7-Suppose we are using the usual node definition (with member functions called data and link).
Your program is using a node* variable called head_ptr to point to the first node of a linked list
(or head_ptr == NULL for the empty list). Write a few lines of C++ code that will print all the
double numbers on the list.
Answer:
3
Data Structure- Sheet 1 2020
ptr=head_ptr;
while(ptr!=NULL)
{cout<<ptr->data;
ptr= ptr->link;
Q8-Implement the following function as a new function for the linked list toolkit. (Use the usual
node definition with member variables called data and link.)
size_t count_42s(const node* head_ptr);
// Precondition: head_ptr is the head pointer of a linked list.
// The list might be empty or it might be non-empty.
// Postcondition: The return value is the number of occurrences
// of 42 in the data field of a node on the linked list.
// The list itself is unchanged.
Answer:
while (head_ptr!=Null)
count++;
head_ptr= head_ptr->link;
return count;
Q9-Implement the following function as a new function for the linked list toolkit. (Use the usual
node definition with member variables called data and link. The data field is an int.)
int sum(const node* head_ptr);
// Precondition: head_ptr is the head pointer of a linked list.
4
Data Structure- Sheet 1 2020
// The list might be empty or it might be non-empty.
// Postcondition: The return value is the sum of all the data components
// of all the nodes. NOTE: If the list is empty, the function returns 0.
Answer:
int sum(const node* head_ptr)
{node *p;
int sum=0;
for(p= * head_ptr;p!=NULL;p=p->link)
sum+=p->data;
return sum;
Q10-Implement the following function as a new function for the linked list toolkit. (Use the
usual node definition with member variables called data and link.)