Stack Using Linked List Questions and Answers

Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

28/11/2023, 09:26 Stack using Linked List Questions and Answers - Sanfoundry

Data Structure Questions and Answers – Stack


using Linked List
This set of Data Structure Multiple Choice Questions & Answers (MCQs) focuses on “Stack using
Linked List”.

1. What is the best case time complexity of deleting a node in a Singly Linked list?
a) O (n)
b) O (n2)
c) O (nlogn)
d) O (1)
View Answer

2. Which of the following statements are not correct with respect to Singly Linked List(SLL) and
Doubly Linked List(DLL)?
a) Complexity of Insertion and Deletion at known position is O(n) in SLL and O(1) in DLL
b) SLL uses lesser memory per node than DLL
c) DLL has more searching power than SLL
d) Number of node fields in SLL is more than DLL
View Answer

3. Given below is the Node class to perform basic list operations and a Stack class with a no arg
constructor.
Select from the options the appropriate pop() operation that can be included in the Stack class. Also
‘first’ is the top-of-the-stack.

advertisement

https://www.sanfoundry.com/data-structure-questions-answers-stack-linked-list/ 1/11
28/11/2023, 09:26 Stack using Linked List Questions and Answers - Sanfoundry

class Node
{
protected Node next;
protected Object ele;
Node()
{
this(null,null);
}
Node(Object e,Node n)
{
ele=e;
next=n;
}
public void setNext(Node n)
{
next=n;
}
public void setEle(Object e)
{
ele=e;
}
public Node getNext()
{
return next;
}
public Object getEle()
{
return ele;
}
}

class Stack
{
Node first;
int size=0;
Stack()
{
first=null;

https://www.sanfoundry.com/data-structure-questions-answers-stack-linked-list/ 2/11
28/11/2023, 09:26 Stack using Linked List Questions and Answers - Sanfoundry

}
}

a)

Note: Join free Sanfoundry classes at Telegram or Youtube

public Object pop()


{
if(size == 0)
System.out.println("underflow");
else
{
Object o = first.getEle();
first = first.getNext();
size--;
return o;
}
}

b)

advertisement

public Object pop()


{
if(size == 0)
System.out.println("underflow");
else
{
Object o = first.getEle();

https://www.sanfoundry.com/data-structure-questions-answers-stack-linked-list/ 3/11
28/11/2023, 09:26 Stack using Linked List Questions and Answers - Sanfoundry

first = first.getNext().getNext();
size--;
return o;
}
}

c)

advertisement

public Object pop()


{
if(size == 0)
System.out.println("underflow");
else
{
first = first.getNext();
Object o = first.getEle();
size--;
return o;
}
}

d)

public Object pop()


{
if(size == 0)
System.out.println("underflow");
else
{
first = first.getNext().getNext();
Object o = first.getEle();
size--;
return o;

https://www.sanfoundry.com/data-structure-questions-answers-stack-linked-list/ 4/11
28/11/2023, 09:26 Stack using Linked List Questions and Answers - Sanfoundry

}
}

View Answer

4. What does the following function do?

public Object some_func()throws emptyStackException


{
if(isEmpty())
throw new emptyStackException("underflow");
return first.getEle();
}

a) pop
b) delete the top-of-the-stack element
c) retrieve the top-of-the-stack element
d) push operation
View Answer

5. What is the functionality of the following piece of code?

public void display()


{
if(size == 0)
System.out.println("underflow");
else
{
Node current = first;
while(current != null)
{
System.out.println(current.getEle());
current = current.getNext();
}
}
}

a) reverse the list


b) display the list
c) display the list excluding top-of-the-stack-element
d) reverse the list excluding top-of-the-stack-element
View Answer

6. What does ‘stack overflow’ refer to?


a) accessing item from an undefined stack
b) adding items to a full stack

https://www.sanfoundry.com/data-structure-questions-answers-stack-linked-list/ 5/11
28/11/2023, 09:26 Stack using Linked List Questions and Answers - Sanfoundry

c) removing items from an empty stack


d) index out of bounds exception
View Answer

7. Given below is the Node class to perform basic list operations and a Stack class with a no arg
constructor. Select from the options the appropriate push() operation that can be included in the
Stack class. Also ‘first’ is the top-of-the-stack.

