Due 6/23/2017 (Fr) 11:59 PM date: Submission: C source file, Assgn10.c, on Black
ID: 3851085 • Letter: D
Question
Due 6/23/2017 (Fr) 11:59 PM date: Submission: C source file, Assgn10.c, on Blackboard: http:lluml.umassonline.ne Description: Anode in a linked list is defined by Wpedef struct node int value; info member struct node next; pointer to a node NODE A sorted linked list maintains nodes such values in the list are in ascending order. It also includes a header node, which does not carry any value information but serves the purpose of providing a sentinel node to the list. When the list is pointed by sortL the head assures that never becomes NULL The following functions are to be written: Int is Empty(N NODE sortL) The function isEmpty(sortL return 1 for true when the list is empty. Even when the list is empty, you still have the header node. void ODE "sortL, NODE newNode); To the exiting list pointed by ptr, save the newNode so that the values stored in the newNode keeps the list order in the ascending order. void NODE sortL); Print values in the list as they are traversed from the beginning of the list to the last. The insert function without the header node can be written as follows. This is just for your reference. NODE No ptr, NODE "newNode){ newNode: next ptr; ptr pewNode; else if (ptr value newNode- value)d DewNode: next ptr ptr. pewNode; else EVENT prey, curr; prey. ptr curr ptr- next; while (curr NULL && curr- value newNade: value)( curr curr- next; prey- next RewNade new Node; next curr;Explanation / Answer
/* Program to insert in a sorted list */
#include<stdio.h>
#include<stdlib.h>
/* link list node */
typedef struct node{
int value; //info member
struct node *next; // pointer to a node
}NODE;
/* A utility function to create a new node */
NODE *newNode(int new_data)
{
/* allocate node */
NODE* new_node = (NODE*) malloc(sizeof(NODE*));
/* put in the data */
new_node->value = new_data;
new_node->next = NULL;
return new_node;
}
/*
Function for inserting a new node in llist.
since we are returning nothing, we should use parameters call by value
*/
void insert(NODE** sortL, NODE* newNode)
{
NODE* current;
/*
if sortL is empty then we are setting newnode as sortList
if head is having value that is greater than new node value, then we are inserting newnode before sortList
*/
if (isEmpty(sortL) || (*sortL)->value >= newNode->value)
{
newNode->next = *sortL;
*sortL = newNode;
}
else
{
/* Locating the node before the point of insertion */
current = *sortL;
while (current->next!=NULL && current->next->value < newNode->value)
{
current = current->next;
}
newNode->next = current->next;
current->next = newNode;
}
}
// function to check whether the list is empty or not
int isEmpty(NODE *sortL){
if(sortL == NULL){
return 1;
}
else{
return 0;
}
}
/* Function to travers linked list */
void traverse(NODE *sortL)
{
NODE *ptr = sortL; //starting with head
while(ptr != NULL)
{
printf("%d ", ptr->value);
ptr = ptr->next;
}
}
int main()
{
/* Start with the empty list */
NODE* head = NULL;
printf(" %d ",isEmpty(head));
NODE *new_node = newNode(5);
insert(&head, new_node);
new_node = newNode(10);
insert(&head, new_node);
new_node = newNode(4);
insert(&head, new_node);
traverse(head);
printf(" %d ",isEmpty(head));
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.