0% found this document useful (0 votes)
34 views9 pages

WordPile and Stack Data Structures

stack implementation

Uploaded by

Piyush Sarswat
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views9 pages

WordPile and Stack Data Structures

stack implementation

Uploaded by

Piyush Sarswat
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

DATA STRUCTURE (Stack)

1. WordPile is an entity which can hold maximum of 20 characters. The restriction is that a character
can be added or removed from one end only. Some of the members of classes are given below:-
Class name : WordPile
Data Members/instance variables:
ch[ ] : character array to hold the character elements
capacity : integer variable to store the maximum capacity
top : to point to the index of the topmost element
Member functions/methods:
WordPile(int cap) : constructor to initialize the data member capacity=cap, top=-1
and create the WordPile
void pushChar(char v) : adds the character to the top of WordPile if possible, otherwise outputs
a message “WordPile is full”
char popChar( ) : returns the deleted character from the topof the WordPile if possible,
otherwise returns„\\‟

Specify the class WordPile giving the details of the constructor, void pushChar(char) and char
popChar(). The main function and algorithm need not be written.

class WordPile
{
char ch[];
int capacity,top;

WordPile(int cap)
{
capacity=cap;
top=-1;
ch=new char[capacity];
}

void pushChar(char v)
{
if(top<capacity-1)
ch[++top]=v;
else
[Link]("WordPile is full");
}

char popChar()
{
if(top>-1)
return ch[top--];
else
return '\\';
}
}
2. Stack is a kind of data structure which can store elements with the restriction that an element can be
added or removed from the top only. The details of class Stack are given below:

Class name : Stack


st[ ] : the array to hold names.
size : the maximum capacity of the string array
top : the index of the topmost element of the stack
ctr : to count the number of elements of the stack
Member functions
Stack( ) : default constructor
Stack(int cap) : constructor to initialize size=cap and top=-1
void pushname(String n) : to push a name into the stack. If the stack is full, display the
message “overflow”.
String popname( ) : removes a name from the top of the stack and returns It. If the stack is empty,
display the message “underflow”.
void display( ) : Display the elements of the stack.

Specify class Stack giving details of the constructors(), void pushname(String n), String
popname() and void display(). The main function and algorithm need not be written.

class Stack
{
String st[];
int size,top,ctr;
Stack()
{
size=0;
top=0;
ctr=0;
}

Stack(int cap)
{
size=cap;
top=-1;
st=new String[size];
}

void pushname(String n)
{
if(top<size-1)
{
st[++top]=n;
ctr++;
}
else
[Link]("overflow");
}
String popname()
{
if(top>-1)
{
ctr--;
return st[top--];
}
else
return "underflow";
}

void display()
{
[Link]("Stack elements are : ");
for(int i=0;i<=top;i++)
{
[Link](st[i]);
}
}
}
3. A bookshelf is designed to store the books in a stack with LIFO (Last In First Out) operation.
Define a class Book with the following specifications:

class name : Book


Data members/instance variables:
name[ ] : stores the names of the books
point : stores the index of the topmost book
max : stores the maximum capacity of the bookshelf
Member Functions/instance variables:
Book(int cap) : constructor to initialize the data members max = cap and point =-1
void tell( ) : displays the name of the book which was last entered in the shelf.
If there is no book left in the shelf, displays the message “Shelf Empty”.
void add(String v) : adds the name of the book to the shelf if possible, otherwise displays the
message “Shelf full”.
void display( ) : displays all the names of the books available in the shelf.

Specify the class Book giving details of ONLY the functions void tell( ) and void add(String).
Assume that the other functions have been defined. The main function need not be written.
class Book
{
String name[];
int point,max;

Book(int cap)
{
max=cap;
point=-1;
name=new String[max];
}
void tell()
{
if(point>-1)
[Link]("Last book entered = "+name[point--]);
else
[Link]("Shelf Empty");
}

void add(String v)
{
if(point<max-1)
name[++point]=v;
else
[Link]("Shelf full");
}

void display()
{
[Link]("Stack elements are : ");
for(int i=0;i<=point;i++)
{
[Link](name[i]);
}
}
}

4. In a computer game, a vertical column and a pile of rings are displayed. The objective of the game is
to pile up rings on the column till it is full. It can hold 10 rings at the most. Once the column is full, the
rings have to be removed from the top till the column is empty and the game is over.
Define the class RingGame with the following details:

Class Name : RingGame


Data members/instance variables:
ring[ ] : array to hold rings (integer)
max : integer to hold maximum capacity of ring array
upper : integer to point to the uppermost element
Member functions/methods:
RingGame(int m) : constructor to initialize, max = m & upper to -1.
void jump-in(int) : adds a ring to the top of the column, if possible, otherwise, displays a
message “Column full. Start removing rings”.
void jump-out( ) : removes the ring from the top, if column is not empty, otherwise outputs a
message, “Congratulations the game is over “.

