Question 4: On the D2L site for this course, in the Module 2 section, I\'ve post
ID: 3738090 • Letter: Q
Question
Question 4: On the D2L site for this course, in the Module 2 section, I've posted files called LLStack.java and Stack. java. LLStackim plements the Stack interface defined in Stack.java, using the LinkedList code that we worked on in class. Without changing the LinkedList API (that is, without changing anything in LinkedList.java), how could you implement the following two functions in LLStack? (Keep in mind that, LLStack has no direct access to the Node structure in LinkedList.) // peek0 returns the top element of the stack, / leaving the stack unchanged. public T peek0 { // exchange0 exchanges the top two elements // of the stack. public void exchange0Explanation / Answer
int isEmpty(struct Node* top)
{
return top == NULL;
}
int peek(struct Node *top)
{
if (!isEmpty(top))
return top->data;
else
return -1;
}
void exchange(struct node* top)
{
int a= top.peek();
top.pop();
int b=top.peek();
top.pop();
top.push(a);
top.push(b);
}
void push(int value)
{
struct Node *newNode;
newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;
if(top == NULL)
newNode->next = NULL;
else
newNode->next = top;
top = newNode;
}
void pop()
{
if(top == NULL)
return;
else{
struct Node *temp = top;
top = temp->next;
free(temp);
}
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.