SlideShare a Scribd company logo
1 of 9
Download to read offline
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;
}
Solution
PROGRAM CODE:
#include
#include
#include
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:

More Related Content

Similar to Please finish the int LLInsert function.typedef struct STUDENT {.pdf

-JAVA-provide a test class that do the required -you may add met.pdf
-JAVA-provide a test class that do the required -you may add met.pdf-JAVA-provide a test class that do the required -you may add met.pdf
-JAVA-provide a test class that do the required -you may add met.pdfalphawheels007
 
Program In C You are required to write an interactive C program that.pdf
Program In C You are required to write an interactive C program that.pdfProgram In C You are required to write an interactive C program that.pdf
Program In C You are required to write an interactive C program that.pdfamitbagga0808
 
Doublylinklist
DoublylinklistDoublylinklist
Doublylinklistritu1806
 
Using the provided table interface table.h and the sample linked lis.pdf
Using the provided table interface table.h and the sample linked lis.pdfUsing the provided table interface table.h and the sample linked lis.pdf
Using the provided table interface table.h and the sample linked lis.pdfconnellalykshamesb60
 
take the following code and give details of what each line of code i.pdf
take the following code and give details of what each line of code i.pdftake the following code and give details of what each line of code i.pdf
take the following code and give details of what each line of code i.pdffastechsrv
 
M 0 1 2 3 4 5 6 7 0.pdf
 M  0  1  2  3  4  5  6  7    0.pdf M  0  1  2  3  4  5  6  7    0.pdf
M 0 1 2 3 4 5 6 7 0.pdfajay1317
 
#include stdafx.h #include iostream using namespace std;vo.docx
#include stdafx.h #include iostream using namespace std;vo.docx#include stdafx.h #include iostream using namespace std;vo.docx
#include stdafx.h #include iostream using namespace std;vo.docxajoy21
 
GIVEN CODE template -typename T- class DList { private- struct Node {.docx
GIVEN CODE template -typename T- class DList { private- struct Node {.docxGIVEN CODE template -typename T- class DList { private- struct Node {.docx
GIVEN CODE template -typename T- class DList { private- struct Node {.docxLeonardN9WWelchw
 
mainpublic class AssignmentThree {    public static void ma.pdf
mainpublic class AssignmentThree {    public static void ma.pdfmainpublic class AssignmentThree {    public static void ma.pdf
mainpublic class AssignmentThree {    public static void ma.pdffathimafancyjeweller
 
Write a C program that reads the words the user types at the command.pdf
Write a C program that reads the words the user types at the command.pdfWrite a C program that reads the words the user types at the command.pdf
Write a C program that reads the words the user types at the command.pdfSANDEEPARIHANT
 
#includeiostream struct node {    char value;    struct no.pdf
#includeiostream struct node {    char value;    struct no.pdf#includeiostream struct node {    char value;    struct no.pdf
#includeiostream struct node {    char value;    struct no.pdfankitmobileshop235
 
Help I keep getting the same error when running a code. Below is the.pdf
Help I keep getting the same error when running a code. Below is the.pdfHelp I keep getting the same error when running a code. Below is the.pdf
Help I keep getting the same error when running a code. Below is the.pdfmail931892
 
Lec-4_Linked-List (1).pdf
Lec-4_Linked-List (1).pdfLec-4_Linked-List (1).pdf
Lec-4_Linked-List (1).pdfKylaMaeGarcia1
 

Similar to Please finish the int LLInsert function.typedef struct STUDENT {.pdf (20)

-JAVA-provide a test class that do the required -you may add met.pdf
-JAVA-provide a test class that do the required -you may add met.pdf-JAVA-provide a test class that do the required -you may add met.pdf
-JAVA-provide a test class that do the required -you may add met.pdf
 
Linked Stack program.docx
Linked Stack program.docxLinked Stack program.docx
Linked Stack program.docx
 
Ds 2 cycle
Ds 2 cycleDs 2 cycle
Ds 2 cycle
 
Program In C You are required to write an interactive C program that.pdf
Program In C You are required to write an interactive C program that.pdfProgram In C You are required to write an interactive C program that.pdf
Program In C You are required to write an interactive C program that.pdf
 
Doublylinklist
DoublylinklistDoublylinklist
Doublylinklist
 
Lab-2.2 717822E504.pdf
Lab-2.2 717822E504.pdfLab-2.2 717822E504.pdf
Lab-2.2 717822E504.pdf
 
Using the provided table interface table.h and the sample linked lis.pdf
Using the provided table interface table.h and the sample linked lis.pdfUsing the provided table interface table.h and the sample linked lis.pdf
Using the provided table interface table.h and the sample linked lis.pdf
 
take the following code and give details of what each line of code i.pdf
take the following code and give details of what each line of code i.pdftake the following code and give details of what each line of code i.pdf
take the following code and give details of what each line of code i.pdf
 
Linked lists
Linked listsLinked lists
Linked lists
 
M 0 1 2 3 4 5 6 7 0.pdf
 M  0  1  2  3  4  5  6  7    0.pdf M  0  1  2  3  4  5  6  7    0.pdf
M 0 1 2 3 4 5 6 7 0.pdf
 
#include stdafx.h #include iostream using namespace std;vo.docx
#include stdafx.h #include iostream using namespace std;vo.docx#include stdafx.h #include iostream using namespace std;vo.docx
#include stdafx.h #include iostream using namespace std;vo.docx
 
137 Lab-2.2.pdf
137 Lab-2.2.pdf137 Lab-2.2.pdf
137 Lab-2.2.pdf
 
GIVEN CODE template -typename T- class DList { private- struct Node {.docx
GIVEN CODE template -typename T- class DList { private- struct Node {.docxGIVEN CODE template -typename T- class DList { private- struct Node {.docx
GIVEN CODE template -typename T- class DList { private- struct Node {.docx
 
mainpublic class AssignmentThree {    public static void ma.pdf
mainpublic class AssignmentThree {    public static void ma.pdfmainpublic class AssignmentThree {    public static void ma.pdf
mainpublic class AssignmentThree {    public static void ma.pdf
 
Write a C program that reads the words the user types at the command.pdf
Write a C program that reads the words the user types at the command.pdfWrite a C program that reads the words the user types at the command.pdf
Write a C program that reads the words the user types at the command.pdf
 
#includeiostream struct node {    char value;    struct no.pdf
#includeiostream struct node {    char value;    struct no.pdf#includeiostream struct node {    char value;    struct no.pdf
#includeiostream struct node {    char value;    struct no.pdf
 
Help I keep getting the same error when running a code. Below is the.pdf
Help I keep getting the same error when running a code. Below is the.pdfHelp I keep getting the same error when running a code. Below is the.pdf
Help I keep getting the same error when running a code. Below is the.pdf
 
C Exam Help
C Exam Help C Exam Help
C Exam Help
 
Lec-4_Linked-List (1).pdf
Lec-4_Linked-List (1).pdfLec-4_Linked-List (1).pdf
Lec-4_Linked-List (1).pdf
 
C Homework Help
C Homework HelpC Homework Help
C Homework Help
 

More from fortmdu

How is a Decision Support Systems (DSS) different from a Management .pdf
How is a Decision Support Systems (DSS) different from a Management .pdfHow is a Decision Support Systems (DSS) different from a Management .pdf
How is a Decision Support Systems (DSS) different from a Management .pdffortmdu
 
I am trying to create a program That works with two other programs i.pdf
I am trying to create a program That works with two other programs i.pdfI am trying to create a program That works with two other programs i.pdf
I am trying to create a program That works with two other programs i.pdffortmdu
 
How does anti-malware software detect virusesWhat techniques are .pdf
How does anti-malware software detect virusesWhat techniques are .pdfHow does anti-malware software detect virusesWhat techniques are .pdf
How does anti-malware software detect virusesWhat techniques are .pdffortmdu
 
How can I upload a picture in here I tried with image properties ic.pdf
How can I upload a picture in here I tried with image properties ic.pdfHow can I upload a picture in here I tried with image properties ic.pdf
How can I upload a picture in here I tried with image properties ic.pdffortmdu
 
Explain the role of ATP in the action of Hsp70 and GroELES.Solu.pdf
Explain the role of ATP in the action of Hsp70 and GroELES.Solu.pdfExplain the role of ATP in the action of Hsp70 and GroELES.Solu.pdf
Explain the role of ATP in the action of Hsp70 and GroELES.Solu.pdffortmdu
 
Files Please respond to the following •Suppose you are creating.pdf
Files Please respond to the following •Suppose you are creating.pdfFiles Please respond to the following •Suppose you are creating.pdf
Files Please respond to the following •Suppose you are creating.pdffortmdu
 
evil_server.cpp#include string #include cstdlib #include.pdf
evil_server.cpp#include string #include cstdlib #include.pdfevil_server.cpp#include string #include cstdlib #include.pdf
evil_server.cpp#include string #include cstdlib #include.pdffortmdu
 
ecorrect Question 32 0 1 pts Referencing the diagram above, use the .pdf
ecorrect Question 32 0 1 pts Referencing the diagram above, use the .pdfecorrect Question 32 0 1 pts Referencing the diagram above, use the .pdf
ecorrect Question 32 0 1 pts Referencing the diagram above, use the .pdffortmdu
 
Disneys Expedition EverestOne of the newest thrill rides to open.pdf
Disneys Expedition EverestOne of the newest thrill rides to open.pdfDisneys Expedition EverestOne of the newest thrill rides to open.pdf
Disneys Expedition EverestOne of the newest thrill rides to open.pdffortmdu
 
Discuss ONE risk that a company faces when trying to diversify inte.pdf
Discuss ONE risk that a company faces when trying to diversify inte.pdfDiscuss ONE risk that a company faces when trying to diversify inte.pdf
Discuss ONE risk that a company faces when trying to diversify inte.pdffortmdu
 
Describe at least one reason why transitioning from PVST+ to Rapid P.pdf
Describe at least one reason why transitioning from PVST+ to Rapid P.pdfDescribe at least one reason why transitioning from PVST+ to Rapid P.pdf
Describe at least one reason why transitioning from PVST+ to Rapid P.pdffortmdu
 
CASE 2-1 BUILDING UP OUR ASSETS DHR CONSTRUCTIONIn August 2011, w.pdf
CASE 2-1 BUILDING UP OUR ASSETS DHR CONSTRUCTIONIn August 2011, w.pdfCASE 2-1 BUILDING UP OUR ASSETS DHR CONSTRUCTIONIn August 2011, w.pdf
CASE 2-1 BUILDING UP OUR ASSETS DHR CONSTRUCTIONIn August 2011, w.pdffortmdu
 
C++ Write a function that takes two numbers are parameters and retu.pdf
C++ Write a function that takes two numbers are parameters and retu.pdfC++ Write a function that takes two numbers are parameters and retu.pdf
C++ Write a function that takes two numbers are parameters and retu.pdffortmdu
 
B.1 Reaction of a Hydrate Addition of Water (2) Appearance Heating (1.pdf
B.1 Reaction of a Hydrate Addition of Water (2) Appearance Heating (1.pdfB.1 Reaction of a Hydrate Addition of Water (2) Appearance Heating (1.pdf
B.1 Reaction of a Hydrate Addition of Water (2) Appearance Heating (1.pdffortmdu
 
Assignment isPage 349-350 #4 and #5 Use the Linked List lab.pdf
Assignment isPage 349-350 #4 and #5 Use the Linked List lab.pdfAssignment isPage 349-350 #4 and #5 Use the Linked List lab.pdf
Assignment isPage 349-350 #4 and #5 Use the Linked List lab.pdffortmdu
 
You maintain several virtual machines (VMs) in an offline state. How.pdf
You maintain several virtual machines (VMs) in an offline state. How.pdfYou maintain several virtual machines (VMs) in an offline state. How.pdf
You maintain several virtual machines (VMs) in an offline state. How.pdffortmdu
 
Which of the following statements is not TRUE1)For a 64-bit compute.pdf
Which of the following statements is not TRUE1)For a 64-bit compute.pdfWhich of the following statements is not TRUE1)For a 64-bit compute.pdf
Which of the following statements is not TRUE1)For a 64-bit compute.pdffortmdu
 
You are running an ELISA on a sample to test for the presence of .pdf
You are running an ELISA on a sample to test for the presence of .pdfYou are running an ELISA on a sample to test for the presence of .pdf
You are running an ELISA on a sample to test for the presence of .pdffortmdu
 
You are to write an efficient program that will read a dictionary of.pdf
You are to write an efficient program that will read a dictionary of.pdfYou are to write an efficient program that will read a dictionary of.pdf
You are to write an efficient program that will read a dictionary of.pdffortmdu
 
X = C B - B C D; Use Accumulator Register-Register (LoadSt.pdf
X = C B - B  C  D; Use Accumulator  Register-Register (LoadSt.pdfX = C B - B  C  D; Use Accumulator  Register-Register (LoadSt.pdf
X = C B - B C D; Use Accumulator Register-Register (LoadSt.pdffortmdu
 

More from fortmdu (20)

How is a Decision Support Systems (DSS) different from a Management .pdf
How is a Decision Support Systems (DSS) different from a Management .pdfHow is a Decision Support Systems (DSS) different from a Management .pdf
How is a Decision Support Systems (DSS) different from a Management .pdf
 
I am trying to create a program That works with two other programs i.pdf
I am trying to create a program That works with two other programs i.pdfI am trying to create a program That works with two other programs i.pdf
I am trying to create a program That works with two other programs i.pdf
 
How does anti-malware software detect virusesWhat techniques are .pdf
How does anti-malware software detect virusesWhat techniques are .pdfHow does anti-malware software detect virusesWhat techniques are .pdf
How does anti-malware software detect virusesWhat techniques are .pdf
 
How can I upload a picture in here I tried with image properties ic.pdf
How can I upload a picture in here I tried with image properties ic.pdfHow can I upload a picture in here I tried with image properties ic.pdf
How can I upload a picture in here I tried with image properties ic.pdf
 
Explain the role of ATP in the action of Hsp70 and GroELES.Solu.pdf
Explain the role of ATP in the action of Hsp70 and GroELES.Solu.pdfExplain the role of ATP in the action of Hsp70 and GroELES.Solu.pdf
Explain the role of ATP in the action of Hsp70 and GroELES.Solu.pdf
 
Files Please respond to the following •Suppose you are creating.pdf
Files Please respond to the following •Suppose you are creating.pdfFiles Please respond to the following •Suppose you are creating.pdf
Files Please respond to the following •Suppose you are creating.pdf
 
evil_server.cpp#include string #include cstdlib #include.pdf
evil_server.cpp#include string #include cstdlib #include.pdfevil_server.cpp#include string #include cstdlib #include.pdf
evil_server.cpp#include string #include cstdlib #include.pdf
 
ecorrect Question 32 0 1 pts Referencing the diagram above, use the .pdf
ecorrect Question 32 0 1 pts Referencing the diagram above, use the .pdfecorrect Question 32 0 1 pts Referencing the diagram above, use the .pdf
ecorrect Question 32 0 1 pts Referencing the diagram above, use the .pdf
 
Disneys Expedition EverestOne of the newest thrill rides to open.pdf
Disneys Expedition EverestOne of the newest thrill rides to open.pdfDisneys Expedition EverestOne of the newest thrill rides to open.pdf
Disneys Expedition EverestOne of the newest thrill rides to open.pdf
 
Discuss ONE risk that a company faces when trying to diversify inte.pdf
Discuss ONE risk that a company faces when trying to diversify inte.pdfDiscuss ONE risk that a company faces when trying to diversify inte.pdf
Discuss ONE risk that a company faces when trying to diversify inte.pdf
 
Describe at least one reason why transitioning from PVST+ to Rapid P.pdf
Describe at least one reason why transitioning from PVST+ to Rapid P.pdfDescribe at least one reason why transitioning from PVST+ to Rapid P.pdf
Describe at least one reason why transitioning from PVST+ to Rapid P.pdf
 
CASE 2-1 BUILDING UP OUR ASSETS DHR CONSTRUCTIONIn August 2011, w.pdf
CASE 2-1 BUILDING UP OUR ASSETS DHR CONSTRUCTIONIn August 2011, w.pdfCASE 2-1 BUILDING UP OUR ASSETS DHR CONSTRUCTIONIn August 2011, w.pdf
CASE 2-1 BUILDING UP OUR ASSETS DHR CONSTRUCTIONIn August 2011, w.pdf
 
C++ Write a function that takes two numbers are parameters and retu.pdf
C++ Write a function that takes two numbers are parameters and retu.pdfC++ Write a function that takes two numbers are parameters and retu.pdf
C++ Write a function that takes two numbers are parameters and retu.pdf
 
B.1 Reaction of a Hydrate Addition of Water (2) Appearance Heating (1.pdf
B.1 Reaction of a Hydrate Addition of Water (2) Appearance Heating (1.pdfB.1 Reaction of a Hydrate Addition of Water (2) Appearance Heating (1.pdf
B.1 Reaction of a Hydrate Addition of Water (2) Appearance Heating (1.pdf
 
Assignment isPage 349-350 #4 and #5 Use the Linked List lab.pdf
Assignment isPage 349-350 #4 and #5 Use the Linked List lab.pdfAssignment isPage 349-350 #4 and #5 Use the Linked List lab.pdf
Assignment isPage 349-350 #4 and #5 Use the Linked List lab.pdf
 
You maintain several virtual machines (VMs) in an offline state. How.pdf
You maintain several virtual machines (VMs) in an offline state. How.pdfYou maintain several virtual machines (VMs) in an offline state. How.pdf
You maintain several virtual machines (VMs) in an offline state. How.pdf
 
Which of the following statements is not TRUE1)For a 64-bit compute.pdf
Which of the following statements is not TRUE1)For a 64-bit compute.pdfWhich of the following statements is not TRUE1)For a 64-bit compute.pdf
Which of the following statements is not TRUE1)For a 64-bit compute.pdf
 
You are running an ELISA on a sample to test for the presence of .pdf
You are running an ELISA on a sample to test for the presence of .pdfYou are running an ELISA on a sample to test for the presence of .pdf
You are running an ELISA on a sample to test for the presence of .pdf
 
You are to write an efficient program that will read a dictionary of.pdf
You are to write an efficient program that will read a dictionary of.pdfYou are to write an efficient program that will read a dictionary of.pdf
You are to write an efficient program that will read a dictionary of.pdf
 
X = C B - B C D; Use Accumulator Register-Register (LoadSt.pdf
X = C B - B  C  D; Use Accumulator  Register-Register (LoadSt.pdfX = C B - B  C  D; Use Accumulator  Register-Register (LoadSt.pdf
X = C B - B C D; Use Accumulator Register-Register (LoadSt.pdf
 

Recently uploaded

Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Mark Reed
 
Romantic Opera MUSIC FOR GRADE NINE pptx
Romantic Opera MUSIC FOR GRADE NINE pptxRomantic Opera MUSIC FOR GRADE NINE pptx
Romantic Opera MUSIC FOR GRADE NINE pptxsqpmdrvczh
 
Planning a health career 4th Quarter.pptx
Planning a health career 4th Quarter.pptxPlanning a health career 4th Quarter.pptx
Planning a health career 4th Quarter.pptxLigayaBacuel1
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfMr Bounab Samir
 
Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptxSherlyMaeNeri
 
AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.arsicmarija21
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfUjwalaBharambe
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementmkooblal
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designMIPLM
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxAnupkumar Sharma
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxRaymartEstabillo3
 
ACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfSpandanaRallapalli
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Celine George
 
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxGrade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxChelloAnnAsuncion2
 

Recently uploaded (20)

Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)
 
Romantic Opera MUSIC FOR GRADE NINE pptx
Romantic Opera MUSIC FOR GRADE NINE pptxRomantic Opera MUSIC FOR GRADE NINE pptx
Romantic Opera MUSIC FOR GRADE NINE pptx
 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
Planning a health career 4th Quarter.pptx
Planning a health career 4th Quarter.pptxPlanning a health career 4th Quarter.pptx
Planning a health career 4th Quarter.pptx
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
 
Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptx
 
AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of management
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-design
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
 
OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...
 
ACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdf
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
Rapple "Scholarly Communications and the Sustainable Development Goals"
Rapple "Scholarly Communications and the Sustainable Development Goals"Rapple "Scholarly Communications and the Sustainable Development Goals"
Rapple "Scholarly Communications and the Sustainable Development Goals"
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17
 
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxGrade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
 

Please finish the int LLInsert function.typedef struct STUDENT {.pdf

  • 1. 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
  • 2. // 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); }
  • 3. 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); }
  • 4. // // 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; } Solution PROGRAM CODE: #include #include #include
  • 5. 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 //
  • 6. 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) {
  • 7. 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];
  • 8. 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: //
  • 9. 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: