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

Please finish the int LLInsert function. typedef struct STUDENT { char *Netid; i

ID: 3781740 • Letter: P

Question

Please finish the int LLInsert function.

typedef struct STUDENT
{
char *Netid;
int Midterm;
int Final;
} STUDENT;


typedef char* LLKey;
typedef STUDENT LLValue;

typedef struct LLNode
{
LLKey Key;
LLValue Value;
struct LLNode *Next;
} LLNode;

typedef struct LL
{
LLNode *Head;
int Count;
} LL;


//
// LLCreate: dynamically creates and returns an empty linked-list:
//
LL *LLCreate()
{
LL *list;

list = (LL *) malloc(sizeof(LL));
list->Head = NULL;
list->Count = 0;

return list;
}

//
// LLCompareKeys: compares key1 and key2, returning
// value < 0 if key1 < key2
// 0 if key1 == key2
// value > 0 if key1 > key2
//
int LLCompareKeys(LLKey key1, LLKey key2)
{
// NOTE: with STUDENTs, keys are Netids, so use strcmp.
if (strcmp(key1, key2) < 0)
return -1;
else if (strcmp(key1, key2) == 0)
return 0;
else
return 1;
}

//
// LLInsert: inserts the given (key, value) pair into the linked-list such
// that the new key is in ascending order with respect to the other keys.
// Returns true (non-zero) if the insert was successful, returns false (0)
// if the given key is already in the list -- and the given (key, value)
// pair is not inserted.
//
int LLInsert(LL *list, LLKey key, LLValue value)
{



return 1; // insert was successful:
}


// inputs and discards the remainder of the current line for the
// given input stream, including the EOL character(s):
void skipRestOfInput(FILE *stream)
{
char restOfLine[256];
int rolLength = sizeof(restOfLine) / sizeof(restOfLine[0]);

fgets(restOfLine, rolLength, stream);
}

int main(int argc, char *argv[])
{
char netid[16];
int mid, fnl, added;
STUDENT s;

LL *list = LLCreate();
LLNode *cur = NULL;

memdebug_init(0); // 1 => interactive; switch to 0 inside zyLabs

scanf("%s %d %d", netid, &mid, &fnl);
skipRestOfInput(stdin);

while (netid[0] != '#')
{
s.Netid = (char *)malloc((strlen(netid)+1) * sizeof(char));
strcpy(s.Netid, netid);
s.Midterm = mid;
s.Final = fnl;

// add to end of list:
LLNode *node;
node = (LLNode *) malloc(sizeof(LLNode));
node->Key = s.Netid;
node->Value = s;
node->Next = NULL;

if (cur == NULL) // first time:
{
list->Head = node;
cur = node;
}
else // new node follows current:
{
cur->Next = node;
cur = node;
}

list->Count++;

scanf("%s %d %d", netid, &mid, &fnl);
skipRestOfInput(stdin);
}

//
// now call insert function to see if it works:
//
scanf("%s %d %d", netid, &mid, &fnl);
skipRestOfInput(stdin);

s.Netid = (char *)malloc((strlen(netid) + 1) * sizeof(char));
strcpy(s.Netid, netid);
s.Midterm = mid;
s.Final = fnl;

added = LLInsert(list, s.Netid, s);
if (!added)
{
printf("**not inserted** ");
}
else
{
printf("**inserted** ");
}

// output contents of list:
printf(">>Count=%d ", list->Count);

cur = list->Head;
while (cur != NULL)
{
printf(">>%s: %d,%d ", cur->Value.Netid, cur->Value.Midterm, cur->Value.Final);
cur = cur->Next;
}


return 0;
}

Explanation / Answer

PROGRAM CODE:

#include <stdio.h>
#include<string.h>
#include<malloc.h>
typedef struct STUDENT
{
char *Netid;
int Midterm;
int Final;
} STUDENT;

typedef char* LLKey;
typedef STUDENT LLValue;
typedef struct LLNode
{
LLKey Key;
LLValue Value;
struct LLNode *Next;
} LLNode;
typedef struct LL
{
LLNode *Head;
int Count;
} LL;

//
// LLCreate: dynamically creates and returns an empty linked-list:
//
LL *LLCreate()
{
LL *list;
list = (LL *) malloc(sizeof(LL));
list->Head = NULL;
list->Count = 0;
return list;
}
//
// LLCompareKeys: compares key1 and key2, returning
// value < 0 if key1 < key2
// 0 if key1 == key2
// value > 0 if key1 > key2
//
int LLCompareKeys(LLKey key1, LLKey key2)
{
// NOTE: with STUDENTs, keys are Netids, so use strcmp.
if (strcmp(key1, key2) < 0)
return -1;
else if (strcmp(key1, key2) == 0)
return 0;
else
return 1;
}
//
// LLInsert: inserts the given (key, value) pair into the linked-list such
// that the new key is in ascending order with respect to the other keys.
// Returns true (non-zero) if the insert was successful, returns false (0)
// if the given key is already in the list -- and the given (key, value)
// pair is not inserted.
//
int LLInsert(LL *list, LLKey key, LLValue value)
{
LLNode *node = (LLNode *) malloc(sizeof(LLNode));
node->Key = key;
node->Value = value;
node->Next = NULL;
LLNode *temp = list->Head;
while(temp != NULL)
{
    if(LLCompareKeys(temp->Key, key)==0)
    {
        return 0;
    }
       
    temp = temp->Next;
}

if(list->Head == NULL)
{
    list->Head = node;
}
else
{
    LLNode *cur = list->Head;
    while(cur->Next != NULL)
    {
        if(LLCompareKeys(cur->Key, key)==1)
        {
            LLNode *temp =(LLNode *) malloc(sizeof(LLNode));
            temp->Key = cur->Key;
            temp->Value = cur->Value;
            temp->Next = cur->Next;
            cur->Key = key;
            cur->Value = value;
            cur->Next = temp;
            list->Head = cur;
            return 1;
        }
        cur = cur->Next;
    }
   
}
return 1; // insert was successful:
}

// inputs and discards the remainder of the current line for the
// given input stream, including the EOL character(s):
void skipRestOfInput(FILE *stream)
{
char restOfLine[256];
int rolLength = sizeof(restOfLine) / sizeof(restOfLine[0]);
fgets(restOfLine, rolLength, stream);
}
int main(int argc, char *argv[])
{
char netid[16];
int mid, fnl, added;
STUDENT s;
LL *list = LLCreate();
LLNode *cur = NULL;
//memdebug_init(0); // 1 => interactive; switch to 0 inside zyLabs
scanf("%s %d %d", netid, &mid, &fnl);
skipRestOfInput(stdin);
while (netid[0] != '#')
{
s.Netid = (char *)malloc((strlen(netid)+1) * sizeof(char));
strcpy(s.Netid, netid);
s.Midterm = mid;
s.Final = fnl;
// add to end of list:
LLNode *node;
node = (LLNode *) malloc(sizeof(LLNode));
node->Key = s.Netid;
node->Value = s;
node->Next = NULL;
if (cur == NULL) // first time:
{
list->Head = node;
cur = node;
}
else // new node follows current:
{
cur->Next = node;
cur = node;
}
list->Count++;
scanf("%s %d %d", netid, &mid, &fnl);
skipRestOfInput(stdin);
}
//
// now call insert function to see if it works:
//
scanf("%s %d %d", netid, &mid, &fnl);
skipRestOfInput(stdin);
s.Netid = (char *)malloc((strlen(netid) + 1) * sizeof(char));
strcpy(s.Netid, netid);
s.Midterm = mid;
s.Final = fnl;
added = LLInsert(list, s.Netid, s);
//added = 1;
if (!added)
{
printf("**not inserted** ");
}
else
{
printf("**inserted** ");
}
// output contents of list:
printf(">>Count=%d ", list->Count);
cur = list->Head;
while (cur != NULL)
{
printf(">>%s: %d,%d ", cur->Value.Netid, cur->Value.Midterm, cur->Value.Final);
cur = cur->Next;
}

return 0;
}

INPUT:
ASD234 23 34
ASD233 23 34
ASD234 23 34
#ASD235 2 3

OUTPUT: