Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Q1 - 25 pts) The deleteElement int deleteData) member function in the code for t

ID: 3869770 • Letter: Q

Question



Q1 - 25 pts) The deleteElement int deleteData) member function in the code for the Singly Linked List based implementation of a List ADT deletes the first occurrence of deleteData in the List. Modify this member function in such a way that all occurrences of deleteData in the List are deleted with a single call to the deleteElement function from main. After you modify the deleteElement function, run the main function (as given in the startup code for this question) by creating a List of at least 10 elements with certain elements repeating. Now ask for a value (delete Data) to delete from the user and call the deleteElement deleteData) function to delete all occurrences of deleteData in the List. Capture the output of your program displaying the contents of the List before and after the call to the deleteElement function A sample of the expected screenshot of the program is shown below. As noticed in the screenshot, all occurrences of deleteData '15' in the List are removed after a call to the deleteElement function. Enter the nu" her of ele"ento you want 50 insert: 18 Enter element : 12 Enter element 1 : 14 Enter element 2 14 Enter element : 15 Enter elemen Enter ele-ent 7 : 15 : 19 Contente of the List: (hefore delete 12 14 14 15 15 18 17 15 19 14 Contente of the List Cafter delete 12 14 14 18 17 19 14 F(n-2) + F(n-1), Q2 - 25 pts) The Fibonacci sequence is generated using the following recursion: Fn) for n > 1·F(0) = 0 and F( 1 ) =1 In this question, you will generate the Fibonacci sequence as a "Singly Linked List" of integers 0, 1, 1, 2, 3, 5, 8, 13, 21, 34,. N where N is the largest integer in the sequence that is less than the integer J comprising the last three digits of your For example, if your J# is J00543244, then J s 244 . In that case, the Fibonacci sequence that is generated should be 0, 2, 3, 5, 8, 13, 21, 34. 55, 89, 144, 233 If the last three digits of your J# form an integer that is less than 100, add 100 to the last three digits of your J# and use the resulting value as J. For example, if your J# is J00543034, then J is 34 + 100-134 and the Fibonacci sequence generated would be: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89 You are given the code for the Singly Linked List-based implementation of the List ADT. Add a member function constructSequence int) to the List class that takes an integer argument (upperBound: for the Fibonacci sequence) The main function given to you already inserts the first two nodes (with data 0 and 1 respectively) to a List called FibonacciList. Your task will be to implement the constructSequence function that will insert a sequence of nodes (one node per iteration) to the FibonacciList whose last element ill be the largest element in the sequence that is less than the upperBound Q3 - 20 pts) You are given the code (including the main function) for evaluating an expression in post-fix format using Stack. Your task is to modify the code in the main function to input an expression in pre-fix

Explanation / Answer

Solution:

1.

#include<stdio.h>
#include <stdio.h>
#include <stdlib.h>

// A linked list node
struct Node
{
int data;
struct Node *next;
};
/* Given a reference to the head of a list
and an int, inserts a new node on the front of the list. */
void insertel(struct Node** head_ref, int new_data)
{
struct Node* new_node = (struct Node*) malloc(sizeof(struct Node));
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}
void deleteElement(struct Node **head_ref, int key)
{
// Store head node
struct Node* temp = *head_ref, *prev;

// If head node itself holds the key or multiple occurrences of key
while (temp != NULL && temp->data == key)
{
*head_ref = temp->next; // Changed head
free(temp); // free old head
temp = *head_ref; // Change Temp
}

// Delete occurrences other than head
while (temp != NULL)
{
// Search for the key to be deleted, keep track of the
// previous node as we need to change 'prev->next'
while (temp != NULL && temp->data != key)
{
prev = temp;
temp = temp->next;
}

// If key was not present in linked list
if (temp == NULL) return;

// Unlink the node from linked list
prev->next = temp->next;

free(temp); // Free memory

//Update Temp for next iteration of outer loop
temp = prev->next;
}
}

// This function prints contents of linked list starting from
// the given node
void printList(struct Node *node)
{
while (node != NULL)
{
printf(" %d ", node->data);
node = node->next;
}
}

