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

Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin ClassesCeline George
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...Poonam Aher Patil
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfNirmal Dwivedi
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...pradhanghanshyam7136
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsMebane Rash
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxDenish Jangid
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the ClassroomPooky Knightsmith
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSCeline George
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.MaryamAhmad92
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseAnaAcapella
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxAreebaZafar22
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptxMaritesTamaniVerdade
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701bronxfugly43
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfSherif Taha
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Association for Project Management
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.pptRamjanShidvankar
 

Recently uploaded (20)

Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Spatium Project Simulation student brief
Spatium Project Simulation student briefSpatium Project Simulation student brief
Spatium Project Simulation student brief
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 

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: