Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Any help would be appreciated, tia! Write a MIPS program to add a given node in a linked list at the specified location, using Nested
Any help would be appreciated, tia!
Write a MIPS program to add a given node in a linked list at the specified location, using Nested Procedure Calls. If your code runs perfectly, but you didn't use Procedure Execution correctly, you will be given zero points.
Given Inputs:
the head of a linked list, ie address of the start first node of the list
location: number of node in the linked list after which node is to be added to add before the st node, to add after st node and so on
the address of the node to be inserted And each node contains:
an integer value
address to the next node address is NULL if it is the last node in the list
Write the following three functions that will be called in order to update the linked list by adding the new node.
main
task: calls the addNode function and reads the value of the newly added node.
addNode
inputs: head of linked list head location to add node n and address of node to be added node
task: calls the findNode function and add the node after the nth node in the linked list. If the number is greater than the size of list, then add the node at the end of the list.
output: value of the inserted node.
findNode
inputs: head of linked list head and location to add node n
task: navigate the linked list to find the addresses of the nth and nth nodes
outputs: addresses of the nth and nth nodes.
Following is a sample C code segment to perform the required task. this code is incomplete and does not include creating the linked list or a new node, so you cannot compile it using any C compiler. You may modify the code for the functions, but the task performed should not be changed.
Parameters of a node in the linked list need not declare or initialize in MIPS
typedef struct node
int value; Value in the node accessed by nodevalue
node next; Address of next node accessed by nodenext
node; Datatype for each node
node head; address of head first node of linked list global pointer
int main
Variable Declaration
node newNode; address of node to be added
int n; number of the node in the list after which node is to be added
int value; Value of the node to be added
Task of main function
value addNodehead n newNode;
int addNode node head, int n node newNode
node addraddr; addr address of nth node, addr address of nth node
if n If node should be added at the beginning of the list
newNodenext head; Next for new node head of original list
head newNode; global head updated to the new node
returnnewNodevalue; value of the node data at the address of the node, and then return to caller
addr addr findNode head n; Call findNode function
addrnext newNode; Next for nth node node to be added
newNodenext addr; Next for added node nth node of original list
returnnewNodevalue; value of the node data at the address of the node
node findNode node head, int n
node curr head; Start with head of linked list
for int i ; i n; i
curr currnext; Update the pointer to next node address
if currnext Break if end of List
break;
returncurr currnext; Two return values need not return as array in MIPS
Registers Variables
$s head
$s newNode
$s n
$s val
Linked List and New Node in Memory:
Addresses Contents
newNode newNodevalue
head nodevalue
head nodenext
nodenext nodevalue
nodenext nodenext
nodenext nodevalue
nodenext nodenext
Example Test: If the values of $s through $s and Memory contents are initialized in the simulator as:
Registers Data
$s
$s
$s
$s
Addresses Contents
The resultant registers are:
Registers Data
$s
$s
$s
$s
The resultant array is:
Addresses Contents
This is what I have, but I get an error saying Line : Unrecognized syntax near: data
data
head: word x
newNode: word
location: word
text
globl main
main:
lw $a head
la $a newNode
lw $a location
jal addNode
li $v
syscall
addNode:
beqz $a addAtStart
jal findNode
lw $t$a
sw $t$a
sw $a$a
jr $ra
addAtStart:
sw $a$a
sw $a head
jr $ra
findNode:
jr $ra
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started