SlideShare a Scribd company logo
Write the definition of the linkedListKeepLast function. (Please write the following in C,
Thank you)
The purpose of the function is to create a new linked list containing
the last node in each linked list in the hash table beginning with the first linked list.
The rest of the nodes are to be displayed to the screen, then returned to the heap.
Example:
If the user enters the following keys:
201, 102, 233, 567, 456, 654, 465, 645, quit
list at index 0 is empty
list at index 1 is not empty:
201
102
567
list at index 2 is not empty:
233
list at index 3 is empty
list at index 4 is not empty:
456
654
465
645
Deleted nodes:
--> at index 0:
--> at index 1: 201 102
--> at index 2:
--> at index 3:
--> at index 4: 456 654 465
The final linked list contains:
567
233
645
Written by:
*/
#include <stdio.h>
#include <stdlib.h> // malloc(), free(), exit()
#include <string.h>
#define NUMPOINTERS 5
typedef struct node STUDENTREC;
struct node
{
char id[10];
struct node *next;
};
// Function Declarations
int hash(char id[]);
STUDENTREC *insert(char id[],
STUDENTREC *student_body[],
int hashval);
void traverse(STUDENTREC *student_body[]);
void displayLL(STUDENTREC *list, char *description);
STUDENTREC *linkedListKeepLast(STUDENTREC *student_body[]);
int main (void)
{
STUDENTREC *student_body[NUMPOINTERS] = {NULL};
STUDENTREC *person;
STUDENTREC *endList;
char id[10];
int hashval;
printf(" ~*~ Hashing using collision resolution by chaining ~*~n");
printf("t Enter Student ID (or quit): ");
scanf("%s", id);
while(strcmp(id, "quit"))
{
hashval = hash(id);
person = insert(id, student_body, hashval);
if (person) // not NULL => duplicate
{
printf("Duplicate record!n");
}
printf("t Enter Student ID (or quit): ");
scanf("%s", id);
}
traverse(student_body);
endList = linkedListKeepLast(student_body);
displayLL(endList, "New List");
traverse(student_body);
return 0;
}
/*
The purpose of the function is to create a new linked list containing
the last node in each linked list in the hash table beginning with the first linked list.
The rest of the nodes are to be displayed to the screen, then returned to the heap.
*/
STUDENTREC *linkedListKeepLast(STUDENTREC *student_body[])
{
STUDENTREC *newList = NULL;
/* *********************************************************
Write your code here
Get the job done without calling other linked list functions
********************************************************* */
return newList;
}
/***************************************************
Hash Student ID by summing the cubes
of the ASCII value of characters and then take
the modulo of this sum.
*/
int hash(char id[])
{
long sum = 0;
while (*id) // != '0'
{
sum += *id * *id * *id;
id++;
}
return sum % NUMPOINTERS;
}
/***************************************************
Insert a new Social Security number into the
array of student records, at index equal to
hashvalue
*/
STUDENTREC *insert(char id[],
STUDENTREC *student_body[],
int hashval)
{
STUDENTREC **mover; // Use ** to write elegant code
mover = &student_body[hashval];
while (*mover)
{
if (strcmp(id,(*mover)->id) == 0) return *mover;
mover = &((*mover)->next);
}
if ((*mover = (STUDENTREC *) malloc(sizeof(STUDENTREC))) == NULL)
{
printf("Malloc error in insert!n");
exit(1);
}
strcpy((*mover)->id, id);
(*mover)->next = NULL; // set the link of the new node to NULL
printf("%s has been placed in the list at location %d.n", (*mover)->id, hashval);
return NULL;
}
/***************************************************
Traversing the lists in a hash table
*/
void traverse(STUDENTREC *student_body[])
{
int i;
// STUDENTREC **mover; // Use ** for fun and practice
// not needed for traverse
for (i = 0; i < NUMPOINTERS; i++)
{
printf("Contents of list %2dn", i);
printf("--------------------n");
// for (mover = &student_body[i]; *mover; mover = &(*mover)->next)
// { // &((*mover)->next)
// printf("%sn", (*mover)->ss);
// }
displayLL(student_body[i], NULL);
printf("n");
}
}
/***************************************************
Traversing a linked list
*/
void displayLL(STUDENTREC *list, char *description)
{
if (list) // != NULL
{
if (description) // != NULL
{
printf ("%sn", description);
printf("--------------------n");
}
STUDENTREC **mover; // Use ** for fun and practice
// not needed for traverse
for (mover = &list; *mover; mover = &(*mover)->next)
{ // &((*mover)->next)
printf("%sn", (*mover)->id);
}
}
else
printf ("Empty!");
printf("n");
}
/*
~*~ Hashing using collision resolution by chaining ~*~
Enter Student ID (or quit): 201
201 has been placed in the list at location 1.
Enter Student ID (or quit): 102
102 has been placed in the list at location 1.
Enter Student ID (or quit): 233
233 has been placed in the list at location 2.
Enter Student ID (or quit): 567
567 has been placed in the list at location 1.
Enter Student ID (or quit): 456
456 has been placed in the list at location 4.
Enter Student ID (or quit): 654
654 has been placed in the list at location 4.
Enter Student ID (or quit): 465
465 has been placed in the list at location 4.
Enter Student ID (or quit): 645
645 has been placed in the list at location 4.
Enter Student ID (or quit): quit
Contents of list 0
--------------------
Empty!
Contents of list 1
--------------------
201
102
567
Contents of list 2
--------------------
233
Contents of list 3
--------------------
Empty!
Contents of list 4
--------------------
456
654
465
645
Deleted nodes:
--> at index 0:
--> at index 1: 201 102
--> at index 2:
--> at index 3:
--> at index 4: 456 654 465
New List
--------------------
567
233
645
Contents of list 0
--------------------
Empty!
Contents of list 1
--------------------
Empty!
Contents of list 2
--------------------
Empty!
Contents of list 3
--------------------
Empty!
Contents of list 4
--------------------
Empty!
*/

