Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

DSA Notes by Inam Unit 1

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

Data Structures and Algorithm – Unit 1

Differentiate between primitive and non-primitive data structures


with examples.

Primitive data structure Non-primitive data structure


Primitive data structure is a kind of data Non-primitive data structure is a type of data
structure that stores the data of only structure that can store the data of more
one type. than one type.
Examples of primitive data structure are Examples of non-primitive data structure are
integer, character, float. Array, Linked list, stack.
Primitive data structure will contain Non-primitive data structure can consist of a
some value, i.e., it cannot be NULL. NULL value.
The size depends on the type of the In case of non-primitive data structure, size is
data structure. not fixed.
It starts with a lowercase character. It starts with an uppercase character.
Primitive data structure can be used to Non-primitive data structure cannot be used
call the methods. to call the methods.

Differentiate between linear and non-linear data structures with


examples.

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

Memory In this, the memory In this, memory is utilized in a very


utilization utilization is not efficient. efficient manner.
Applications Linear data structures are Non-linear data structures are used
mainly used for developing in image processing and Artificial
the software. Intelligence.

Determine importance of the data structures?

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.

Construct a code to implement stack using array.

#include <stdio.h>

int stack[100],i,j,choice=0,n,top=-1;

void push();

void pop();

void show();

void main ()

printf("Enter the number of elements in the stack ");

scanf("%d",&n);

printf("*********Stack operations using array*********");

DSA Page 2
Data Structures and Algorithm – Unit 1

printf("\n----------------------------------------------\n");

while(choice != 4)

printf("Chose one from the below options...\n");

printf("\n1.Push\n2.Pop\n3.Show\n4.Exit");

printf("\n Enter your choice \n");

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:

printf("Please Enter valid choice ");

};

void push ()

int val;

if (top == n )

printf("\n Overflow");

else

printf("Enter the value?");

scanf("%d",&val);

top = top +1;

stack[top] = val;

DSA Page 4
Data Structures and Algorithm – Unit 1

void pop ()

if(top == -1)

printf("Underflow");

else

top = top -1;

void show()

for (i=top;i>=0;i--)

printf("%d\n",stack[i]);

if(top == -1)

printf("Stack is empty");

Identify operations performed in Stack and Explain.

 push() − Pushing (storing) an element on the stack.


 pop() − Removing (accessing) an element from the stack.
 peek() − get the top data element of the stack, without removing it.
 isFull() − check if stack is full.
 isEmpty() − check if 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()

Check if stack is full.

Example

bool isfull() {

if(top == MAXSIZE)

return true;

else

return false;

isempty()

Check if stack is empty.

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

void push(int data) {

if(!isFull()) {

top = top + 1;

stack[top] = data;

} else {

printf("Could not insert data, Stack is full.\n");

Pop Operation

Accessing the content while removing it from the stack, is known as a Pop Operation.

Example

int pop(int data) {

if(!isempty()) {

data = stack[top];

top = top - 1;

return data;

} else {

printf("Could not retrieve data, Stack is empty.\n");

Implement a Program in C for Converting an Infix Expression to


Postfix Expression. (Refer Lab Program)

Implement code for Evaluating a Postfix Expression using stack.


(Refer Lab Program)

DSA Page 7
Data Structures and Algorithm – Unit 1

Convert below given infix expression to postfix and trace using


stack data structures. A+(B*C-(D/E^F)*G)*H)

DSA Page 8
Data Structures and Algorithm – Unit 1

DSA Page 9
Data Structures and Algorithm – Unit 1

Design an algorithm for evaluating the postfix expression. Trace the


same for the following expression using stack data structures: 6 2 3
+ - 3 82 / + * 2 ^3 +

Algorithm to evaluate the postfix expression:

1. Create a stack to store operands.


2. Scan the given expression from left to right.
3. a) If the scanned character is an operand, push it into the stack.

b) If the scanned character is an operator, POP 2 operands from stack and


perform operation and PUSH the result back to the stack.

4. Repeat step 3 till all the characters are scanned.


5. When the expression is ended, the number in the stack is the final result.

DSA Page 10
Data Structures and Algorithm – Unit 1

DSA Page 11

You might also like