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

Please use C language and give some explanations please. Thank you! We\'re going

ID: 3764616 • Letter: P

Question

Please use C language and give some explanations please. Thank you!

We're going to simulate the line at Starbucks using a linked list. In computer science (and British English), the Starbucks line is an example of a queue: a list where things are added at one end and removed at the other. We're going to use the linked list structure and functions we wrote in class this week and add an additional function: delete from the back. Write a function deleteFromEnd that takes a list struct (should it be by pointer? Would you ever need to modify the list struct?) and deletes the last element in the list. In main, test your new function by making a list, adding people to the front, and deleting the end of it, printing the list after each operation.

Here is a sample output (you can hardcode a sequence of people arriving/leaving inside main)

customer arrived

1

customer arrived

2

1

customer arrived

3

2

1

customer served

3

2

customer arrived

4

3

2

customer served

4

3

customer served

4

customer served

Explanation / Answer

Here is the code for you. If you need any further modifications, just get back to me.

#include<stdio.h>

typedef struct node
{
int info;
struct node *next;
}node;
int insert(node **, int);
int delete(node **);
int display(node *);
int main()
{
node *head;
int ch,ele,result;
head=NULL;
insert(&head, 1);
printf("customer arrived. ");
display(head);
insert(&head, 2);
printf("customer arrived. ");
display(head);
insert(&head, 3);
printf("customer arrived. ");
display(head);
delete(&head);
printf("customer served. ");
display(head);
insert(&head, 4);
printf("customer arrived. ");
display(head);
delete(&head);
printf("customer served. ");
display(head);
delete(&head);
printf("customer served. ");
display(head);
delete(&head);
printf("customer served. ");
display(head);
}

int insert(node **head,int ele)
{
node *temp;
temp=malloc(sizeof(node));
temp->info=ele;
temp->next=*head;
*head=temp;
}

int delete(node **head)
{
node *x,*y;
int ele;
if(*head==NULL)
return -1;
else if((*head)->next==NULL)
{
x=*head;
ele=x->info;
*head=NULL;
free(x);
return ele;
}
else
{
y=x=*head;
while(x->next!=NULL)
{
y=x;
x=x->next;
}
ele=x->info;
y->next=NULL;
free(x);
return ele;
}
}
int display(node *head)
{
node *x;
x=head;
if(head==NULL)
return -1;
else
{
while(x!=NULL)
{
printf("%d ",x->info);
x=x->next;
}
return 1;
}
}