Data Structure
(KCS-301)
Arvind Kumar
Assistant Professor
CSE/IT Dept.
Data Structure
(KCS-301)
Unit-1
Data Structure
Data structure is simply an arrangement of data or organization of data in
different ways.
Classification of data structures:
Arrays
Linked list
Stacks
Queues
Trees
Graphs
Definition of Algorithm
An algorithm is a set of instructions, sometimes called a procedure or a
function, that is used to perform a certain task.
An algorithm is a description of a procedure which terminates with a
result. Simple algorithms can be implemented within a function for
instance, the factorial of a number x is x multipled by x-1 multipled by
x-2 and so on until it is multiplied by 1.
Arrays
Arrays An array is a series of elements of the same type placed in
contiguous memory locations that can be individually referenced by
adding an index to a unique identifier.
The elements of global and static arrays, on the other hand, are
automatically initialized with their default values, which for all
fundamental types this means they are filled with zeros.
In both cases, local and global, when we declare an array, we have
the possibility to assign initial values to each one of its elements by
enclosing the values in braces { }.
Arrays
Inserting an element in array
It means adding a new element in an existing array
Algorithm
Insertion (A , N, n= no of elements, new= new element,
Index)
Set I= n
Repeat steps 3 while I>index
Set A[I]=A[I+1]
Set A[Index]=new
Exit
Arrays
Algorithm for traversing the array
Set I=Lowerbound
Repeat step 3 and 4 while I<=Upeer bound
Print A[I]
Set I=I+1
Exit
Inserting an element in array
It means adding a new element in an existing array
Algorithm
Insertion (A , N, n= no of elements, new= new element, Index)
Set I= n
Repeat steps 3 while I>index
Set A[I]=A[I+1]
Set A[Index]=new
Exit
Arrays
Searching in Array
It works on unsorted list Called sequential search
The best case is when element is found at first position in array
The worst case is when element is found at last position or not found
Algorithm( I, found,num= val to be found,n=no of elements)
Set I=0, found=0
Enter the number to be searched
Repeat steps 4 while I<n
If (A[I]==num)
Set found=1
Print position I
(End of if)
(End of loop)
If found=0, then value is not searched
Exit
Address calculation of an element
Address calculation of an element:
Row-Major : addr (i,j) = B + W * ( Nc * (i - Lr) + (j-Lc) )
Col-Major : addr (i,j) = B + W * ( (i - Lr) + Nr * (j-Lc) )
i,j = subscript number. B = Base address W = width (size) of
each element Nc = Number of Columns Nr = Number of Rows
Lc = Lower-bound of Column Lr = Lower-bound of Row
In above example, for element (6), i.e., a(1,2) in row-major or
a(2,1) in col-major, B = 200 (say) W = 2 Lr=Lc=0 Nc=Nr=3
addr (1,2) = 210; addr (2,1) = 214
Linked list
Linked list is the collection of nodes connected together with the help of addresses
Advantages of Linked Lists
They are a dynamic in nature which allocates the memory when required.
Insertion and deletion operations can be easily implemented.
Stacks and queues can be easily executed.
Linked List reduces the access time.
Disadvantages of Linked Lists
The memory is wasted as pointers require extra memory for storage.
No element can be accessed randomly; it has to access each node sequentially.
Reverse Traversing is difficult in linked list.
Applications of Linked Lists
Linked lists are used to implement stacks, queues, graphs, etc.
Linked lists let you insert elements at the beginning and end of the list.
In Linked Lists we don’t need to know the size in advance.
Types of Linked List
Types of Linked List:
• Singly Linked list or 1- way linked list
• Doubly or 2-way linked list
• Circular Linked list
Singly Linked List : Singly linked lists contain nodes which have a
data part as well as an address part i.e. next, which points to the next
node in sequence of nodes. The operations we can perform on singly
linked lists are insertion, deletion and traversal.
Types of Linked List
Doubly Linked List : In a doubly linked list, each node contains two
links the first link points to the previous node and the next link points
to the next node in the sequence.
Circular Linked List : In the circular linked list the last node of the
list contains the address of the first node and forms a circular chain.
Implement a Singly Linked list in Stack
#include<stdio.h>
#include<conio.h>
struct stu *new(int)
void addnode( );
void traverse( );
struct stu
{
Int info;
Struct stu * next;
}
*head=NULL, *temp,*p;
Void main( )
{
int a, ch;
while( ch!=0)
{
Scanf(“%d”,&a);
P=new(a);
addnode(p);
printf(“press 1 for vontinue and 0 for end”);
scanf(“%d”, &ch);
}
Traverse (head);
getch( );
}
Struct stu *new ( int a)
{
p=((struct stu *) malloc (sizeof(struct stu)));
p – info=a;
p – next=NULL;
return p;
}
Void addnode( )
{
Temp=head;
If (head==NULL)
{
Head=p;
}
Else
{
P – next=temp;
Head=p;
}
Void traverse
{
Temp=head;
While(temp!=NULL)
{
Printf(“%d”, temp- info);
Temp=temp – next;
}
}
Deletion
Deleting the first node of Singly Linked List:
main( )
{
Int a,ch;
.
. Same as above programs
.
traverse();
delfirst()
Getch();
}
Void delfirst ( )
{
Temp=head;
Head=temp->next;
Free (temp);
}
Deleting the last node of Singly Linked List:
main( )
{
Int a,ch;
.
. Same as above programs
.
traverse();
dellast()
getch();
}
Void dellast( )
{
Temp=head;
While (temp->next!=NULL)
{
r= temp;
temp=temp->next;
}
r-> next=NULL;
free(temp);
}
Deleting the particular node of Singly Linked List:
Void delpart( )
{
Temp=head;
While (temp!=NULL)
{
r=temp;
temp=temp->next;
if(temp->info==item)
{
r->next=temp->next;
free(temp);
}
}
}
Implement Doubly Linked list using Stack
#include<stdio.h>
#include<conio.h>
struct stu *new(int)
void addnode( );
void traverse( );
struct stu
{
Int info;
Struct stu * next;
Struct stu *pre;
}
*head=NULL, *temp,*p;
Void main( )
{
int a, ch;
while( ch!=0)
{
Scanf(“%d”,&a);
P=new(a);
addnode(p);
printf(“press 1 for Continue and 0 for end”);
scanf(“%d”, &ch);
}
Traverse ( );
getch( );
}
Struct stu *new ( int a)
{
p=((struct stu *) malloc (sizeof(struct stu)));
p – >info=a;
p – >next=NULL;
p->pre=NULL;
return p;
}
Void addnode( struct stu *p)
{
Temp=head;
If (head==NULL)
{
Head=p;
}
Else
{
P – next=temp;
Temp->pre=p;
Head=p;
}
Void traverse
{
Temp=head;
While(temp!=NULL)
{
Printf(“%d”, temp- info);
Temp=temp – next;
}
}
Delete the first node in Doubly Linked list
#include<stdio.h>
#include<conio.h>
struct stu *new(int)
void addnode( );
void traverse( );
void delfirst( );
struct stu
{
Int info;
Struct stu * next;
Struct stu *pre;
}
*head=NULL, *temp,*p;
Void main( )
{
int a, ch;
while( ch!=0)
{
Scanf(“%d”,&a);
P=new(a);
addnode(p);
printf(“press 1 for Continue and 0 for
end”);
scanf(“%d”, &ch);
}
Traverse ( );
delfirst ( );
getch( );
}
Struct stu *new ( int a)
{
p=((struct stu *) malloc (sizeof(struct
stu)));
p – >info=a;
p – >next=NULL;
p->pre=NULL;
return p;
}
Void addnode (struct stu*p)
{
Temp=head;
If(head==NULL)
{
Head=p;
{
Else
{
while(temp->next!=NULL)
{
Temp=temp->next;
}
Temp->next=p;
p->pre=temp;
}
}
Void traverse( )
{
Temp=head;
While(temp!=NULL)
Printf(“%d”, temp-> info);
Temp=temp – >next;
}
}
Void delfirst( )
{
Temp=head;
head=temp->next;
head->pre=NULL;
free(temp);
}
Program to add the before a given node in
Doubly Linked list:
#include<stdio.h>
#include<conio.h>
struct stu *new(int)
void addnode( );
void traverse( );
void addbefore( );
struct stu
{
Int info;
Struct stu * next;
Struct stu *pre;
}
*head=NULL, *temp,*p;
Void main( )
{
int a, ch,item;
while( ch!=0)
{
Scanf(“%d”,&a);
P=new(a);
addnode(p);
printf(“press 1 for Continue and 0 for end”);
scanf(“%d”, &ch);
}
Traverse ( );
Printf(“enter the new node value”);
Scanf(“%d”,&a);
P=new(a);
Printf(“Enter the node value before which
the value to be added”);
Scanf( “%d”, &item);
Addbefore( item);
getch( );
}
Struct stu *new ( int a)
{
p=((struct stu *) malloc (sizeof(struct stu)));
p – >info=a;
p – >next=NULL;
p->pre=NULL:
return p;
}
Void addnode (struct stu*p)
{
Temp=head;
If(head==NULL)
{
Head=p;
{
Else
{
while(temp->next!=NULL)
{
Temp=temp->next;
}
Temp->next=p;
p->pre=temp;
}
}
Void traverse( )
{
Temp=head;
While(temp!=NULL)
Printf(“%d”, temp-> info);
Temp=temp – >next;
}
}
Void addbefore( int item)
{
Temp=head;
While(temp!=NULL)
{
If(temp->info==item)
{
p->next=temp;
temp->pre->next=p;
p->pre=temp->pre;
}
Temp=temp->next;}}
Sparse matrix
Sparse matrix is a matrix with majority of its elements equal to
zeros.
Linked list representation of Sparse Matrix
The above given matrix is represented as:
Program to sparse matrix
void main ()
{
static int array[10][10];
int i, j, m, n;
int counter = 0;
printf("Enter the order of the matix
n");
scanf("%d %d", &m, &n);
printf("Enter the co-efficients of
the matix n");
for (i = 0; i < m; ++i)
{
for (j = 0; j < n; ++j)
{
scanf("%d", &array[i][j]);
if (array[i][j] == 0)
{
++counter;
}
}
}
if (counter > ((m * n) / 2))
{
printf("The given matrix is
sparse matrix n");
}
else
printf("The given matrix is not a
sparse matrix n");
printf("There are %d number of
zeros", counter);
}

Data structure

  • 1.
  • 2.
  • 3.
    Data Structure Data structureis simply an arrangement of data or organization of data in different ways. Classification of data structures: Arrays Linked list Stacks Queues Trees Graphs
  • 4.
    Definition of Algorithm Analgorithm is a set of instructions, sometimes called a procedure or a function, that is used to perform a certain task. An algorithm is a description of a procedure which terminates with a result. Simple algorithms can be implemented within a function for instance, the factorial of a number x is x multipled by x-1 multipled by x-2 and so on until it is multiplied by 1.
  • 5.
    Arrays Arrays An arrayis a series of elements of the same type placed in contiguous memory locations that can be individually referenced by adding an index to a unique identifier. The elements of global and static arrays, on the other hand, are automatically initialized with their default values, which for all fundamental types this means they are filled with zeros. In both cases, local and global, when we declare an array, we have the possibility to assign initial values to each one of its elements by enclosing the values in braces { }.
  • 6.
    Arrays Inserting an elementin array It means adding a new element in an existing array Algorithm Insertion (A , N, n= no of elements, new= new element, Index) Set I= n Repeat steps 3 while I>index Set A[I]=A[I+1] Set A[Index]=new Exit
  • 7.
    Arrays Algorithm for traversingthe array Set I=Lowerbound Repeat step 3 and 4 while I<=Upeer bound Print A[I] Set I=I+1 Exit Inserting an element in array It means adding a new element in an existing array Algorithm Insertion (A , N, n= no of elements, new= new element, Index) Set I= n Repeat steps 3 while I>index Set A[I]=A[I+1] Set A[Index]=new Exit
  • 8.
    Arrays Searching in Array Itworks on unsorted list Called sequential search The best case is when element is found at first position in array The worst case is when element is found at last position or not found Algorithm( I, found,num= val to be found,n=no of elements) Set I=0, found=0 Enter the number to be searched Repeat steps 4 while I<n If (A[I]==num) Set found=1 Print position I (End of if) (End of loop) If found=0, then value is not searched Exit
  • 9.
    Address calculation ofan element Address calculation of an element: Row-Major : addr (i,j) = B + W * ( Nc * (i - Lr) + (j-Lc) ) Col-Major : addr (i,j) = B + W * ( (i - Lr) + Nr * (j-Lc) ) i,j = subscript number. B = Base address W = width (size) of each element Nc = Number of Columns Nr = Number of Rows Lc = Lower-bound of Column Lr = Lower-bound of Row In above example, for element (6), i.e., a(1,2) in row-major or a(2,1) in col-major, B = 200 (say) W = 2 Lr=Lc=0 Nc=Nr=3 addr (1,2) = 210; addr (2,1) = 214
  • 10.
    Linked list Linked listis the collection of nodes connected together with the help of addresses Advantages of Linked Lists They are a dynamic in nature which allocates the memory when required. Insertion and deletion operations can be easily implemented. Stacks and queues can be easily executed. Linked List reduces the access time. Disadvantages of Linked Lists The memory is wasted as pointers require extra memory for storage. No element can be accessed randomly; it has to access each node sequentially. Reverse Traversing is difficult in linked list. Applications of Linked Lists Linked lists are used to implement stacks, queues, graphs, etc. Linked lists let you insert elements at the beginning and end of the list. In Linked Lists we don’t need to know the size in advance.
  • 11.
    Types of LinkedList Types of Linked List: • Singly Linked list or 1- way linked list • Doubly or 2-way linked list • Circular Linked list Singly Linked List : Singly linked lists contain nodes which have a data part as well as an address part i.e. next, which points to the next node in sequence of nodes. The operations we can perform on singly linked lists are insertion, deletion and traversal.
  • 12.
    Types of LinkedList Doubly Linked List : In a doubly linked list, each node contains two links the first link points to the previous node and the next link points to the next node in the sequence. Circular Linked List : In the circular linked list the last node of the list contains the address of the first node and forms a circular chain.
  • 13.
    Implement a SinglyLinked list in Stack #include<stdio.h> #include<conio.h> struct stu *new(int) void addnode( ); void traverse( ); struct stu { Int info; Struct stu * next; } *head=NULL, *temp,*p; Void main( ) { int a, ch; while( ch!=0) { Scanf(“%d”,&a); P=new(a); addnode(p); printf(“press 1 for vontinue and 0 for end”); scanf(“%d”, &ch); } Traverse (head); getch( ); } Struct stu *new ( int a) { p=((struct stu *) malloc (sizeof(struct stu))); p – info=a; p – next=NULL; return p; } Void addnode( ) { Temp=head; If (head==NULL) { Head=p; } Else { P – next=temp; Head=p; } Void traverse { Temp=head; While(temp!=NULL) { Printf(“%d”, temp- info); Temp=temp – next; } }
  • 14.
    Deletion Deleting the firstnode of Singly Linked List: main( ) { Int a,ch; . . Same as above programs . traverse(); delfirst() Getch(); } Void delfirst ( ) { Temp=head; Head=temp->next; Free (temp); } Deleting the last node of Singly Linked List: main( ) { Int a,ch; . . Same as above programs . traverse(); dellast() getch(); } Void dellast( ) { Temp=head; While (temp->next!=NULL) { r= temp; temp=temp->next; } r-> next=NULL; free(temp); } Deleting the particular node of Singly Linked List: Void delpart( ) { Temp=head; While (temp!=NULL) { r=temp; temp=temp->next; if(temp->info==item) { r->next=temp->next; free(temp); } } }
  • 15.
    Implement Doubly Linkedlist using Stack #include<stdio.h> #include<conio.h> struct stu *new(int) void addnode( ); void traverse( ); struct stu { Int info; Struct stu * next; Struct stu *pre; } *head=NULL, *temp,*p; Void main( ) { int a, ch; while( ch!=0) { Scanf(“%d”,&a); P=new(a); addnode(p); printf(“press 1 for Continue and 0 for end”); scanf(“%d”, &ch); } Traverse ( ); getch( ); } Struct stu *new ( int a) { p=((struct stu *) malloc (sizeof(struct stu))); p – >info=a; p – >next=NULL; p->pre=NULL; return p; } Void addnode( struct stu *p) { Temp=head; If (head==NULL) { Head=p; } Else { P – next=temp; Temp->pre=p; Head=p; } Void traverse { Temp=head; While(temp!=NULL) { Printf(“%d”, temp- info); Temp=temp – next; } }
  • 16.
    Delete the firstnode in Doubly Linked list #include<stdio.h> #include<conio.h> struct stu *new(int) void addnode( ); void traverse( ); void delfirst( ); struct stu { Int info; Struct stu * next; Struct stu *pre; } *head=NULL, *temp,*p; Void main( ) { int a, ch; while( ch!=0) { Scanf(“%d”,&a); P=new(a); addnode(p); printf(“press 1 for Continue and 0 for end”); scanf(“%d”, &ch); } Traverse ( ); delfirst ( ); getch( ); } Struct stu *new ( int a) { p=((struct stu *) malloc (sizeof(struct stu))); p – >info=a; p – >next=NULL; p->pre=NULL; return p; } Void addnode (struct stu*p) { Temp=head; If(head==NULL) { Head=p; { Else { while(temp->next!=NULL) { Temp=temp->next; } Temp->next=p; p->pre=temp; } } Void traverse( ) { Temp=head; While(temp!=NULL) Printf(“%d”, temp-> info); Temp=temp – >next; } } Void delfirst( ) { Temp=head; head=temp->next; head->pre=NULL; free(temp); }
  • 17.
    Program to addthe before a given node in Doubly Linked list: #include<stdio.h> #include<conio.h> struct stu *new(int) void addnode( ); void traverse( ); void addbefore( ); struct stu { Int info; Struct stu * next; Struct stu *pre; } *head=NULL, *temp,*p; Void main( ) { int a, ch,item; while( ch!=0) { Scanf(“%d”,&a); P=new(a); addnode(p); printf(“press 1 for Continue and 0 for end”); scanf(“%d”, &ch); } Traverse ( ); Printf(“enter the new node value”); Scanf(“%d”,&a); P=new(a); Printf(“Enter the node value before which the value to be added”); Scanf( “%d”, &item); Addbefore( item); getch( ); } Struct stu *new ( int a) { p=((struct stu *) malloc (sizeof(struct stu))); p – >info=a; p – >next=NULL; p->pre=NULL: return p; } Void addnode (struct stu*p) { Temp=head; If(head==NULL) { Head=p; { Else { while(temp->next!=NULL) { Temp=temp->next; } Temp->next=p; p->pre=temp; } } Void traverse( ) { Temp=head; While(temp!=NULL) Printf(“%d”, temp-> info); Temp=temp – >next; } } Void addbefore( int item) { Temp=head; While(temp!=NULL) { If(temp->info==item) { p->next=temp; temp->pre->next=p; p->pre=temp->pre; } Temp=temp->next;}}
  • 18.
    Sparse matrix Sparse matrixis a matrix with majority of its elements equal to zeros. Linked list representation of Sparse Matrix The above given matrix is represented as:
  • 19.
    Program to sparsematrix void main () { static int array[10][10]; int i, j, m, n; int counter = 0; printf("Enter the order of the matix n"); scanf("%d %d", &m, &n); printf("Enter the co-efficients of the matix n"); for (i = 0; i < m; ++i) { for (j = 0; j < n; ++j) { scanf("%d", &array[i][j]); if (array[i][j] == 0) { ++counter; } } } if (counter > ((m * n) / 2)) { printf("The given matrix is sparse matrix n"); } else printf("The given matrix is not a sparse matrix n"); printf("There are %d number of zeros", counter); }