Data Structure- Sheet 1 2020
Q1- Assume the structure of a Linked List node is as follows.
Structnode
{
Int data;
Struct node *next;
};
Explain the functionality of following C functions.
1. What does the following function do for a given Linked List?
Void fun1(struct node* head)
{
if(head == NULL)
return;
fun1(head->next);
printf("%d ", head->data);
}
Answer:
fun1() prints the given Linked List in reverse manner. For Linked List 1->2->3->4- >5, fun1() prints 5->4->3->2-
>1.
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
if(head == NULL) { //Checks whether list is empty
return; }
else {
for(current = head; current != NULL; current = current-
>next) { //Initially, current will point to head node
for(index = current->next; index != NULL; index = index-
>next) { //index will point to node next to current
if(current->data == index->data) {
temp = index; //Store the duplicate node in temp
//index's previous node will point to node next to index thus, removes the duplicate node
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:
void print(struct node * head_ptr)
struct node *ptr;
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:
int count_42s(const node* head_ptr)
{int count =0;
while (head_ptr!=Null)
{if (head_ptr->data == 42)
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.)
1. void list_tail_insert(node* head_ptr, const node::value_type& entry);
2. // Precondition: head_ptr is the head pointer of a non-empty
3. // linked list.
4. // Postcondition: A new node has been added at the tail end
5. // of the list. The data in the new node is taken from the
6. // parameter called entry.