15IT305 Data Structures and Algorithms
15IT305 Data Structures and Algorithms
DATA STRUCTURES AND ALGORITHMS
Encapsulation
The user need not worry out implementation of the operation
Flexibility
The operations may be implemented differently for different
applications.
PRIMARY ADTs
• LIST
• STACK
• QUEUE
SO2-Linked List ADT
A linked list is a series of connected nodes (or links) where each
node is a data structure.
Head
a1 a2 a3 a4
Memory
Address 1000 800 712 992
*
Advantages of Linked Lists over Arrays
Linked lists are more complex to code and manage than arrays, but
they have some distinct advantages.
a) linked list can easily grow and shrink in size
The programmer doesn’t need to know how many nodes will
be in the list. They are created in memory as needed.
b) speed of insertion or deletion from the list
Inserting and deleting elements into and out of arrays
requires
moving elements of the array. When a node is inserted, or
deleted from a linked list, none of the other nodes have to be
moved.
LISTof elements
Collection
Types
pHead pTail
struct Node {
double data; // data
a
struct Node* next; // pointer to next
};
“self-referentiality”
*
Linked list representation
#include<stdio.h> int main()
#include<stdlib.h> {
struct node int i,n,x;
{ void insert(int);
void display();
int data; printf("list size is\n");
struct node* link; scanf("%d",&n);
}; printf("Enter the array values\n");
struct node* head; for(i=0;i<n;i++)
{
scanf("%d",&x);
insert(x);
}
display();
getch();
return 0;
}
void insert(int a)
{
struct node* temp=(node*)malloc(sizeof(struct node));
temp->data=a;
temp->link=head;
head=temp;
}
void display()
{
struct node* t=head;
while(t!= NULL)
{
printf("%d\t",t->data);
t=t->link;
} }
FAQ -1
1.A linear collection of data elements where the linear node is
given by means of pointer is called
(A) linked list (B) node list (C) primitive list
(D) None of these
a)O(1)
b)O(n)
c)O(n2
d) None
FAQ 2
Struct node
{
int data;
struct node *prevptr;
struct node *nextptr;
};
struct node *root=null;
Insertion routine
Display routine
Circular Linked List
A Circular Linked List is a special type of Linked List
It supports traversing from the end of the list to the beginning
by making the last node point back to the head of the list
A Rear pointer is often used instead of a Head pointer
Applications of List
• Polynomials
• Radix sort
• Multilist
Polynomials
• A polynomial is a sum of terms. Each term consists of a
coefficient and a (common) variable raised to an
exponent.
• perform operations on polynomials.
Example: 4x3 + 5x – 10
struct node
float coefficient;
int exponent;
struct node *next;
Operations on polynomials
Adding polynomials
(3x5 – 9x3 + 4x2) + (–8x5 + 8x3 + 2)
= 3x5 – 8x5 – 9x3 + 8x3 + 4x2 + 2
= –5x5 – x3 + 4x2 + 2
Multiplying polynomials
(2x – 3)*(2x2 + 3x – 2)
= 2x(2x2 + 3x – 2) – 3(2x2 + 3x – 2)
= 4x3 + 6x2 – 4x – 6x2 – 9x+ 6
= 4x3 – 13x+ 6
Linked List representation
Radix sort
• repeatedly sorts by digit—perform multiple
bucket sorts on S starting with the rightmost
digit
• If maximum value is 999999, only ten buckets
(not 1 million) will be necessary
58
12 37 18
20 52 63 64 36 47 88 99
20 12 52 63 64 36 37 47 58 18 88 9 99
Example: second pass
20 12 52 63 64 36 37 47 58 18 88 9 99
12 36 52 63
18 20 37 47 58 64 88 99
9 12 18 20 36 37 47 52 58 63 64 88 99
Example: 1 and 2 passes
st nd
12 58 37 64 52 36 99 63 18 9 20 88 47
20 12 52 63 64 36 37 47 58 18 88 9 99
9 12 18 20 36 37 47 52 58 63 64 88 99
FAQ -1
1.To insert a new node in linked list free node will be available
in ........
A. Available list B. Avail list C. Free node list
D. Memory space list
FAQ 2
1. In a circular linked list
struct node
int data;
struct node * next;
a) ptr=(NODE*)malloc(sizeof(NODE));
b) ptr=(NODE*)malloc(NODE);
c) ptr=(NODE*)malloc(sizeof(NODE*));
d) ptr=(NODE)malloc(sizeof(NODE));
STACK ADT
STACK
Stack operation
The push operation
The push operation inserts an item at the top of the stack.
The following shows the format.
Push operation
The pop operation
The pop operation deletes the item at the top of the stack.
The following shows the format.
Pop operation
The empty operation
The empty operation checks the status of the stack. The
following shows the format.
•Balancing Symbols
•Infix to Postfix
•Evaluation of expression
•Recursion
STACK USING ARRAYS
do {
# include <stdio.h>
printf("[1].PUSH [2].POP
# include <conio.h>
[3].Display [4].Exit");
# define SIZE 10
printf("Enter your choice
int arr[SIZE], top = -1, i ; [1-4] : ") ;
void push() ; scanf("%d", &ch) ;
void pop() ; switch(ch)
void display() ; {
void main() case 1 :
{ push() ;
int ch ; break ;
clrscr() ; case 2 :
pop() ;
break ;
case 3 :
display() ;
break ;
case 4 :
break ;
default :
printf("Invalid option") ;
getch() ;
}
} while(ch != 4) ;
getch() ;
}
Push operation
void push()
{
if(top == SIZE - 1)
{
printf("Stack is full (overflow)") ;
getch() ;
return ;
}
top++ ;
printf("Enter the element to PUSH : ") ;
scanf("%d", &arr[top]) ;
}
Pop operation
void pop()
{
if(top == -1)
{
printf("Stack is empty (underflow)") ;
getch() ;
return ;
}
printf("The POP element is : %d", arr[top]) ;
getch() ;
top-- ;
}
Display operation
void display()
{
if(top == -1)
{
printf("Stack is empty (underflow)") ;
getch() ;
return ;
}
printf("The elements in stack are :TOP") ;
for(i = top ; i >= 0 ; i--)
printf(" -> %d", arr[i]) ;
getch() ;
}
Assessment
1. In what order the elements of a pushdown stack are
accessed
a. LILO b. LIFO c. FIFO d. None
2. The operation for removing an entry from a stack is
traditionally called:
A. delete B. peek C. pop D. remove
3. The operation for adding an entry to a stack is
traditionally called:
A. add B. append C. insert D. push
4. Which of the following stack operations could result in
stack underflow?
A. is_empty B. pop C. push d. None
IMPLEMENTATION OF STACK
USING LINKED LIST
List Stack Example
top
6
List Stack Example
top
6
List Stack Example
top 7
6
List Stack Example
top 7
6
List Stack Example
top 7
6
List Stack Example
top 7
6
Linked List Implementation of
stack
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
Struct node
{
char data;
node *next;
};
node *top=NULL,*newnode,*temp;
PUSH & POP Routine
char pop()
Void push(char data) {
{ if(top==NULL)
newnode=(node return ‘\0’;
*)malloc(sizeof(node)); else
newnode->data=data; {
char ch=top->data;
newnode->next=top;
temp=top;
top=newnode; top=top->next;
} free(temp);
return ch; }
}
Display & Main Routine
void display()
void main()
{
if(top==NULL) {
printf(“\n The stack has no
data\n”); push(a);
else push(b);
{
temp=top; display();
While(temp!=NULL) pop();
{
display();
printf(“%c\n”,temp->data”);
temp=temp-
push(c);
>next; push(d);
}
} display(); }
}
Algorithm for Infix to Postfix conversion
postfixVect
Infix to postfix conversion
stackVect
infixVect
a+b-c)*d–(e+f)
postfixVect
(
Infix to postfix conversion
stackVect
infixVect
+b-c)*d–(e+f)
postfixVect
a
(
Infix to postfix conversion
stackVect
infixVect
b-c)*d–(e+f)
postfixVect
a
+
(
Infix to postfix conversion
stackVect
infixVect
-c)*d–(e+f)
postfixVect
ab
+
(
Infix to postfix conversion
stackVect
infixVect
c)*d–(e+f)
postfixVect
ab+
-
(
Infix to postfix conversion
stackVect
infixVect
)*d–(e+f)
postfixVect
ab+c
-
(
Infix to postfix conversion
stackVect
infixVect
*d–(e+f)
postfixVect
ab+c-
Infix to postfix conversion
stackVect
infixVect
d–(e+f)
postfixVect
ab+c-
*
Infix to postfix conversion
stackVect
infixVect
–(e+f)
postfixVect
ab+c-d
*
Infix to postfix conversion
stackVect
infixVect
(e+f)
postfixVect
ab+c–d*
-
Infix to postfix conversion
stackVect
infixVect
e+f)
postfixVect
ab+c–d*
(
-
Infix to postfix conversion
stackVect
infixVect
+f)
postfixVect
ab+c–d*e
(
-
Infix to postfix conversion
stackVect
infixVect
f)
postfixVect
+ ab+c–d*e
(
-
Infix to postfix conversion
stackVect
infixVect
)
postfixVect
+ ab+c–d*ef
(
-
Infix to postfix conversion
stackVect
infixVect
postfixVect
ab+c–d*ef+
-
Infix to postfix conversion
stackVect
infixVect
postfixVect
ab+c–d*ef+-
Algorithm for evaluating postfix
expressions
Start scanning the postfix expressions from left to right
If operand is encountered then push it on to the stack
If an operator is encountered, pop last two operands
from the stack, apply the operator on the operands
and then push the resultant value on to the stack
Finally the stack must contain the single value which is
the result
1. 5 6 2 + * 12 4 / -
Ans: 37
2. 6 5 2 3 + 8 * + 3 + *
Ans: 288
3. (A*B)+(C/D) where A=3, B=2, C=25, D=5)
Ans:11
SO1-QUEUES
10
7
null
head_ptr
tail_ptr
Linked List Implementation
15
10
7
null
head_ptr
tail_ptr
Linked List Implementation
10
7
null
head_ptr
tail_ptr
Rear
LINKED LIST IMPLEMENTATION OF QUEUE
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
Struct qnode
{
Char data;
Qnode *next;
};
Qnode *front=NULL,*rear=NULL,*newnode,*temp;
ENQUEUE ROUTINE
Void enqueue(char data)
{
newnode=(qnode*)malloc(sizeof(qnode));
newnode->data=data;
newnode->next=NULL;
if(front==NULL&&rear==NULL)
{
front=rear=newnode;
}
CONTD..
Void main()
{
enqueue(a);
enqueue(b);
display();
}