We developed a system that stores the entry history to a room per user ID for se
ID: 3905811 • Letter: W
Question
We developed a system that stores the entry history to a room per user ID for security reasons. The system prints the number of entries of each user in an increasing order in terms of user ID. The following output shows an example of the program for the recent six entries, where -1 indicates the end of history. ? ./a.out Enter id: 6927238 Enter id: 3428529 Enter id: 2800555 Enter id: 2900123 Enter id: 3428529 Enter id: 6927238 Enter id: -1 id: 2800555 (1) id: 2900123 (1) id: 3428529 (2) id: 6927238 (2)Explanation / Answer
Below is the C code I hope that i have provided sufficient comments for your better understanding
void record_entry(int id)
{
Nodeptr n;
Nodeptr p = head , prev=NULL;
//list is empty initially
if(p==NULL)
{
n=(Nodeptr)malloc(sizeof(struct node));
n->id=id;
n->count=1;
n->next=NULL;
head=n;
}
else
{
while(p!=NULL&&id>p->id)
{
prev=p;
p=p->next;
}
//New id to be inserted is the highest
if(p==NULL)
{
n=(Nodeptr)malloc(sizeof(struct node));
n->id=id;
n->count=1;
n->next=NULL;
prev->next=n;
}
//New id to be inserted is the lowest
else if(prev==NULL)
{
n=(Nodeptr)malloc(sizeof(struct node));
n->id=id;
n->count=1;
n->next=head;
//make this as the head node
head=n;
}
//This id is already present hence...
//... increment the count by 1
else if(id==p->id)
{
p->count++;
}
//Add the node between "prev" and "p"
else
{
n=(Nodeptr)malloc(sizeof(struct node));
n->id=id;
n->count=1;
n->next=p;
prev->next=n;
}
}
}
Hope i have answered your question satisfactorily.Leave doubts in comment section if any.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.