CS1301 Data Structures
Unit I INTRODUCTION
 Introduction
 Data structure
 Linked DS
 Succinct DS
 Implicit DS
 Compressed DS
 Search DS
 Static and Dynamic DS
 Persistent DS
 Concurrent DS
 Arrays
 Recursions
Data structure
• a particular way of storing and organizing
data in a computer so that it can be used
efficiently.
Linked DS
• is a data structure which consists of a set of data
records (nodes) linked together and organized by
references (links or pointers)
• In linked data structures, the links are usually
treated as special data types that can only be
compared for equality
• a structure contains a pointer to another instance
of the same structure or different structure
• Linking can be done in two ways - Using dynamic
allocation and using array index linking
• include linked lists, search trees, expression trees
Linked DS
• Advantages against arrays
– more flexibility
– saving wasted memory (built dynamically)
– the reference to each node gives us the information
where to find out the next one
– Memory can be utilized more efficiently
Linked DS
• General disadvantages
– follow multiple pointers so element access time
varies
– may also use more memory
Linked LIST
• Consists of series of node
• Each node of the list contains
• the data item
• a pointer to the next node
• Flexible space use
• Dynamically allocate space for each element as needed
• Include a pointer to the next item
Object
Data Next
Linked LIST
• Types of Linked List
– Singly Linked List
– Doubly Linked List
– Circular Linked List
SINGLY Linked LIST
• Each node contains only one link field pointing
to the next node in the list
700 1000 800
550 700 1000 800
10 20 30 40
Header
SINGLY Linked LIST
 Collection structure has a pointer to the list head
 Initially NULL
 Add first item
 Allocate space for node
 Set its data pointer to object
 Set Next to NULL
 Set Head to point to new node
Data Next
object
Head
Collection
node
SINGLY Linked LIST
 Add second item
 Allocate space for node
 Set its data pointer to object
 Set Next to current Head
 Set Head to point to new node
Data Next
object
Head
Collection
node
Data Next
object2
node
SINGLY Linked LIST
• Declaration for Linked List
Struct node;
typedef struct Node *List
typedef struct Node *Position
int IsLast (List L);
int IsEmpty(List L);
position Find(int x, List L);
void delete(int x, List L);
Struct node
{
int element;
position next;
};
SINGLY Linked LIST
• Routine to insert an element in the list
void insert(int x, List L, Position P) // insert after position P
{
Position Newnode;
Newnode = malloc(size of(Struct Node));
If(Newnode !=NULL)
{
Newnode Element =X;
Newnode  Next =PNext;
P  Next = Newnode;
}
}
To insert an element in the list
SINGLY Linked LIST
• Routine to check whether list is empty
int IsEmpty(List L) // returns 1 if L is empty
{
if (L Next == NULL)
return(1);
}
SINGLY Linked LIST
• Routine to check whether current
position is last
int IsLast(position p,List L) // returns 1 if P is the last
position in L
{
if (P Next == NULL)
return(1);
}
SINGLY Linked LIST
• Find Routine
position Find(int x,List L)
{
//Returns the position of x in L, null if x is not found
Position P;
P= L Next;
while(P!= NULL && PElement!= X)
P=PNext
return P;
}
SINGLY Linked LIST
• Find Previous Routine
position Findprevious(int x,List L)
{
//Returns the position of the predecessor
Position p;
P=L
while(P Next!= NULL && PNextElement!= X)
P=PNext
return P;
}
SINGLY Linked LIST
• Find Next Routine
position FindNext(int x,List L)
{
//Returns the position of its successor
P=L Next
while(P Next!= NULL && PElement!= X)
P=PNext
return PNext;
}
SINGLY Linked LIST
• Routine to delete an element from the list
Void Delete(int x, List L)
{
// Delete the first occurrence of X from the list
position P, temp;
P= Findprevious(x,L);
If(!IsLast(P,L))
{
Temp=P Next;
PNext = Temp Next
free(temp);
}
}
SINGLY Linked LIST
• Routine to delete the list
Void DeleteList(List L)
{
position P, Temp;
P= LNext ;
LNext = NULL;
while(P!=NULL)
{
Temp=P Next;
free(P);
P=Temp;
}
}
DoUBLY Linked LIST
• Each node has three fields
– Data field
– Forward Link(FLINK)
– Backward link(BLINK)
• FLINK points to the successor node in the list
• BLINK points to the predecessor node
BLINK Data FLINK
Doubly Linked LIST
L
10 20
Header
30
Structure Declaration
Struct Node
{
Int element;
Struct Node *FLINK;
Struct Node *BLINK;
}
DoUBLY Linked LIST
Routine to insert an element in a doubly linked list
Void insert(int x, list l, position p)
{
struct Node *Newnode;
Newnode= malloc(size of(Struct Node));
If (Newnode !=NULL)
{
Newnode  Element =x;
Newnode  Flink = p Flink;
P  flink  blink =Newnode;
P  Flink = Newnode;
Newnode  Blink = p;
}
}
DoUBLY Linked LIST
DoUBLY Linked LIST
DoUBLY Linked LIST
• Advantages
– Deletion operation is easier
– Finding predecessor & successor of a node is
easier
• Disadvantages
– More memory space required since it has two
pointers
CIRCULAR Linked LIST
• Pointer of last node points to the first
node
• Types
–Singly Linked Circular List
–Doubly Linked circular List
CIRCULAR Linked LIST
• Singly Linked Circular List
–Last node of the list points to the first
node
CIRCULAR Linked LIST
• Doubly Linked Circular List
– Doubly linked list
– Forward link of last node points to the first node
– Backward link of the first node points to the last
node of the list
CIRCULAR Linked LIST
• Advantages
–Allows to traverse the list starting at any
point
–Allows quick access to the first and last
records
–Circular doubly linked list allows the list to
traverse in either directions.
POINTERS and arrays
• Special variables which contain the address of
another memory location
int arr[4] = {2,4,6,8};
• arr acts as constant pointer pointing to first
element
• arr= &arr[0]=100
arr[0] arr[1] arr[2] arr[3]
100 102 104 106
• & is the address operator, represents address of
the variable
• %u – obtaining the address
2 4 6 8
POINTERS
main()
{
int a=2;
printf(“value of a=%d”,a);
printf(“addressof a=%u”,&a);
} a – variable name
value of a or value at address 1000
1000 - address
2
• * operator is the value of at the address operator
• Pointer variable declaration - * should preced variable
name
int *b;
b=&a;
• b is the variable which contains the address of variable
a as its value
• Pointer variable that stores the address of a pointer
variable
int a=2;
int *b;
int **c;
b=&a;
c=&b;
• C has been declared as pointer to pointer variable
which contains the address of pointer variable b