Specify the class RingGame giving details of the constructor and functions void jump-in(int) and void
jump-out(). Also define the main function to create an object and call methods accordingly to enable
the task.
import [Link].*;
class RingGame
{
int ring[];
int max,upper;

RingGame(int m)
{
max=m;
upper=-1;
ring=new int[upper];
}

void jump_in(int v)
{
if(upper<max-1)
ring[++upper]=v;
else
[Link]("Column full, Start removing rings");
}

void jump_out()
{
if(upper>-1)
[Link]("Removed Ring = "+ring[upper--]);
else
[Link]("Congratulation the game is over");
}

public static void main()


{
Scanner sc=new Scanner([Link]);
[Link]("Enter the number of rings");
int size=[Link]();
RingGame obj=new RingGame(size);
[Link]("Enter the Ring number");
int vv=[Link]();
obj.jump_in(vv);
obj.jump_out();
}
}
DATA STRUCTURE (Queue)

5. Queue is a an entity which can hold a maximum of 100 integers. The queue enables the user to add
integers
from the rear and remove integers from the front.

Class Name : Queue


Data members/instance variables:
Que[ ] : array to hold the integer elements stores the size of the array.
size : to point the size of array
front : to point the index of the array
rear : to point the index of the rear
Member functions:
Queue(int mm) : constructor to initialize the data size=mm, front=0, rear=0;
void addele(int v) : to add integer from the rear if possible else display the message
“Overflow”.
int delete( ) : returns elements from front if present, otherwise displays the message
“Underflow” and return -9999.
void display( ) : displays the array elements

Specify the class Queue giving details of ONLY the functions void addele (int) and int delete( ).
Assume that the other functions have been defined.

The main function and algorithm need not be written.

6. Define a class Repeat which allows the user to add elements from one end (rear) and remove elements
from one end (rear) and remove elements from the other end (front) only.

The following details of the class Repeat are given below:


Class name : Repeat
Data members/instance variable:
st[ ] : an array to hold a maximum of 100 integer elements
cap : stores the capacity of the array
f : to point to the index of the front
r : to point to the index of the rear
Member functions/methods:
Repeat(int m) : constructor to initialize the data members cap=m, f=0, r=0 and to create the
integer array
void pushvalue(int v) : to add integers from the rear index if possible else display the message
“overflow”
int popvalue( ) : to remove and return element from the front, if array is empty then return -9999
void disp( ) : displays the elements present in the list

Specify the class Repeat giving details of the constructor(int), member function void pushvalue(int),
int popvalue() and void disp().
The main function need not be written.
7. NIC institute‟s resource manager has decided to network the computer resources like printer, storage
media, etc. so that minimum resources and maximum sharing could be availed. Accordingly printers are
linked to a centralized system and the printing jobs are done on a „first come first served basis, only.
This
is like the first person‟s printing job will get done first and the next person‟s job will be done as the next
job in the list and so on. In order to avoid collision, the restriction is that no more than 20 printing jobs
can be added.
Define the class Printjob with the following details:
Class name : Printjob
Data members/instance variable:
job[ ] : array of integers to hold the printing jobs.
Newjob : to add a new printing job into the array.
Capacity : the maximum capacity of the integer array.
Front : to point out to the index of the front.
Rear : to point out to the index of the last.
Member functions/Methods:
Printjob( ) : constructor to initialize data members. Capacity=20, Front = Rear=-1 and call
the
function createJob().
void createJob( ) : to create an array to hold the printing jobs.
void addJob( ) : adds the new printing job to the end of the last printing job, if possible,
otherwise
displays the message “Printjob is full, cannot add any more”.
void removeJob() : Removes the printing job from the front if the printing job is not empty,
otherwise
displays the message “Printjob is empty”.

Specify the class Printjob giving details of the constructor and the function void addJob(), void
createJob()
and void removeJob() only. You do not need to write the main function.

8.

Programming Solution Point


PIYUSH SIR (8273651685)
DATA STRUCTURE (Link List)

9. Link is an entity which can hold a maximum of 100 integers. Link enables the user to add elements
from
the rear end and remove integers from the front end of the entity. Define a class Link with the following
details:
Class Name : Link
Data members/instance variable:
lnk[] : entity to hold the integer elements.
max : stores the maximum capacity of the entity.
Begin : to point to the index of the front end.
end : to point to the index of the rear end.
Member functions/Methods:
Link(int mm) : constructor to initialize max=mm, begin=0, end=0.
voidaddlink(int v) : to add an element from the rear index if possible otherwise display the message
“ OUT OF SIZE “.
intdellink( ) : to remove and return an element from the front index, if possible otherwise
display the message “ EMPTY… “and return -99.
void display( ) : displays the elements of the entity.

Specify the class Link giving details of the constructor(int ), void addlink(int ), intdellink() and void
display().
THE MAIN FUNCTION AND ALGORITHM NEED NOT BE WRITTEN.

