(10 pts) Write a function called addNode that inserts a node structure onto a st
ID: 3851996 • Letter: #
Question
(10 pts) Write a function called addNode that inserts a node structure onto a stack, by attaching it at the beginning of a linked list. The function accepts two arguments new and pHead 9. n h asrhrted new and pHead is the address of a variable which holds the address of the first node structure of the stack implemented in a inked list of node structures. When the stack is empty, pHead has NULL as its value. Your function must attach the new node structure at the beginning of the list, and then return a pointer to the new node structure that was just attached. The node structure looks like this: typedef struct node f int value; struct node next;/* next node is NULL if end of list */ NODEExplanation / Answer
C Program :
typedef struct node{
int value;
struct node *next;
}
node addNode(node newNode, node pHead){
//If stack is null pHead is NULL. .Then make the new Node as the Head, pHead
if(pHead.== NULL){
pHead = newNode;
return pHead;
}
//Set the next node of the new Node to the head [NEW]------next------->[Head]
newNode.next = pHead;
//Change the head to the new Node [NEW_HEAD]------next------->[Old Head]
pHead = newNode;
return pHead;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.