DSA Notes by Inam Unit 1
DSA Notes by Inam Unit 1
DSA Notes by Inam Unit 1
Linear Non-Linear
Basic In this structure, the In this structure, the elements are
elements are arranged arranged hierarchically or non-
sequentially or linearly and linear manner.
attached to one another.
Types Arrays, linked list, stack, Trees and graphs are the types of a
queue are the types of a non-linear data structure.
linear data structure.
implementation Due to the linear Due to the non-linear organization,
organization, they are easy they are difficult to implement.
to implement.
Traversal It requires a single run to It requires multiple runs to be
traverse each data item. traversed.
Arrangement Each data item is attached Each item is attached to many other
to the previous and next items.
items.
Levels In this, the data elements In this, the data elements are
are organized in a single arranged in multiple levels.
level.
DSA Page 1
Data Structures and Algorithm – Unit 1
As applications are becoming more complex and the amount of data is increasing
day by day, which may cause problems with processing speed, searching data,
handling multiple requests etc. Data structure provides a way of organizing,
managing, and storing data efficiently. With the help of data structure, the data
items can be traversed easily. Data structure provides efficiency, reusability and
abstraction. It plays an important role in enhancing the performance of a program
because the main function of the program is to store and retrieve the user’s data
as fast as possible.
Learning data structure and algorithms will help you become a better programmer.
You will be able to write code that is more efficient and more reliable. You will also
be able to solve problems more quickly and more effectively.
#include <stdio.h>
int stack[100],i,j,choice=0,n,top=-1;
void push();
void pop();
void show();
void main ()
scanf("%d",&n);
DSA Page 2
Data Structures and Algorithm – Unit 1
printf("\n----------------------------------------------\n");
while(choice != 4)
printf("\n1.Push\n2.Pop\n3.Show\n4.Exit");
scanf("%d",&choice);
switch(choice)
case 1:
push();
break;
case 2:
pop();
break;
case 3:
show();
break;
case 4:
DSA Page 3
Data Structures and Algorithm – Unit 1
printf("Exiting....");
break;
default:
};
void push ()
int val;
if (top == n )
printf("\n Overflow");
else
scanf("%d",&val);
stack[top] = val;
DSA Page 4
Data Structures and Algorithm – Unit 1
void pop ()
if(top == -1)
printf("Underflow");
else
void show()
for (i=top;i>=0;i--)
printf("%d\n",stack[i]);
if(top == -1)
printf("Stack is empty");
peek()
Get the top data element of the stack, without removing it.
DSA Page 5
Data Structures and Algorithm – Unit 1
Example
int peek() {
return stack[top];
isfull()
Example
bool isfull() {
if(top == MAXSIZE)
return true;
else
return false;
isempty()
Example
bool isempty() {
if(top == -1)
return true;
else
return false;
Push Operation
The process of putting a new data element onto stack is known as a Push Operation.
DSA Page 6
Data Structures and Algorithm – Unit 1
Example
if(!isFull()) {
top = top + 1;
stack[top] = data;
} else {
Pop Operation
Accessing the content while removing it from the stack, is known as a Pop Operation.
Example
if(!isempty()) {
data = stack[top];
top = top - 1;
return data;
} else {
DSA Page 7
Data Structures and Algorithm – Unit 1
DSA Page 8
Data Structures and Algorithm – Unit 1
DSA Page 9
Data Structures and Algorithm – Unit 1
DSA Page 10
Data Structures and Algorithm – Unit 1
DSA Page 11