class Node
{
protected Node next;
protected Object ele;
Node()
{
this(null,null);
}
Node(Object e,Node n)
{
ele=e;
next=n;
}
public void setNext(Node n)
{
next=n;
}
public void setEle(Object e)
{
ele=e;
}
public Node getNext()
{
return next;
}
public Object getEle()
{
return ele;
}
}

class Stack
{
Node first;
int size=0;
Stack()
{
first=null;
}
}

a)
https://www.sanfoundry.com/data-structure-questions-answers-stack-linked-list/ 6/11
28/11/2023, 09:26 Stack using Linked List Questions and Answers - Sanfoundry

public void push(Object item)


{
Node temp = new Node(item,first);
first = temp;
size++;
}

b)

public void push(Object item)


{
Node temp = new Node(item,first);
first = temp.getNext();
size++;
}

c)

public void push(Object item)


{
Node temp = new Node();
first = temp.getNext();
first.setItem(item);
size++;
}

d)

public void push(Object item)


{
Node temp = new Node();
first = temp.getNext.getNext();
first.setItem(item);
size++;
}

View Answer

8. Consider these functions:


push() : push an element into the stack
pop() : pop the top-of-the-stack element
top() : returns the item stored in top-of-the-stack-node
What will be the output after performing these sequence of operations

push(20);
push(4);
top();

https://www.sanfoundry.com/data-structure-questions-answers-stack-linked-list/ 7/11
28/11/2023, 09:26 Stack using Linked List Questions and Answers - Sanfoundry

pop();
pop();
push(5);
top();

a) 20
b) 4
c) stack underflow
d) 5
View Answer

9. Which of the following data structures can be used for parentheses matching?
a) n-ary tree
b) queue
c) priority queue
d) stack
View Answer

10. Minimum number of queues to implement stack is ___________


a) 3
b) 4
c) 1
d) 2
View Answer

Sanfoundry Global Education & Learning Series – Data Structure.

To practice all areas of Data Structure, here is complete set of 1000+ Multiple Choice Questions and
Answers.

« Prev - Data Structure Questions and Answers » Next - Data Structure Questions and Answers –
– Stack using Array Queue using Array

Related Posts:

Apply for Data Structure Internship


Practice Computer Science MCQs
Practice Design & Analysis of Algorithms MCQ
Check Programming Books
Practice Programming MCQs

https://www.sanfoundry.com/data-structure-questions-answers-stack-linked-list/ 8/11
28/11/2023, 09:26 Stack using Linked List Questions and Answers - Sanfoundry

Data Structure MCQ, DS MCQ - Abstract Datatype

advertisement

Recommended Articles:
1. Data Structure Questions and Answers – Doubly Linked List
2. Data Structure Multiple Choice Questions – Linked List
3. Data Structure Questions and Answers – Circular Linked List
4. Data Structure Questions and Answers – Singly Linked List Operations – 1
5. Data Structure Questions and Answers – Singly Linked List Operations – 3
6. Python Program to Implement Stack using Linked List
7. Data Structure Questions and Answers – Singly Linked List Operations – 2
8. Java Program to Implement Stack using Linked List
9. Data Structure Questions and Answers – Xor Linked List
10. Data Structure Questions and Answers – Queue using Linked List

advertisement

https://www.sanfoundry.com/data-structure-questions-answers-stack-linked-list/ 9/11
28/11/2023, 09:26 Stack using Linked List Questions and Answers - Sanfoundry

Additional Resources:
Linked List Programs in C
Linked List Programs in Python
Data Structure MCQ Questions
Data Structures in C
Data Structures in Java

Popular Pages:
Data Structures in C++
C Programs on Recursion
Data Science MCQ Questions
C Tutorial
Python Programming Examples

Subscribe: Data Structure Newsletter

Name

Email

Subscribe

Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to


get free Certificate of Merit. Join our social networks below and stay updated with latest contests,
videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest


https://www.sanfoundry.com/data-structure-questions-answers-stack-linked-list/ 10/11
28/11/2023, 09:26 Stack using Linked List Questions and Answers - Sanfoundry

Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is
Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on
development of Linux Kernel, SAN Technologies, Advanced C, Data
Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram


SanfoundryClasses.

About | Certifications | Internships | Jobs | Privacy Policy | Terms | Copyright | Contact

     

© 2011-2023 Sanfoundry. All Rights Reserved.

https://www.sanfoundry.com/data-structure-questions-answers-stack-linked-list/ 11/11

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy