Question

In: Computer Science

Time limit: 5000ms Memory limit: 256mb Description: Given a series of stack operations, please complete the...

Time limit: 5000ms
Memory limit: 256mb

Description:
Given a series of stack operations, please complete the stack ADT to output for respective operations.

-------------------------Copy the following code, complete it and submit-------------------------
#include <stdio.h>
#include <stdlib.h>

typedef enum {push = 1, pop, end} Operation;

typedef struct Node * PtrToNode;

struct Node {
    int element;
    PtrToNode next;
    PtrToNode prev;
};

typedef struct ListRecord * List;

struct ListRecord {
    PtrToNode head;
    PtrToNode tail;
};

List Create() {
    List L;
    L = (List)malloc(sizeof(struct ListRecord));
    L->head = (PtrToNode)malloc(sizeof(struct Node));
    L->head->prev = NULL;
    L->tail = (PtrToNode)malloc(sizeof(struct Node));
    L->head->next = L->tail;
    L->tail->prev = L->head;
    L->tail->next = NULL;
    return L;
}

void Insertion(int x, List L) {
    // write your code here
}

int Deletion(List L) {
    // write your code here
}

Operation Getop() {
    char str[7];
    scanf("%s", str);
    if (strcmp(str, "Push") == 0) {
        return 1;
    }
    else if (strcmp(str, "Pop") == 0) {
        return 2;
    }
    else {
        return 3;
    }
}

void printList(List L) {
    PtrToNode ptr;
    ptr = L->head->next;
    while(ptr != L->tail) {
        printf("%d ", ptr->element);
        ptr = ptr->next;
    }
}

int main(void) {
    int X;
    List L;
    int flag = 0;

    L = Create();
    while (!flag) {
        switch(Getop()) {
            case push:
                scanf("%d", &X);
                Insertion(X, L);
                break;
            case pop:
                X = Deletion(L);
                if (X == 1e5) {
                    printf("Stack is empty\n");
                }
                break;
            case end:
                printList(L);
                flag = 1;
                break;
        }
    }
    return 0;
}

-------------------------------------------End of Code-------------------------------------------

Input:
M lines of stack operations, each chosen from one of the followings: Push x(an integer) and Pop, except the last of which, which is End.

Output
A line per Pop (if stack is empty) operation, denoting the stack is empty, following by a line of remaining elements in the stack from bottom to top upon End

Sample Input 1:
Pop
Push 1
Push 2
Pop
End

Sample Output 1:
Stack is Empty
1

Sample Input 2:
Pop
Push 1
End

Sample Output 2:
Stack is empty
1

Solutions

Expert Solution

Note:

Used the code provided in the Question and updated the code that you need.

The code in the Bold format is the Updated code.

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

typedef enum {push = 1, pop, end} Operation;

typedef struct Node * PtrToNode;

struct Node {
int element;
PtrToNode next;
PtrToNode prev;
};

typedef struct ListRecord * List;

struct ListRecord {
PtrToNode head;
PtrToNode tail;
};

List Create() {
List L;
L = (List)malloc(sizeof(struct ListRecord));
L->head = (PtrToNode)malloc(sizeof(struct Node));
L->head->prev = NULL;
L->tail = (PtrToNode)malloc(sizeof(struct Node));
L->head->next = L->tail;
L->tail->prev = L->head;
L->tail->next = NULL;
return L;
}

void Insertion(int x, List L) {
// write your code here
   L->head->element = x;
   struct Node* new_Node = (struct Node*)malloc(sizeof(struct Node));
   new_Node->next = L->head;
   new_Node->prev = NULL;
   L->head = new_Node;
}

int Deletion(List L) {
// write your code here
   if(L->head->next == L->tail)
   {
       return 1e5;
   }
   else
   {
       struct Node* new_Node = L->head->next;
       L->head->next = new_Node->next;
       return 1;
   }
}

Operation Getop() {
char str[7];
scanf("%s", str);
if (strcmp(str, "Push") == 0) {
return 1;
}
else if (strcmp(str, "Pop") == 0) {
return 2;
}
else {
return 3;
}
}

void printList(List L) {
PtrToNode ptr;
ptr = L->head->next;
while(ptr != L->tail) {
printf("%d ", ptr->element);
ptr = ptr->next;
}
}

int main(void) {
int X;
List L;
int flag = 0;

L = Create();
while (!flag) {
switch(Getop()) {
case push:
scanf("%d", &X);
Insertion(X, L);
break;
case pop:
X = Deletion(L);
if (X == 1e5) {
printf("Stack is empty\n");
}
break;
case end:
printList(L);
flag = 1;
break;
}
}
return 0;
}