Data structures2

  • 1.
  • 2.
    Unit I INTRODUCTION Introduction  Data structure  Linked DS  Succinct DS  Implicit DS  Compressed DS  Search DS  Static and Dynamic DS  Persistent DS  Concurrent DS  Arrays  Recursions
  • 3.
    Data structure • aparticular way of storing and organizing data in a computer so that it can be used efficiently.
  • 4.
    Linked DS • isa data structure which consists of a set of data records (nodes) linked together and organized by references (links or pointers) • In linked data structures, the links are usually treated as special data types that can only be compared for equality • a structure contains a pointer to another instance of the same structure or different structure • Linking can be done in two ways - Using dynamic allocation and using array index linking • include linked lists, search trees, expression trees
  • 5.
    Linked DS • Advantagesagainst arrays – more flexibility – saving wasted memory (built dynamically) – the reference to each node gives us the information where to find out the next one – Memory can be utilized more efficiently
  • 6.
    Linked DS • Generaldisadvantages – follow multiple pointers so element access time varies – may also use more memory
  • 7.
    Linked LIST • Consistsof series of node • Each node of the list contains • the data item • a pointer to the next node • Flexible space use • Dynamically allocate space for each element as needed • Include a pointer to the next item Object Data Next
  • 8.
    Linked LIST • Typesof Linked List – Singly Linked List – Doubly Linked List – Circular Linked List
  • 9.
    SINGLY Linked LIST •Each node contains only one link field pointing to the next node in the list 700 1000 800 550 700 1000 800 10 20 30 40 Header
  • 10.
    SINGLY Linked LIST Collection structure has a pointer to the list head  Initially NULL  Add first item  Allocate space for node  Set its data pointer to object  Set Next to NULL  Set Head to point to new node Data Next object Head Collection node
  • 11.
    SINGLY Linked LIST Add second item  Allocate space for node  Set its data pointer to object  Set Next to current Head  Set Head to point to new node Data Next object Head Collection node Data Next object2 node
  • 12.
    SINGLY Linked LIST •Declaration for Linked List Struct node; typedef struct Node *List typedef struct Node *Position int IsLast (List L); int IsEmpty(List L); position Find(int x, List L); void delete(int x, List L); Struct node { int element; position next; };
  • 13.
    SINGLY Linked LIST •Routine to insert an element in the list void insert(int x, List L, Position P) // insert after position P { Position Newnode; Newnode = malloc(size of(Struct Node)); If(Newnode !=NULL) { Newnode Element =X; Newnode  Next =PNext; P  Next = Newnode; } }
  • 14.
    To insert anelement in the list
  • 15.
    SINGLY Linked LIST •Routine to check whether list is empty int IsEmpty(List L) // returns 1 if L is empty { if (L Next == NULL) return(1); }
  • 16.
    SINGLY Linked LIST •Routine to check whether current position is last int IsLast(position p,List L) // returns 1 if P is the last position in L { if (P Next == NULL) return(1); }
  • 17.
    SINGLY Linked LIST •Find Routine position Find(int x,List L) { //Returns the position of x in L, null if x is not found Position P; P= L Next; while(P!= NULL && PElement!= X) P=PNext return P; }
  • 18.
    SINGLY Linked LIST •Find Previous Routine position Findprevious(int x,List L) { //Returns the position of the predecessor Position p; P=L while(P Next!= NULL && PNextElement!= X) P=PNext return P; }
  • 19.
    SINGLY Linked LIST •Find Next Routine position FindNext(int x,List L) { //Returns the position of its successor P=L Next while(P Next!= NULL && PElement!= X) P=PNext return PNext; }
  • 20.
    SINGLY Linked LIST •Routine to delete an element from the list Void Delete(int x, List L) { // Delete the first occurrence of X from the list position P, temp; P= Findprevious(x,L); If(!IsLast(P,L)) { Temp=P Next; PNext = Temp Next free(temp); } }
  • 21.
    SINGLY Linked LIST •Routine to delete the list Void DeleteList(List L) { position P, Temp; P= LNext ; LNext = NULL; while(P!=NULL) { Temp=P Next; free(P); P=Temp; } }
  • 22.
    DoUBLY Linked LIST •Each node has three fields – Data field – Forward Link(FLINK) – Backward link(BLINK) • FLINK points to the successor node in the list • BLINK points to the predecessor node BLINK Data FLINK
  • 23.
    Doubly Linked LIST L 1020 Header 30 Structure Declaration Struct Node { Int element; Struct Node *FLINK; Struct Node *BLINK; }
  • 24.
    DoUBLY Linked LIST Routineto insert an element in a doubly linked list Void insert(int x, list l, position p) { struct Node *Newnode; Newnode= malloc(size of(Struct Node)); If (Newnode !=NULL) { Newnode  Element =x; Newnode  Flink = p Flink; P  flink  blink =Newnode; P  Flink = Newnode; Newnode  Blink = p; } }
  • 25.
  • 26.
  • 27.
    DoUBLY Linked LIST •Advantages – Deletion operation is easier – Finding predecessor & successor of a node is easier • Disadvantages – More memory space required since it has two pointers
  • 28.
    CIRCULAR Linked LIST •Pointer of last node points to the first node • Types –Singly Linked Circular List –Doubly Linked circular List
  • 29.
    CIRCULAR Linked LIST •Singly Linked Circular List –Last node of the list points to the first node
  • 30.
    CIRCULAR Linked LIST •Doubly Linked Circular List – Doubly linked list – Forward link of last node points to the first node – Backward link of the first node points to the last node of the list
  • 31.
    CIRCULAR Linked LIST •Advantages –Allows to traverse the list starting at any point –Allows quick access to the first and last records –Circular doubly linked list allows the list to traverse in either directions.
  • 32.
    POINTERS and arrays •Special variables which contain the address of another memory location int arr[4] = {2,4,6,8}; • arr acts as constant pointer pointing to first element • arr= &arr[0]=100 arr[0] arr[1] arr[2] arr[3] 100 102 104 106 • & is the address operator, represents address of the variable • %u – obtaining the address 2 4 6 8
  • 33.
    POINTERS main() { int a=2; printf(“value ofa=%d”,a); printf(“addressof a=%u”,&a); } a – variable name value of a or value at address 1000 1000 - address 2
  • 34.
    • * operatoris the value of at the address operator • Pointer variable declaration - * should preced variable name int *b; b=&a; • b is the variable which contains the address of variable a as its value • Pointer variable that stores the address of a pointer variable int a=2; int *b; int **c; b=&a; c=&b; • C has been declared as pointer to pointer variable which contains the address of pointer variable b