Data Structure
A Data Structure is a particular way of organizing data in a computer so
that it can be used effectively.
For Example : we can store a list of items having the same data-type using
the array data structure.
U B F D A E C . . .
Dynamic Allocation Memory
Function Code Task
Malloc() ptr = (cast-type*) malloc(byte-size) Allocates request size
of bytes and returns a
pointer to the first
bytes of the allocated
Space.
Calloc() ptr = (cast-type*)calloc(n, element-size); Allocates space for an
array of elements,
initializes them to
zero and then returns
a pointer to the
memory.
Function Code Task
Free() free(ptr); Frees previously
allocated space.
Realloc() ptr = realloc(ptr, newSize); Modifies the size of
previously allocated
space.
• A linked list is a series of connected nodes
• Each node contains at least
– A piece of data (any type)
– Pointer to the next node in the list
• Head: pointer to the first node
• The last node points to NULL
Single Linked List
• Singly linked list is a
collection of
nodes linked together
in a sequential way
where each node
of singly linked
list contains a data
field and an address
field which contains
the reference of the
next node.
Circular Linked List
• The last node points
to the first node of the
list
• How do we know
when we have
finished traversing the
list? (Tip: check if the
pointer of the current
node is equal to the
head.)
Doubly linked lists
• Each node points to
not only successor but
the predecessor
• There are two NULL:
at the first and last
nodes in the list
• Advantage: given a
node, it is easy to
visit its predecessor.
Convenient to
traverse lists
backwards
Single Linked List
• A linked list we must keep track/reference of the first
node which may be referred by head pointer
variable. In singly linked list address field of last node
must contain a NULL value specifying end of the list.
struct node
{
int data; //Data
node * next; // Address
};
Circular Linked List
I. A circular linked list is basically a linear linked list that may be singly or doubly. The only difference is
that there is no any NULL value terminating the list. In fact in the list every node points to the next node
and last node points to the first node, thus forming a circle. Since it forms a circle with no end to stop
hence it is called as circular linked list.
• Algorithm to create circular linked list
• %%Input : N {Total number of nodes to be created}
• Being: alloc (head)
• read (data)
• head.data ← data;
• head.next ← NULL;
• prevNode ← head;
• For count ← 2 to N do alloc (newNode)
• read (data) newNode.data ← data;
• newNode.next ← NULL;
• prevNode.next ← newNode;
• prevNode ← newNode;
• End for
• prevNode.next ← head;
• End
Doubly linked list
Doubly linked list is a collection of nodes linked together in a sequential way. Each node of the list
contains two parts (as in singly linked list) data part and the reference or address part. The
basic structure of node is shown in the below image:
struct node { int data; // Data field
struct node * prev; // Address of previous node
struct node * next; // Address of next node };

Data structure

  • 2.
    Data Structure A DataStructure is a particular way of organizing data in a computer so that it can be used effectively. For Example : we can store a list of items having the same data-type using the array data structure. U B F D A E C . . .
  • 3.
    Dynamic Allocation Memory FunctionCode Task Malloc() ptr = (cast-type*) malloc(byte-size) Allocates request size of bytes and returns a pointer to the first bytes of the allocated Space. Calloc() ptr = (cast-type*)calloc(n, element-size); Allocates space for an array of elements, initializes them to zero and then returns a pointer to the memory.
  • 4.
    Function Code Task Free()free(ptr); Frees previously allocated space. Realloc() ptr = realloc(ptr, newSize); Modifies the size of previously allocated space.
  • 5.
    • A linkedlist is a series of connected nodes • Each node contains at least – A piece of data (any type) – Pointer to the next node in the list • Head: pointer to the first node • The last node points to NULL
  • 6.
    Single Linked List •Singly linked list is a collection of nodes linked together in a sequential way where each node of singly linked list contains a data field and an address field which contains the reference of the next node. Circular Linked List • The last node points to the first node of the list • How do we know when we have finished traversing the list? (Tip: check if the pointer of the current node is equal to the head.) Doubly linked lists • Each node points to not only successor but the predecessor • There are two NULL: at the first and last nodes in the list • Advantage: given a node, it is easy to visit its predecessor. Convenient to traverse lists backwards
  • 7.
    Single Linked List •A linked list we must keep track/reference of the first node which may be referred by head pointer variable. In singly linked list address field of last node must contain a NULL value specifying end of the list. struct node { int data; //Data node * next; // Address };
  • 8.
    Circular Linked List I.A circular linked list is basically a linear linked list that may be singly or doubly. The only difference is that there is no any NULL value terminating the list. In fact in the list every node points to the next node and last node points to the first node, thus forming a circle. Since it forms a circle with no end to stop hence it is called as circular linked list. • Algorithm to create circular linked list • %%Input : N {Total number of nodes to be created} • Being: alloc (head) • read (data) • head.data ← data; • head.next ← NULL; • prevNode ← head; • For count ← 2 to N do alloc (newNode) • read (data) newNode.data ← data; • newNode.next ← NULL; • prevNode.next ← newNode; • prevNode ← newNode; • End for • prevNode.next ← head; • End
  • 9.
    Doubly linked list Doublylinked list is a collection of nodes linked together in a sequential way. Each node of the list contains two parts (as in singly linked list) data part and the reference or address part. The basic structure of node is shown in the below image: struct node { int data; // Data field struct node * prev; // Address of previous node struct node * next; // Address of next node };