typedef struct llnode{ int val ; struct llnode * nxt ; } llnode ; llnode* newNod
ID: 3791428 • Letter: T
Question
typedef struct llnode{
int val ;
struct llnode * nxt ;
} llnode ;
llnode* newNode( int val ){
llnode* n = malloc(sizeof(llnode));
n->val = val ;
n->nxt = NULL ;
return n ;
}
llnode* pushFront( llnode* head, llnode* push){
push->nxt = head ;
return push;
}
llnode *pushBack( llnode* head, llnode* push){
llnode *oHead = head ;
if( head == NULL )
return push;
while( head->nxt != NULL )
head = head->nxt ;
head->nxt = push ;
return oHead ;
}
llnode *arr2ll( int arr[] , int size ){
llnode *head = newNode(arr[size-1]);
int i ;
for( i=size-2 ; i>=0 ; i-- ){
head = pushFront(head,newNode(arr[i]));
}
return head ;
}
llnode *insert( llnode *head, llnode *ins, int loc){
return NULL ;
}
int getMin( llnode *head ){
/*
* Write a procedure that accepts a pointer to a linked list
* node and returns the smallest value stored in the linked list.
* You should assume that the linked list node pointed to is the
* head of the linked list. You should assume the a linked list
* struct contains an integer value ``val'' and a pointer to
* another linked list node ``nxt''. You should assume that the
* tail of the linked list has a nxt which points to NULL. */
return 0 ;
}
int cntVal( llnode *head, int v ){
/* Write a procedure that accepts a pointer to a linked list node and a value
``v''.
* The function should return the number of times that ``v'' appears in the
linked list.
* You should assume that the linked list node pointed to is the head of th
e linked list.
* You should assume the a linked list struct contains an integer value ``v
al'' and a
* pointer to another linked list node ``nxt''. You should assume that the
tail of the
* linked list has a nxt which points to NULL. */
return 0 ;
}
please help finish getMin( llnode *head ) and cntVal( llnode *head, int v )
Explanation / Answer
int getMin(llnode *head) {
llnode *p = head;
int min = 0;
while(p != null)
{
if(p->val < min)
{ min = p->val; }
p = p->nxt;
}
}
return min;
}
int cntVal(llnode *head, int v)
{
llnode *p = head;
int cntVal = 0;
while(p != null) {
if(p->val == v)
{ cntVal++; }
p = p->nxt;
}
return cntVal;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.