More Related Content

Similar to Write the definition of the linkedListKeepLast function- (Please write.docx (20)

PPT
Linked list
Fraboni Ec
 
PPT
Linked list
Tony Nguyen
 
PPT
Linked list
Harry Potter
 
PPT
Linked list
James Wong
 
PPT
Linked list
Young Alista
 
PPT
Linked list
Hoang Nguyen
 
PPT
17 linkedlist (1)
Himadri Sen Gupta
 
PPTX
linkedlistforslideshare-210123143943.pptx
shesnasuneer
 
PPTX
Linked List.pptx
SherinRappai
 
PPT
DS Unit 2.ppt
JITTAYASHWANTHREDDY
 
PPT
LINKED LIST IN C++ +2 COMPUTER SCIENCE CBSE AND STATE SYLLABUS
Venugopalavarma Raja
 
PPTX
Ll.pptx
chin463670
 
PPTX
linked list.pptx
chin463670
 
PPTX
LINKED LIST.pptx
ManojUniversity
 
PPT
Linked list
Trupti Agrawal
 
PDF
Linked List Static and Dynamic Memory Allocation
Prof Ansari
 
PPT
Unit ii(dsc++)
Durga Devi
 
PDF
Lec-4_Linked-List (1).pdf
KylaMaeGarcia1
 
PPTX
UNIT 2LINKEDLISdddddddddddddddddddddddddddT.pptx
shesnasuneer
 
PPTX
Linked Lists, Single Linked list and its operations
BackiyalakshmiVenkat
 
Linked list
Fraboni Ec
 
Linked list
Tony Nguyen
 
Linked list
Harry Potter
 
Linked list
James Wong
 
Linked list
Young Alista
 
Linked list
Hoang Nguyen
 
17 linkedlist (1)
Himadri Sen Gupta
 
linkedlistforslideshare-210123143943.pptx
shesnasuneer
 
Linked List.pptx
SherinRappai
 
DS Unit 2.ppt
JITTAYASHWANTHREDDY
 
LINKED LIST IN C++ +2 COMPUTER SCIENCE CBSE AND STATE SYLLABUS
Venugopalavarma Raja
 
Ll.pptx
chin463670
 
linked list.pptx
chin463670
 
LINKED LIST.pptx
ManojUniversity
 
Linked list
Trupti Agrawal
 
Linked List Static and Dynamic Memory Allocation
Prof Ansari
 
Unit ii(dsc++)
Durga Devi
 
Lec-4_Linked-List (1).pdf
KylaMaeGarcia1
 
UNIT 2LINKEDLISdddddddddddddddddddddddddddT.pptx
shesnasuneer
 
Linked Lists, Single Linked list and its operations
BackiyalakshmiVenkat
 

More from delicecogupdyke (20)

DOCX
You have been learning about Urie Bronfenbrenner this week- Let's mak.docx
delicecogupdyke
 
DOCX
You encounter a flat green organism growing on a rock with no obvious.docx
delicecogupdyke
 
DOCX
You are the PR specialist for St- John's Ambulance- The organization w.docx
delicecogupdyke
 
DOCX
You are the CFO of a large academic medical center and your organizati (1).docx
delicecogupdyke
 
DOCX
You are on a field trip with your bio class- Your water sample from a.docx
delicecogupdyke
 
DOCX
You are fortunate to travel to a tropical forest- You find an insect o.docx
delicecogupdyke
 
DOCX
You are a Government employee working in the Human Resources departmen.docx
delicecogupdyke
 
DOCX
You are a genetic cownselar- A single mother cnmes to vou- where both.docx
delicecogupdyke
 
DOCX
Write the following report that can be a powerpoint presentation-The g.docx
delicecogupdyke
 
DOCX
Write two functions using the Front and Back Linear Search algorithms-.docx
delicecogupdyke
 
DOCX
xpected to grow geometrically- (Assume that interactions ith other spe.docx
delicecogupdyke
 
DOCX
Write the pseudocode for Python for the following- from Celsius to Fah.docx
delicecogupdyke
 
DOCX
Write SQL commands that convert the Database schema to Tables with the.docx
delicecogupdyke
 
DOCX
write the adjusting entries for following Given Data-.docx
delicecogupdyke
 
DOCX
Write python code to collect 1000 posts from Twitter- or Facebook- or.docx
delicecogupdyke
 
DOCX
write one to two paragraphs on the significance about Jati Explain how.docx
delicecogupdyke
 
DOCX
Write in C++ Calculate the mean of a vector of floating point numbers.docx
delicecogupdyke
 
DOCX
Write down the code that will count the number of clicks for a LIKE bu.docx
delicecogupdyke
 
DOCX
Write declarations for variables p1 and p2 whose values will be addres.docx
delicecogupdyke
 
DOCX
Write an example of an organization which has formed an inter-organiza.docx
delicecogupdyke
 
You have been learning about Urie Bronfenbrenner this week- Let's mak.docx
delicecogupdyke
 
You encounter a flat green organism growing on a rock with no obvious.docx
delicecogupdyke
 
You are the PR specialist for St- John's Ambulance- The organization w.docx
delicecogupdyke
 
You are the CFO of a large academic medical center and your organizati (1).docx
delicecogupdyke
 
You are on a field trip with your bio class- Your water sample from a.docx
delicecogupdyke
 
You are fortunate to travel to a tropical forest- You find an insect o.docx
delicecogupdyke
 
You are a Government employee working in the Human Resources departmen.docx
delicecogupdyke
 
You are a genetic cownselar- A single mother cnmes to vou- where both.docx
delicecogupdyke
 
Write the following report that can be a powerpoint presentation-The g.docx
delicecogupdyke
 
Write two functions using the Front and Back Linear Search algorithms-.docx
delicecogupdyke
 
xpected to grow geometrically- (Assume that interactions ith other spe.docx
delicecogupdyke
 
Write the pseudocode for Python for the following- from Celsius to Fah.docx
delicecogupdyke
 
Write SQL commands that convert the Database schema to Tables with the.docx
delicecogupdyke
 
write the adjusting entries for following Given Data-.docx
delicecogupdyke
 
Write python code to collect 1000 posts from Twitter- or Facebook- or.docx
delicecogupdyke
 
write one to two paragraphs on the significance about Jati Explain how.docx
delicecogupdyke
 
Write in C++ Calculate the mean of a vector of floating point numbers.docx
delicecogupdyke
 
Write down the code that will count the number of clicks for a LIKE bu.docx
delicecogupdyke
 
Write declarations for variables p1 and p2 whose values will be addres.docx
delicecogupdyke
 
Write an example of an organization which has formed an inter-organiza.docx
delicecogupdyke
 

Recently uploaded (20)

PDF
QNL June Edition hosted by Pragya the official Quiz Club of the University of...
Pragya - UEM Kolkata Quiz Club
 
PDF
Aprendendo Arquitetura Framework Salesforce - Dia 03
Mauricio Alexandre Silva
 
PPTX
How to Create Odoo JS Dialog_Popup in Odoo 18
Celine George
 
PDF
DIGESTION OF CARBOHYDRATES,PROTEINS,LIPIDS
raviralanaresh2
 
PPT
Talk on Critical Theory, Part One, Philosophy of Social Sciences
Soraj Hongladarom
 
PPTX
How to Set Up Tags in Odoo 18 - Odoo Slides
Celine George
 
PPTX
care of patient with elimination needs.pptx
Rekhanjali Gupta
 
PPTX
PPT-Q1-WEEK-3-SCIENCE-ERevised Matatag Grade 3.pptx
reijhongidayawan02
 
PPTX
HUMAN RESOURCE MANAGEMENT: RECRUITMENT, SELECTION, PLACEMENT, DEPLOYMENT, TRA...
PRADEEP ABOTHU
 
PDF
Generative AI: it's STILL not a robot (CIJ Summer 2025)
Paul Bradshaw
 
PPTX
How to Handle Salesperson Commision in Odoo 18 Sales
Celine George
 
PPTX
Universal immunization Programme (UIP).pptx
Vishal Chanalia
 
PDF
CONCURSO DE POESIA “POETUFAS – PASSOS SUAVES PELO VERSO.pdf
Colégio Santa Teresinha
 
PDF
ARAL-Orientation_Morning-Session_Day-11.pdf
JoelVilloso1
 
PDF
Exploring the Different Types of Experimental Research
Thelma Villaflores
 
PPTX
grade 5 lesson matatag ENGLISH 5_Q1_PPT_WEEK4.pptx
SireQuinn
 
PPTX
ASRB NET 2023 PREVIOUS YEAR QUESTION PAPER GENETICS AND PLANT BREEDING BY SAT...
Krashi Coaching
 
PDF
The Constitution Review Committee (CRC) has released an updated schedule for ...
nservice241
 
PPTX
I AM MALALA The Girl Who Stood Up for Education and was Shot by the Taliban...
Beena E S
 
PPTX
How to Manage Large Scrollbar in Odoo 18 POS
Celine George
 
QNL June Edition hosted by Pragya the official Quiz Club of the University of...
Pragya - UEM Kolkata Quiz Club
 
Aprendendo Arquitetura Framework Salesforce - Dia 03
Mauricio Alexandre Silva
 
How to Create Odoo JS Dialog_Popup in Odoo 18
Celine George
 
DIGESTION OF CARBOHYDRATES,PROTEINS,LIPIDS
raviralanaresh2
 
Talk on Critical Theory, Part One, Philosophy of Social Sciences
Soraj Hongladarom
 
How to Set Up Tags in Odoo 18 - Odoo Slides
Celine George
 
care of patient with elimination needs.pptx
Rekhanjali Gupta
 
PPT-Q1-WEEK-3-SCIENCE-ERevised Matatag Grade 3.pptx
reijhongidayawan02
 
HUMAN RESOURCE MANAGEMENT: RECRUITMENT, SELECTION, PLACEMENT, DEPLOYMENT, TRA...
PRADEEP ABOTHU
 
Generative AI: it's STILL not a robot (CIJ Summer 2025)
Paul Bradshaw
 
How to Handle Salesperson Commision in Odoo 18 Sales
Celine George
 
Universal immunization Programme (UIP).pptx
Vishal Chanalia
 
CONCURSO DE POESIA “POETUFAS – PASSOS SUAVES PELO VERSO.pdf
Colégio Santa Teresinha
 
ARAL-Orientation_Morning-Session_Day-11.pdf
JoelVilloso1
 
Exploring the Different Types of Experimental Research
Thelma Villaflores
 
grade 5 lesson matatag ENGLISH 5_Q1_PPT_WEEK4.pptx
SireQuinn
 
ASRB NET 2023 PREVIOUS YEAR QUESTION PAPER GENETICS AND PLANT BREEDING BY SAT...
Krashi Coaching
 
The Constitution Review Committee (CRC) has released an updated schedule for ...
nservice241
 
I AM MALALA The Girl Who Stood Up for Education and was Shot by the Taliban...
Beena E S
 
How to Manage Large Scrollbar in Odoo 18 POS
Celine George
 

Write the definition of the linkedListKeepLast function- (Please write.docx

  • 1. Write the definition of the linkedListKeepLast function. (Please write the following in C, Thank you) The purpose of the function is to create a new linked list containing the last node in each linked list in the hash table beginning with the first linked list. The rest of the nodes are to be displayed to the screen, then returned to the heap. Example: If the user enters the following keys: 201, 102, 233, 567, 456, 654, 465, 645, quit list at index 0 is empty list at index 1 is not empty: 201 102 567 list at index 2 is not empty: 233 list at index 3 is empty list at index 4 is not empty: 456 654 465 645 Deleted nodes: --> at index 0: --> at index 1: 201 102
  • 2. --> at index 2: --> at index 3: --> at index 4: 456 654 465 The final linked list contains: 567 233 645 Written by: */ #include <stdio.h> #include <stdlib.h> // malloc(), free(), exit() #include <string.h> #define NUMPOINTERS 5 typedef struct node STUDENTREC; struct node { char id[10]; struct node *next; }; // Function Declarations int hash(char id[]); STUDENTREC *insert(char id[], STUDENTREC *student_body[],
  • 3. int hashval); void traverse(STUDENTREC *student_body[]); void displayLL(STUDENTREC *list, char *description); STUDENTREC *linkedListKeepLast(STUDENTREC *student_body[]); int main (void) { STUDENTREC *student_body[NUMPOINTERS] = {NULL}; STUDENTREC *person; STUDENTREC *endList; char id[10]; int hashval; printf(" ~*~ Hashing using collision resolution by chaining ~*~n"); printf("t Enter Student ID (or quit): "); scanf("%s", id); while(strcmp(id, "quit")) { hashval = hash(id); person = insert(id, student_body, hashval); if (person) // not NULL => duplicate { printf("Duplicate record!n"); } printf("t Enter Student ID (or quit): ");
  • 4. scanf("%s", id); } traverse(student_body); endList = linkedListKeepLast(student_body); displayLL(endList, "New List"); traverse(student_body); return 0; } /* The purpose of the function is to create a new linked list containing the last node in each linked list in the hash table beginning with the first linked list. The rest of the nodes are to be displayed to the screen, then returned to the heap. */ STUDENTREC *linkedListKeepLast(STUDENTREC *student_body[]) { STUDENTREC *newList = NULL; /* ********************************************************* Write your code here Get the job done without calling other linked list functions ********************************************************* */ return newList; } /***************************************************
  • 5. Hash Student ID by summing the cubes of the ASCII value of characters and then take the modulo of this sum. */ int hash(char id[]) { long sum = 0; while (*id) // != '0' { sum += *id * *id * *id; id++; } return sum % NUMPOINTERS; } /*************************************************** Insert a new Social Security number into the array of student records, at index equal to hashvalue */ STUDENTREC *insert(char id[], STUDENTREC *student_body[], int hashval) {
  • 6. STUDENTREC **mover; // Use ** to write elegant code mover = &student_body[hashval]; while (*mover) { if (strcmp(id,(*mover)->id) == 0) return *mover; mover = &((*mover)->next); } if ((*mover = (STUDENTREC *) malloc(sizeof(STUDENTREC))) == NULL) { printf("Malloc error in insert!n"); exit(1); } strcpy((*mover)->id, id); (*mover)->next = NULL; // set the link of the new node to NULL printf("%s has been placed in the list at location %d.n", (*mover)->id, hashval); return NULL; } /*************************************************** Traversing the lists in a hash table */ void traverse(STUDENTREC *student_body[]) { int i;
  • 7. // STUDENTREC **mover; // Use ** for fun and practice // not needed for traverse for (i = 0; i < NUMPOINTERS; i++) { printf("Contents of list %2dn", i); printf("--------------------n"); // for (mover = &student_body[i]; *mover; mover = &(*mover)->next) // { // &((*mover)->next) // printf("%sn", (*mover)->ss); // } displayLL(student_body[i], NULL); printf("n"); } } /*************************************************** Traversing a linked list */ void displayLL(STUDENTREC *list, char *description) { if (list) // != NULL { if (description) // != NULL {
  • 8. printf ("%sn", description); printf("--------------------n"); } STUDENTREC **mover; // Use ** for fun and practice // not needed for traverse for (mover = &list; *mover; mover = &(*mover)->next) { // &((*mover)->next) printf("%sn", (*mover)->id); } } else printf ("Empty!"); printf("n"); } /* ~*~ Hashing using collision resolution by chaining ~*~ Enter Student ID (or quit): 201 201 has been placed in the list at location 1. Enter Student ID (or quit): 102 102 has been placed in the list at location 1. Enter Student ID (or quit): 233 233 has been placed in the list at location 2. Enter Student ID (or quit): 567
  • 9. 567 has been placed in the list at location 1. Enter Student ID (or quit): 456 456 has been placed in the list at location 4. Enter Student ID (or quit): 654 654 has been placed in the list at location 4. Enter Student ID (or quit): 465 465 has been placed in the list at location 4. Enter Student ID (or quit): 645 645 has been placed in the list at location 4. Enter Student ID (or quit): quit Contents of list 0 -------------------- Empty! Contents of list 1 -------------------- 201 102 567 Contents of list 2 -------------------- 233 Contents of list 3 --------------------
  • 10. Empty! Contents of list 4 -------------------- 456 654 465 645 Deleted nodes: --> at index 0: --> at index 1: 201 102 --> at index 2: --> at index 3: --> at index 4: 456 654 465 New List -------------------- 567 233 645 Contents of list 0 -------------------- Empty! Contents of list 1 --------------------
  • 11. Empty! Contents of list 2 -------------------- Empty! Contents of list 3 -------------------- Empty! Contents of list 4 -------------------- Empty! */