Programming Solution Point


PIYUSH SIR (82736516

DATA STRUCTURE (double queue)

10. A doubly queue is a linear data structure which enables the user to add and remove integers from
either
ends, i.e. from front or rear. Define a class Dequeue with the following details:
Class name : Dequeue
Data members/instance variables:
arr[ ] : array to hold upto 100 integer elements
lim : stores the limit of the dequeue
front : to point to the index of front end
rear : to point to the index of rear end
Member function/Methods:
Dequeue(int l) : constructor to initialise the data members lim=l, front=rear=0
void addfront(int val) : to add integer from the front if possible else display the message (“Overflow
from

front”)
void addrear(int val) : to add integer from the rear if possible else display the message (“Overflow from
rear”)
int popfront( ) : returns element from front, if possible otherwise returns -9999.
int poprear( ) : returns element from rear, if possible otherwise returns -9999.

Specify class Dequeue giving details of the constructor(), void addfront(int), void addrear(int), int
popfront( ), and int poprear( ).
The main function and algorithm need not be written.

11. A dequeue enables the user to add and remove integers from both the ends i.e. front and rear.
Define a class DeQueue with the following details:
Class name : Dequeue
Data members/instance variables:
ele [] : array to hold integer elements.
cap : stores the maximum capacity of the array.
front : to point the index of the front.
rear : to point the index of the rear.
Member functions/methods:
Dequeue(int max) : constructor to inititalize the data member cap = max, front = rear = 0 and
create
the integer array.
voidpushfront(int v) : to add integers from the front index, if possible else display the message
(“full from front”).
intpopfront( ) : to remove and return element from front. If array is empty then return -
999.
voidpushrear(int v) : to add integers from the front index if possible else display the message
(“full from rear”).
intpoprear( ) : to remove and return elements from rear. If the array is empty then return
-999.

Specify the class Dequeue giving details only the constructor(int), void pushfront(int) and intpoprear().
Assume that other functions have been defined.
The main function need not be written.

Programming Solution Point


PIYUSH SIR (8273651685)

Common questions

Powered by AI

They allow efficient resource management by determining the order of operations. Stacks enable efficient function call management, maintaining states through LIFO. Queues optimize fair resource usage via FIFO, suitable for buffered resource sharing such as print jobs. Dequeues merge benefits of stacks and queues, providing flexible data handling suitable for scenarios needing removals or additions at both ends .

Stacks operate on a Last-In, First-Out (LIFO) basis, allowing push and pop operations only at the top. In contrast, queues work on a First-In, First-Out (FIFO) basis, allowing elements to be added at the rear and removed from the front .

Member functions provide robust operation and management interfaces for data structures, encapsulating complexity and reducing user error. Stack’s push, pop, and display methods streamline LIFO operations, while Queue’s addele and delete facilitate FIFO with clear overflow/underflow management. Such abstraction promotes clean, reusable, and reliable code, optimizing performance and usability .

In both stack and queue implementations, an overflow condition arises when trying to add an element into a full structure. The stack signals overflow by outputting "overflow" when an item is pushed past its capacity. Similarly, the queue signals overflow with the message "Overflow" when attempting to add an element to a full queue .

In a stack, the 'top' pointer indicates the most recent position where a new element was added and where the next will be removed from under LIFO order. Contrastingly, a queue uses 'front' to track where elements are removed and 'rear' to indicate where new elements are added under FIFO order .

In the Link class, 'begin' and 'end' pointers facilitate sequential access by marking insertion and removal points. 'End' tracks where new elements come in, whereas 'begin' leads in removals. This coordination maintains possible consistent FIFO behavior for efficient access and management of elements while preserving order .

The Dequeue class allows addition and removal of elements from both ends —the front and rear, while the standard Queue restricts these operations to add at the rear and remove from the front. Dequeue supports methods like addfront(), addrear(), popfront(), and poprear(), demonstrating its double-ended capabilities, in contrast to the more restrictive FIFO approach of a standard Queue .

The RingGame class manages capacity by declaring a 'max' variable to set the maximum capacity of rings it can hold and using an 'upper' pointer set initially at -1. During insertion, it checks if 'upper' has reached 'max-1' to decide whether to allow additional rings or prompt an overflow message .

The Book class's tell() method uses "Shelf Empty" as an error message when trying to access a non-existent book. While clear, the message could be improved for actionable feedback or return a specific error code alongside for programmatic handling, enhancing clarity in contexts where user intervention is required .

Both WordPile and Stack manage their elements in a LIFO manner using an array and a 'top' pointer. WordPile manages characters while Stack manages strings. Both check for overflow before adding an element. WordPile's overflow message is "WordPile is full," while Stack's is simply "overflow". Also, WordPile uses a capacity variable at initialization, whereas Stack may use different initialization methods .

You might also like