Related Solutions

C Programme Time limit: 5000ms Memory limit: 256mb Description: Given a series of queue operations, please...
C Programme Time limit: 5000ms Memory limit: 256mb Description: Given a series of queue operations, please complete the queue ADT to output for respective operations. -------------------------Copy the following code, complete it and submit------------------------- #include <stdio.h> #include <stdlib.h> #include <string.h> typedef enum {enqueue = 1, dequeue, end, isfull, isempty} Operation; struct Queue { int * arr; int capacity; int front; int rear; }; struct Queue * Create(int capacity) { struct Queue * queue = (struct Queue *)malloc(sizeof(struct Queue)); queue->arr = (int*)malloc(sizeof(int)...
Stack Time Limit : 1 sec, Memory Limit : 131072 KB English / Japanese   Reverse Polish...
Stack Time Limit : 1 sec, Memory Limit : 131072 KB English / Japanese   Reverse Polish notation is a notation where every operator follows all of its operands. For example, an expression (1+2)*(5+4) in the conventional Polish notation can be represented as 1 2 + 5 4 + * in the Reverse Polish notation. One of advantages of the Reverse Polish notation is that it is parenthesis-free. Write a program which reads an expression in the Reverse Polish notation and...
Please complete the following list of assignments and upload using this link. Please limit the number...
Please complete the following list of assignments and upload using this link. Please limit the number of files you create to a max of 2, one excel and one word document. P 1-1 FASB Statement of Financial Accounting Concepts No. 2 indicates several qualitative characteristics of useful accounting information. Following is a list of some of these qualities, as well as a list of statements and phrases describing the qualities. a. Benefits > costs b. Decision usefulness c. Relevance d....
C++ Please complete based on the code below. Declare another stack object to the code in...
C++ Please complete based on the code below. Declare another stack object to the code in main(). Add a stack operator called CopyStack to the Stack class which, when executed, copies the contents of the first stack into the second stack. Modify your menu so that this option is available. The menu should also allow the second stack to be printed, pushed, popped, and so forth, just like with the first stack. #include using namespace std; #define MAXSize 10 class...
How do we define the stack ADT, including its operations? Typed please.
How do we define the stack ADT, including its operations? Typed please.
Refer to the gasoline sales time series data in the given table.
Refer to the gasoline sales time series data in the given table. Week Sales (1000s of gallons) 1 17 2 21 3 19 4 24 5 19 6 15 7 21 8 19 9 23 10 19 11 15 12 22 Compute four-week and five-week moving averages for the time series. Round your answers to two decimal places. Week Sales 4-WeekMoving Average 5-WeekMoving Average 1 17     2 21     3 19     4 24    ...
Refer to the gasoline sales time series data in the given table.
Refer to the gasoline sales time series data in the given table. a. Compute four-week and five-week moving averages for the time series. Round your answers to two decimal places.b. Compute the MSE for the four-week and five-week moving average forecasts. Round your intermediate calculations and final answers to two decimal places. MSE for four-week moving average = MSE for five-week moving average = c. What appears to be the best number of weeks of past data (three, four, or five) to use in...
Please complete absolutely follow the requirements. Thanks! Implement a stack ADT by writing a class called...
Please complete absolutely follow the requirements. Thanks! Implement a stack ADT by writing a class called Stack. Use a static array to hold stack elements. Instantiate the Stack class in the main function and provide a user loop and a menu so that all the Stack class member-functions, push, pop, etc., are available so that the user can thoroughly exercise the member-functions of the Stack class. Also, implement a ReversePrint() for the stack. My StackProject, whose exposition I have given...
Is momentum conserved in all of the experiments? Please give a complete description with examples as...
Is momentum conserved in all of the experiments? Please give a complete description with examples as needed supporting your response. 2.Is kinetic energy conserved in all of the experiments? Please give a complete description with examples as needed supporting your response. Does friction affect this experiment? If so, how? If not, why not? If the track were not perfectly level, how would your results change if at all? How might this experiment be improved to illustrate better the concepts of...
Compare and Contrast Hash Map to Binary Search Tree. Memory, Time Efficiency for Operations for Retrieving...
Compare and Contrast Hash Map to Binary Search Tree. Memory, Time Efficiency for Operations for Retrieving Singular Values, Maintaining relationships between data. Please be sure to use your own words.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT