Beruflich Dokumente
Kultur Dokumente
ers.com
Engaging Peers, Inspiring Careers!
Data Structures
-- Data processing often involves in processing huge volumes of data. Many Companies handle million
records of data stored in database. Many ways are formulated to handle data efficiently.
-- An User-defined data type is a combination of different primary data types, which represents a
complex entity.
-- An Abstract Data Type ( A D T ) not only represents a set of complex data objects, but also includes a
set of operations to be performed on these objects, defines that how the data objects are organized.
-- The group of methods implements a set rules, which defines a logical way of handling data.
-- The complex entity along with its group of methods is called Abstract Data Type ( A D T ) .
-- Data structure is described as an instance of Abstract Data Type ( ADT ).
-- We can define that Data structure is a kind of representation of logical relationship between related
data elements. In data structure, decision on the operations such as storage, retrieval and access must be
carried out between the logically related data elements.
Data Structure
Linear
Linear Lists
Stacks
Non-Linear
Queues
Trees
Graphs
4)
5)
Limitations of Arrays
Fixed in size :
Once an array is created, the size of array
cannot be increased or decreased.
Wastage of space :
If no. of elements are less, leads to wastage of
space.
Sequential Storage :
Array elements are stored in contiguous
memory locations. At the times it might so
happen that enough contiguous locations might
not be available. Even though the total space
requirement of an array can be met through a
combination of non-contiguous blocks of
memory, we would still not be allowed to create
the array.
Possibility of overflow :
If program ever needs to process more than
the size of array, there is a possibility of
overflow and code breaks.
Difficulty in insertion and deletion :
In case of insertion of a new element, each
element after the specified location has to be
shifted one position to the right. In case of
deletion of an element, each element after the
specified location has to be shifted one position
to the left.
struct node {
int rollno; struct node *next;
};
int main() {
struct node *head,*n1,*n2,*n3,*n4;
/* creating a new node */
n1=(struct node *) malloc(sizeof(struct node));
n1->rollno=101;
n1->next = NULL;
/* referencing the first node to head pointer
*/
head = n1;
/* creating a new node */
n2=(struct node *)malloc(sizeof(struct node));
n2->rollno=102;
n2->next = NULL;
/* linking the second node after first node */
n1->next = n2;
/* creating a new node * /
n3=(struct node *)malloc(sizeof(struct node));
n3->rollno=104;
n3->next=NULL;
/* linking the third node after second node */
n2->next = n3;
/* creating a new node */
n4=(struct node *)malloc (sizeof (struct node));
n4->rollno=103;
n4->next=NULL;
/* inserting the new node between
second node and third node */
n2->next = n4;
101
NULL
head
150
n1-node
720
102
101
150
720
n2-node
150
n1-node
150
101
720
102
101
400
head
150
n1-node
102
910
720
n2-node
150
n1-node
150
NULL
104 NULL
910
n3-node
720
400
n2-node
104 NULL
103
910
910
n3-node
720
n4-node
150
101
head
150
103
720
n1-node
102
720
400
n2-node
910
720
n4-node
104 NULL
910
n3-node
}
}
void insert_after(struct node **h) {
struct node *new,*temp; int k;
if(*h == NULL) return;
printf("\nEnter data of node after which node : ");
scanf("%d",&k);
temp = *h;
while(temp!=NULL && temp->data!=k)
temp = temp->next;
if(temp!=NULL) {
new=createnode();
new->next = temp->next;
temp->next = new;
}
}
void insert_before(struct node **h) {
struct node *new,*temp,*prev ;
int k;
if(*h==NULL) return;
printf("\nEnter data of node before which node :
");
scanf("%d",&k);
if((*h)->data == k) {
new = createnode();
new->next = *h;
*h = new; return;
}
temp = (*h)->next; prev = *h;
if(temp!=NULL) {
prev->next = temp->next;
free(temp);
}
}
void search(struct node *h) {
struct node *temp;
int k;
if(h==NULL)return;
printf("\nEnter the data to be searched : ");
scanf("%d",&k);
temp=h;
while(temp!=NULL && temp->data!=k)
temp=temp->next;
(temp==NULL)?
printf("\n\t=>Node does not exist") :
printf("\n\t=>Node exists");
}
void destroy(struct node **h) {
struct node *p;
if(*h==NULL) return;
while(*h!=NULL) {
p = (*h)->next;
free(*h);
*h=p;
}
printf("\n\n ******Linked List is destroyed******");
}
head
tail
A
/* a node in doubly linked list */
struct node
{
struct node *prev;
int data ;
struct node *next;
}
C
B
D
C
B
p
q
B
p
D
int main() {
struct node *head=NULL;
int ch;
while(1) {
printf("\n1.Append");
printf("\n2.Display All");
printf("\n3.Insert after a specified node");
printf("\n4.Insert before a specified node");
printf("\n5.Delete a node");
printf("\n6.Search for a node");
printf("\n7.Distroy the list");
printf("\n8.Exit program");
printf("\n\n\tEnter your choice : ");
scanf("%d",&ch);
switch(ch) {
case 1:append(&head);break;
case 2:forward_display(head);break;
case 3:insert_after(&head);break;
case 4:insert_before(&head);break;
case 5:delnode(&head);break;
case 6:search(head);break;
case 7:destroy(&head);break;
case 8:exit(0);break;
default :
printf("Wrong Choice, Enter correct choice :
");
}
}
}
101
Circular Singly
Linked List
400
150
n1-node
102
720
400
n2-node
103
910
104 150
720
n3-node
910
n4-node
910
tail
-- Singly Linked List has a major drawback. From a specified node, it is not possible to reach any of
the preceding nodes in the list. To overcome the drawback, a small change is made to the SLL so that
the next field of the last node is pointing to the first node rather than NULL. Such a linked list is called a
circular linked list.
-- Because it is a circular linked list, it is possible to reach any node in the list from a particular node.
-- There is no natural first node or last node because by virtue of the list is circular.
-- Therefore, one convention is to let the external pointer of the circular linked list, tail, point to the last
node and to allow the following node to be the first node.
-- If the tail pointer refers to NULL, means the circular linked list is empty.
data
next
prev
data
next
prev
data
next
prev
data
next
-- A Circular Doubly Linked List ( CDL ) is a doubly linked list with first node linked to last node and
vice-versa.
-- The prev link of first node contains the address of last node and next link of last node contains
the address of first node.
-- Traversal through Circular Singly Linked List is possible only in one direction.
-- The main advantage of Circular Doubly Linked List ( CDL ) is that, a node can be inserted into list
without searching the complete list for finding the address of previous node.
-- We can also traversed through CDL in both directions, from first node to last node and vice-versa.
Stacks
-- Stack is an ordered collection of data elements into which new elements may be inserted and from
which elements may be deleted at one end called the TOP of stack.
-- A stack is a last-in-first-out ( LIFO ) structure.
-- Insertion operation is referred as PUSH and deletion operation is referred as POP.
-- The most accessible element in the stack is the element at the position TOP.
-- Stack must be created as empty.
-- Whenever an element is pushed into stack, it must be checked whether the stack is full or not.
-- Whenever an element is popped form stack, it must be checked whether the stack is empty or not.
-- We can implement the stack ADT either with array or linked list.
Applications of stack
Reversing Data series
Conversion decimal to binary
Parsing into tokens
Backtracking the operations
Undo operations in Text Editor
Page visited History in web browser
Tracking of Function calls
Maintaining scope and lifetime of local
variables in functions
Infix to postfix conversion
Evaluating postfix expression
Stack ADT
struct stackNode {
int data; struct stackNode *next;
};
init_stack( )
push ( )
pop ( )
isEmpty ( )
display ( )
peek ( )
Pop( )
Pop( )
Pop( )
e
c
a
Operations on Stack
Operation
1. Init_stack( )
2. Push( a )
3. Push( b )
4. Push( c )
5. Pop( )
6. Push( d )
7. Push( e )
8. Pop( )
9. Pop( )
10. Pop( )
11. Pop( )
Stacks contents
<empty>
a
ab
abc
ab
abd
abde
abd
ab
a
<empty>
TOP value
-1
0
1
2
1
2
3
2
1
0
-1
Output
c
c
c
ce
ced
cedb
cedba
Pop( )
int main() {
int choice,item;
init_stack();
do
{
printf("\n\t\t\tMenu\n\t1.Push.\n\t2.Pop.");
printf("\n\t3.Peek.\n\t4.Display.\n\t5.Exit.\n");
printf("\nYour Choice: ");
scanf("%d",&choice);
switch(choice)
{
case 1:printf("\nEnter the element to push :
");
scanf("%d",&item);
push(item); break;
case 2:item = pop();
printf("\nElement poped : %d",item);
printf("\nPress a key to continue...");
getche(); break;
case 3:item = peek();
printf("\nElement at top : %d",item);
printf("\nPress a key to continue...");
getche(); break;
case 4:display();
printf("\nPress a key to continue...");
getche(); break;
case 5:exit(0);
}
while(temp!=NULL) {
printf("%d\t",temp->data);
temp=temp->link;
}
}
void main() {
int choice,num,i;
while(1) {
printf("\n\t\t MENU\n1. Push\n2. Pop\n3. Peek");
printf("\n4. Elements in Stack\n5. Exit\n");
printf("\n\tEnter your choice: ");
scanf("%d",&choice);
switch(choice) {
case 1: printf("\nElement to be pushed:");
scanf("%d",&num);
push(num); break;
case 2: num=pop();
printf("\nElement popped: %d ",num);
getch(); break;
case 3: num=peek();
printf("\nElement peeked : %d ",num);
getch(); break;
case 4: printf("\nElements present in stack : ):
display();getch(); break;
case 5: exit(1);
default: printf("\nInvalid Choice\n"); break;
}
}
}
Queues
-- Queue is a linear data structure that permits insertion of new element at one end and deletion of an
element at the other end.
-- The end at which insertion of a new element can take place is called rear and the end at which
deletion of an element take place is called front .
-- The first element that gets added into queue is the first one to get removed from the list, Hence
Queue is also referred to as First-In-First-Out ( FIFO ) list.
-- Queue must be created as empty.
-- Whenever an element is inserted into queue, it must be checked whether the queue is full or not.
-- Whenever an element is deleted form queue, it must be checked whether the queue is empty or
not.
-- We can implement the queue ADT either with array or linked list.
rear
front
addq (4)
5
delq ( )
Queue ADT
struct queueNode {
int data; struct queueNode *next;
};
init_queue( )
addq ( )
delq ( )
isEmpty ( )
printQueue ( )
Applications of Queues
Execution of Threads
Job Scheduling
Event queuing
Message Queueing
Types of Queues
circular queues
priority queues
double-ended queues
else {
printf("\nElements in Queue :\n");
while(temp!=NULL) {
printf("%5d",temp->data);
temp=temp->next;
}
}
}
int main() {
int ch,num;
init_queue();
do {
printf("\n\tMENU\n\n1. Add\n2. Delete");
printf("\n3. Display Queue\n4. Exit.");
printf("\n\n\tYour Choice: ");
scanf("%d",&ch);
switch(ch) {
case 1: printf("\nEnter an element : ");
scanf("%d",&num);
addq(num);break;
case 2: num=delq();
printf("\nElement deleted : %d",num); break;
case 3: display(); break;
case 4: exit(0);
default:printf("\nInvalid option..");
}
}while(1);
}
--Arithmetic Expressions are represented using three notations infix, prefix and postfix. The prefixes
pre, post, and in refer to position of operators with respect to two operands.
-- In infix notation, the operator is placed between the two operands.
Ex: A + B
A*B+C
(A * B) + (C * D)
-- In Prefix notation, the operator is placed before the two operands.
Ex: +AB
*A+BC
+*AB*CD
-- In Postfix notation, the operator is placed after the two operands.
Ex: AB+
ABC+*
AB*CD*+
y=pop(&s) ;
while(y != '(') {
printf("%c",y);
y=pop(&s) ;
}
} else {
if(s.top ==-1 || s.items[s.top] == '(')
push(&s ,x);
else {
/* y is the top operator in the stack*/
y = s.items[s.top];
/* precedence of y is higher/equal to x*/
if( y=='*' || y=='/'){
printf("%c", pop(&s));
push(&s ,x);
} else if ( y=='+' || y=='-')
/* precedence of y is equal to x*/
if( x=='+' || x=='-') {
printf("%c", pop(&s));
push(&s ,x);
}
/* precedence of y is less than x*/
else
push(&s ,x);
}
}
}
while(s.top != -1) printf("%c",pop(&s));
}
case '*':push(op1*op2);break;
case '/':push(op1/op2);break;
case '^':push(pow(op1,op2));
break;
}
}
j++;
}
return pop();
}
int main() {
int j=0;
char expr[20];
float number[20],result;
printf("\nEnter a post fix expression : ");
gets(expr);
while(expr[j]!='\0')
{
if(isalpha(expr[j]))
{
fflush(stdin);
printf("\nEnter number for %c : ",expr[j]);
scanf("%f",&number[j]);
}
j++;
}
result = evaluate(expr,number);
printf("\nThe result of %s is %f",expr,result);
}
FaaDoOEngine
ers.com
Engaging Peers, Inspiring Careers!