/* Drier program to test above functions*/
int main()
{
/* Start with the empty list */
struct Node* head = NULL;
int n,a;
printf("Enter the number of elements you want to insert ");
scanf("%d",&n);
int i=0;
while(n--)
{
printf("Enter element %d ",n);
scanf("%d",&a);
insertel(&head,a);
  
}
  


puts("Contents of the list:(before delete) ");
printList(head);
puts(" Enter the data to delete");
scanf("%d",&a);
deleteElement(&head, a);
puts(" Contents of the list:(after delete) ");

printList(head);
return 0;
}

2.

#include <stdio.h>
#include <stdlib.h>

// A linked list node
struct Node
{
int data;
struct Node *next;
};

/* Given a reference (pointer to pointer) to the head of a list and
an int, inserts a new node on the front of the list. */
void push(struct Node** head_ref, int new_data)
{
/* 1. allocate node */
struct Node* new_node = (struct Node*) malloc(sizeof(struct Node));

/* 2. put in the data */
new_node->data = new_data;

/* 3. Make next of new node as head */
new_node->next = (*head_ref);

/* 4. move the head to point to the new node */
(*head_ref) = new_node;
}

/* Given a reference (pointer to pointer) to the head
of a list and an int, appends a new node at the end */
void append(struct Node** head_ref, int new_data)
{
/* 1. allocate node */
struct Node* new_node = (struct Node*) malloc(sizeof(struct Node));

struct Node *last = *head_ref; /* used in step 5*/

/* 2. put in the data */
new_node->data = new_data;

/* 3. This new node is going to be the last node, so make next of
it as NULL*/
new_node->next = NULL;

/* 4. If the Linked List is empty, then make the new node as head */
if (*head_ref == NULL)
{
*head_ref = new_node;
return;
}

/* 5. Else traverse till the last node */
while (last->next != NULL)
last = last->next;

/* 6. Change the next of last node */
last->next = new_node;
return;
}

int nthfib(int n)
{
if(n==0) return 0;
else if (n==1) return 1;
else return (nthfib(n-1)+nthfib(n-2));
}
void constructSequence(struct Node ** head_ref,int J)
{
//struct Node *head = head_ref;
int a=2,b;
b = nthfib(a);
while(b<J)
{
append(head_ref,b);
a++;
b = nthfib(a);
}
}
// This function prints contents of linked list starting from head
void printList(struct Node *node)
{
while (node != NULL)
{
printf(" %d ", node->data);
node = node->next;
}
}

/* Driver program to test above functions*/
int main()
{
/* Start with the empty list */
struct Node* head = NULL;
append(&head,0);
append(&head,1);
int J;
printf("Enter the value of J");
scanf("%d",&J);
constructSequence(&head,J);

printf(" Created Linked list is: ");
printList(head);

return 0;
}

3. Assuming you are given code for evaluating postfix the following code converts prefix to postfix

4. Stack as singly linked list

// C program for linked list implementation of stack

#include <stdio.h>

#include <stdlib.h>

#include <limits.h>

// A structure to represent a stack

struct StackNode

{

int data;

struct StackNode* next;

};

struct StackNode* newNode(int data)

{

struct StackNode* stackNode =

(struct StackNode*) malloc(sizeof(struct StackNode));

stackNode->data = data;

stackNode->next = NULL;

return stackNode;

}

int isEmpty(struct StackNode *root)

{

return !root;

}

void push(struct StackNode** root, int data)

{

struct StackNode* stackNode = newNode(data);

stackNode->next = *root;

*root = stackNode;

printf("%d pushed to stack ", data);

}

int pop(struct StackNode** root)

{

if (isEmpty(*root))

return INT_MIN;

struct StackNode* temp = *root;

*root = (*root)->next;

int popped = temp->data;

free(temp);

return popped;

}

int peek(struct StackNode* root)

{

if (isEmpty(root))

return INT_MIN;

return root->data;

}

int main()

{

struct StackNode* root = NULL;

push(&root, 5);

push(&root, 60);

push(&root, 3);

push(&root, 32);

push(&root, 53);

printf("%d popped from stack ", pop(&root));

printf("Top element is %d ", peek(root));

return 0;

}