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

Stack is a linear data structure which follows a

particular order in which the operations are


performed. The order may be LIFO (Last In First Out)
or FILO (First In Last Out).
The following are some common operations
implemented on the stack:

o push(): When we insert an element in a stack


then the operation is known as a push. If the
stack is full then the overflow condition occurs.

o pop(): When we delete an element from the


stack, the operation is known as a pop. If the
stack is empty means that no element exists in
the stack, this state is known as an underflow
state.

o isEmpty(): It determines whether the stack is


empty or not.
o isFull(): It determines whether the stack is full or
not.'

o peek(): It returns the element at the given


position.

o count(): It returns the total number of elements


available in a stack.

o change(): It changes the element at the given


position.

o display(): It prints all the elements available in


the stack.
PUSH operation
The steps involved in the PUSH operation is given
below:

Before inserting an element in a stack, we check whether


the stack is full.

If we try to insert the element in a stack, and the stack is


full, then the overflow condition occurs.

When we initialize a stack, we set the value of top as -1 to


check that the stack is empty.

When the new element is pushed in a stack, first, the value


of the top gets incremented, i.e., top=top+1, and the
element will be placed at the new position of the top.

The elements will be inserted until we reach the max size


of the stack.

: - push(), pop(), isEmpty() and peek() all take O(1)


time. We do not run any loop in any of these
operations.
POP operation
The steps involved in the POP operation is given
below:

Before deleting the element from the stack, we check


whether the stack is empty.

If we try to delete the element from the empty stack, then


the underflow condition occurs.

If the stack is not empty, we first access the element which


is pointed by the top.

Once the pop operation is performed, the top is


decremented by 1, i.e., top=top-1.

Average time complexity;

Data structure Access Search Insertion Deletion

Array O(1) O(N) O(N) O(N)

Stack O(N) O(N) O(1) O(1)

Time Complexity: It is defined as the number of times a


particular instruction set is executed rather than the total
time is taken. 
Algorithm for PUSH Operation:
A simple algorithm for Push operation can be derived as
follows −
begin procedure push: stack, data

if stack is full
return null
endif
top ← top + 1
stack[top] ← data

end procedure

Algorithm for Pop Operation:


A simple algorithm for Pop operation can be derived as
follows −
begin procedure pop: stack

if stack is empty
return null
endif
data ← stack[top]
top ← top - 1
return data

end procedure
peek():
Algorithm of peek() function −
begin procedure peek
return stack[top]
end procedure

isfull():
Algorithm of isfull() function −
begin procedure isfull

if top equals to MAXSIZE


return true
else
return false
endif
end procedure

isempty():
Algorithm of isempty() function −
begin procedure isempty

if top less than 1


return true
else
return false
endif
end procedure
OVERFLOW-
When the stack is full and you still try to push an
element in, the condition is called stack overflow.

UNDERFLOW-
When the stack is empty and an element is popped
of the stack, the condition is called stack underflow.

Infix expression
An infix expression is an expression in which
operators (+, -, *, /) are written between the two
operands.

Postfix Expression
The postfix operator also contains operator and
operands. In the postfix expression, the operator is
written after the operand. It is also known
as Reverse Polish Notation.
Algorithm to convert infix expression to postfix
expression: -
1. Initialize the Stack.
2. Scan the operator from left to right in the infix
expression.
3. If the leftmost character is an operand, set it as the
current output to the Postfix string.
4. And if the scanned character is the operator and the
Stack is empty or contains the '(', ')' symbol, push the
operator into the Stack.
5. If the scanned operator has higher precedence than
the existing precedence operator in the Stack or if the
Stack is empty, put it on the Stack.
6. If the scanned operator has lower precedence than
the existing operator in the Stack, pop all the Stack
operators. After that, push the scanned operator into
the Stack.
7. If the scanned character is a left bracket '(', push it
into the Stack.
8. If we encountered right bracket ')', pop the Stack and
print all output string character until '(' is encountered
and discard both the bracket.
9. Repeat all steps from 2 to 8 until the infix expression
is scanned.
10. Print the Stack output.
11. Pop and output all characters, including the
operator, from the Stack until it is not empty.

STRINGS-
Strings are defined as an array of characters. The
difference between a character array and a string is
the string is terminated with a special character ‘\0’.
char str_name[size];

Arrays of Strings-
An array of strings is just a two-dimensional array
of characters.
1. strcpy : strcpy copies a string, including the null character
terminator from the source string to the destination. This
function returns a pointer to the destination string. Its
prototype is : 

char *strcpy(char *dst, const char *src) ;

2. strncpy : strncpy is similar to strcpy, but it allows the


number of characters to be copied to be specified. 

char *strncpy(char *dst, const char *src, size_t len) ;

3. strcat : This function appends a source string to the end


of a destination string. This function returns a pointer to
the destination string, or NULL pointer on error. Its
prototype is : 

char *strcat(char *dst, const char *src) ;

4. strncat : This function appends at most N characters


from the source string to the end of the destination
string. This function returns a pointer to the destination
string, or a NULL pointer on error. Its prototype is : 

char *strncat(char *dst, const char *src, size_t N) ;

5. strcmp : Two strings are compred with this function. If


the first string is greater than the second, it returns a
number greater than zero. If the second string is greater,
it returns a number less than zero. If the strings are
equal, it returns 0. Its prototype is : 
int strcmp(const char *first, const char *second) ;

6. strncmp : This function compares the first N characters


of each string. If the first string is greater than the second,
it returns a number greater than zero. If the second string
is greater, it returns a number less than zero. If the
strings are equal, it returns 0. Its prototype is : 

int strncmp(const char *first, const char *second, size_T N) ;

7. strlen : This function returns the length of a string, not


counting the null character at the end. That is, it returns
the character count of the string, without the terminator.
Its prototype is: 

size_t strlen(const char *str) ;

8. memset : memset is useful to initialize a string to all


nulls, or to any character.

void *memset(const void *dst, int c, site_t N) ;

9. trtok : The strtok function is used to find the next token


in a string. The token is specified by a list of possible
delimiters.

You might also like