Can some experts please fix my codes by following the examples below!?!? Impleme
ID: 3841956 • Letter: C
Question
Can some experts please fix my codes by following the examples below!?!?
Implementing a locker using Queue signly linked list!!
#include
#include
#include
typedef struct node_elm* node;
struct node_elm {
int item;
int id;
node next;
};
typedef struct list_queue* listq;
struct list_queue {
node head, tail;
int size;
};
listq queueInit(void){
listq Q = (listq) malloc( sizeof(struct list_queue) );
Q->head = Q->tail = NULL;
Q->size = 0;
return Q;
}
node insertAtHead(node head, int item){
node new_node = (node)malloc( sizeof(struct node_elm));
new_node->item = item;
new_node->next=head;
return new_node;
}
void enqueue(listq Q,int item) {
Q->head= insertAtHead(Q->head, item);
}
void enqueue1(listq Q,int id)
{
Q->head->id = id;
}
node deleteFromTail(node head, int *item){
node tail, tmp = NULL;
if (head == NULL){
printf("List is empty. ");
return NULL;
}
for (tail=head; tail->next != NULL; tail=tail->next)
tmp=tail;
if (tmp!=NULL) tmp->next=NULL;
*item=tail->item;
free(tail);
if(head ==tail)
return NULL;
else return head;
}
int dequeue(listq Q)
{
node temp = (node)malloc(sizeof(struct node_elm));
int a = Q->head->item;
temp=deleteFromTail(Q->head,&a);
if(temp==NULL)
{
printf(" Queue is Empty. ");
}
return a;
}
void printList(listq Q)
{
node v = Q->head;
while(v != NULL){
printf("Locker: %d ", v->item);
v = v->next;
}
}
int main()
{
int temp = 1;
listq Q;
node head = NULL;
Q = queueInit();
char input[50];
char command[20];
int val1;
int count = 0;
while(1)
{
scanf(" %49[^ ]s", input);
sscanf(input, "%s %d", &command, &val1);
if(strcmp(command, "CREATE") == 0)
{
enqueue(Q,temp);
printf("New Locker created: %d ", temp);
temp++;
} else if(strcmp(command, "DELETE") == 0)
{
printf("Deleted the locker, %d ",dequeue(Q));
} else if(strcmp(command, "ATTACH") == 0)
{
//Here is the problem!!
} else if(strcmp(command, "QUERYALL") == 0)
{
printList(Q);
} else if(strcmp(command, "QUIT") == 0)
{
printf("Good Bye! ");
exit(0);
}
}
}
What I wanna do is, when I input "CREATE", the locker should be created from 1 to n. This works well. However, for
CREATE - Creates a locker
DELETE - Decommissions a locker
QUERY - Queries a locker and retrieves information
QUERYALL - Queries all lockers and retrieve their information
LOCK - Locks a locker
UNLOCK - Unlocks a locker
ATTACH - Adds an owner to a locker, gets locker at head of the queue
DETACH - Removes an owner from a locker
QUIT - Deletes all lockers and quits the program
I have difficulties in implementing the other commands... please help me!
Example:
Example 1 Create Locker:
Example 2 Query Locker 1:
Example 3 Unlock/Lock:
Example 4 Attach:
Example 5 DELETE:
Example 6 QUERALL example:
Explanation / Answer
#include<stdlib.h>
#include<stdio.h>
#include<pthread.h>
typedef struct node_elm* node;
struct node_elm {
int item;
int id;
char owner[100] =””; //
node next;
pthread_mutex_t lock;
};
typedef struct list_queue* listq;
struct list_queue {
node head, tail;
int size;
};
listq queueInit(void){
listq Q = (listq) malloc( sizeof(struct list_queue) );
Q->head = Q->tail = NULL;
Q->size = 0;
return Q;
}
node insertAtHead(node head, int item){
node new_node = (node)malloc( sizeof(struct node_elm));
new_node->item = item;
new_node->next=head;
return new_node;
}
void enqueue(listq Q,int item) {
Q->head= insertAtHead(Q->head, item);
}
void enqueue1(listq Q,int id)
{
Q->head->id = id;
}
void attachowner (listq Q,char *owner)
{
Int node_id;
Printf (“Enter the node to attach owner--”);
scanf(input, "%s", &node_id);
findnode(listq Q, int node_id);
void findnode(listq Q, int node_id)
{
struct node *prev = head;
while(prev->next != NULL && prev->next != n)
prev = prev->next;
//attach the owner to a particular node ID
if(prev->next==node_id)
{
Qàprev->owner=owner[50];
}
// Check if node really exists in Linked List
if(prev->next == NULL)
{
printf(" Given node is not present in Linked List");
return;
}
}
}
Lock(node head,node *n)
{
Pthread_mutex_lock(&n->lock);
}
Unlock (node head,node *n)
{
Pthread_mutex_unlock(&n->lock);
}
node deleteFromTail(node head, int *item){
node tail, tmp = NULL;
if (head == NULL){
printf("List is empty. ");
return NULL;
}
for (tail=head; tail->next != NULL; tail=tail->next)
tmp=tail;
if (tmp!=NULL) tmp->next=NULL;
*item=tail->item;
free(tail);
if(head ==tail)
return NULL;
else return head;
}
int dequeue(listq Q)
{
node temp = (node)malloc(sizeof(struct node_elm));
int a = Q->head->item;
temp=deleteFromTail(Q->head,&a);
if(temp==NULL)
{
printf(" Queue is Empty. ");
}
return a;
}
void printList(listq Q)
{
node v = Q->head;
while(v != NULL){
printf("Locker: %d ", v->item);
v = v->next;
}
}
int main()
{
int temp = 1;
listq Q;
node head = NULL;
Q = queueInit();
char input[50];
char command[20];
char owner[50];
int val1;
int count = 0;
while(1)
{
scanf(" %49[^ ]s", input);
sscanf(input, "%s %d", &command, &val1);
if(strcmp(command, "CREATE") == 0)
{
enqueue(Q,temp);
printf("New Locker created: %d ", temp);
temp++;
} else if(strcmp(command, "DELETE") == 0)
{
printf("Deleted the locker, %d ",dequeue(Q));
} else if(strcmp(command, "ATTACH") == 0)
{
attachowner(Q,char *owner)
printf("New owner created");
} else if(strcmp(command, "QUERYALL") == 0)
{
printList(Q);
} else if(strcmp(command, "QUIT") == 0)
{
printf("Good Bye! ");
exit(0);
}
else if(strcmp(command, "LOCK") == 0)
{
Lock(Q);
} else if(strcmp(command, "UNLOCK") == 0)
{
Unlock(Q)
}
}
}