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

***USING C LANGUAGE*** Stackula\'s Deliverv Marquis Stackula is making some deli

ID: 3906279 • Letter: #

Question

***USING C LANGUAGE***

Stackula's Deliverv Marquis Stackula is making some deliveries of boxes of dirt and needs your assistance. Each night Stackula receives a list of pick-up and drop-off locations for various packages. His job is simple: Stackula goes to a house, picks up their dirt, goes to the destination, and drops it off as Sadly, no. Each place has two boxes to deliver (and potentially to different locations). Worse of Stackula can only carry all the boxes in one stack and accessing boxes in the middle is tough, so he will only ever deliver boxes to a location, if they are on the top of Stackula's stack. Unfortunately Stackula's customers have no sympathy. When Stackula shows up to a place the patrons take the boxes from the top of Stackula's (if they are the reciptants) and immediately place BOTH of their outgoing boxes on top of Stackula's stack. Stackula is not very tall, so Stackula cannot always see the destination of the top box. Your job is to construct a list of destinations for Stackula, so that Stackula knows the correct order to visit the houses. Stackula is quite tired and does not wish to sight see, so do not output routes that contain superfluous stops. Additionally Stackula will always want to head towards the city which has its box on the top of Stackula's stack, even if there is a more "optimized" order Problem Given a list of stops with pick-up and drop-off locations output the order in which Stackula's stack based storage system would visit Input Specificatiorn The first line of input contains a single integer n (2 Sn25), which represents the number of locations for delivery. Each of the following n lines contains 3 strings which describe each location. For each location description the first string is the name of the corresponding location. The second string represents the destination of the first box placed on Stackula's stack upon arrival. The third string represents the destination of the second box placed on Stackula's stack upon arrival It is guaranteed that each location will be visited before Stackula's stack is permently emptied (after visiting the first location) The name of each location will be composed strictly of at most 19 upper and lowercase Latin letters No locations will share the same name (and no location will be listed twice) . . No location will deliver to itself The first location Stackula visits (with no boxes) is the first city described in input, which should be the first location listed in the output Output Specification For the given delivery list output the list of cities in the order they will be visited

Explanation / Answer

#include<stdio.h>
#include<string.h>

//for linked list node
typedef struct node_{
char city[20];
struct node_ *next;
}Node;


//for pushing into the stack
void push(Node **head,char *city){
Node *cur = (Node *)malloc(sizeof(Node));
cur->next = *head;
*head = cur;
strcpy(cur->city,city);
}

//for poping out the stack
void pop(Node **head){
Node *cur = head;
*head =(*head)->next;
free(cur);
}

//for printing the Node
void printNode(Node *cur){
printf(" %s",cur->city);
}

int main(){
char cities[25][3][20];
int visited[25] = {0};
int num = 0;
Node *stackulaBox = NULL;
int boxTop = 0;

scanf("%d",&num);
int i;
for(i = 0;i<num;i++){
  scanf("%s",&cities[i][0]);
  scanf("%s",&cities[i][1]);
  scanf("%s",&cities[i][2]);
}
int st;
i = 0;
int j;
printf(" %s",cities[0][0]);
push(&stackulaBox,cities[i][1]);
boxTop++;
push(&stackulaBox,cities[i][2]);
boxTop++;
visited[i] = 1;
while(boxTop != 0){
  for(j = 0;j<num;j++){
   if(strcmp(stackulaBox->city,cities[j][0]) == 0){
    i = j;
    printNode(stackulaBox);
    pop(&stackulaBox);
    boxTop--;
    if(strcmp(stackulaBox,cities[j][0]) == 0){
     pop(&stackulaBox);
     boxTop--;
    }
    break;
   }
  }
  if(visited[i] == 0){
   push(&stackulaBox,cities[i][1]);
   boxTop++;
   push(&stackulaBox,cities[i][2]);
   boxTop++;
   visited[i] = 1;
  }
}
}