You are on page 1of 17

Data Structures

Agenda
  

Introduction Arrays Linked list


 

Single linked list Double linked list

 

Stack Queue

Introduction
Data Structure: a way to store and organize data in the structured manner. Computer's memory is divided into small parts, we use different data structures to store data in those small blocks. Example: Arrays, Linked list, Trees ..........

Arrays


Array is basic data structure in which we store data in continues memory locations. Array is variable which holds mulitiple elements of same type. Arrays reduce the redendency Generic form of arrays:
data type array_name[size];
 

 

Data type: what type of data(int, float, char...) Size: number elements you want to store

Arrays
 

Every element will have index value If you want to access any element we must use index value of the element. Disadavantages:
  

Capable of holding only one type of elements. Insertion and deletion is very difficult Waste of memory is very high

Linked list


Linked list: collection of nodes which or connected together Node: is just like variable but it holds data and address of the next node or null. Adavantages:
 

Memory efficincy is very high Insertion and deletion is very easy and fast.

Linked list


When linked list is useful:


 

Do not know the number of data elements Your list need to be sorted quickly Traversal is very slow If you want to access 10th element in the list you need to cross 9 elements (arrays are very fast because of index) You can move only in one direction in single linked list If one link is corrupted remain data will be lost.

Disadvantages:
 

Example program on linked list

Double Linked list


  

In single linked we can move only in one direction Double linked list allow us to move in direction. Double linked is also collection of nodes, but here node is contains more than 2 fields. Node: one field holds data, second field holds address of next node or null and third field holds address of previous node or null.

Double linked list




Disadvantages:
 

Need extra space for holding references. Still it is very slow to travel across the list. If the head of linked list is connected to tail that list is called circular linked list. Stacks and Queues.

Circular linked list:




Application of linked list:




Example program

Stack


Stack


New nodes can be added and removed only at the top Similar to a pile of dishes Last-in, first-out (LIFO) Bottom of stack indicated by a link member to NULL Constrained version of a linked list

  

Operations on Stack


push


Adds a new node to the top of the stack Removes a node from the top Stores the popped value Returns true if pop was successful

pop
  

Queue


Queue
   

Similar to a supermarket checkout line First-in, first-out (FIFO) Nodes are removed only from the head Nodes are inserted only at the tail

Operations on Queue


Insert and remove operations




Enqueue (insert) and dequeue (remove)

Disadvantages:

What happens if you're 10th in line at the supermarket and it takes 5 minutes to process each person ahead of you?
You wait 45 minutes.

References
 

http://ccodings.blogspot.com http://afizafiz.blogspot.com/2010/10/linked-list-in-cprogramming.html

You might also like