Friday 5 October 2012

Data Structure Tutorial-1-Singly linked list

Data Structure:

Data can be stored in different ways in a computer system.Data structure explains different ways of storing the data in the computer so that it can be used efficiently. 

Singly linked list:

The most simplest form of data structure is singly linked list.Its simple and it is used to implement other data structures like stack,queue.

ARRAYS VS SINGLY LINKED LIST

  • The single linked list is similar to ARRAYS.It over comes the disadvantage of arrays.
  • The main disadvantage of ARRAYS is, it is difficult to insert or delete an element in it.Singly linked list gives solution to it.
  • In most of the cases arrays size are not dynamic.In some cases array size is insufficient while in others there is lots of memory space get wasted.In singly linked list we have no need to declare the size of it at starting of the program.We can increase the size when ever we need it.So there is no memory wastage in it.

Structure of Singly Linked list:

  1. Head node
  2. Data nodes

HEAD NODE

 The basic structure of linked list is very similar to arrays.The HEAD node consist of the starting address of the first data node in the linked list.Its points to the address of the first data node.

DATA NODE

It consist of two section.The Data section and Address section.The Data section is used to store the data of the node and the Address section is used to point the next data node address.The last data node is called as tail node whose address section is empty.

Traversal

Using the head node we can find the first node.From the head node we can travers through the node using the address pointer.

OPERATIONS IN SINGLY LINKED LIST

  • INSERTION
  • DELETION 
  • SEARCHING

Insertion

  • To insert THE NODE IS LAST POSITION.Create the node and give the tail node address pointer to the new node and make the address section of new node as empty.Thus making the new node as tail node.
  • To insert THE NODE IN BETWEEN THE NODES.Create the node.Traverse through the list and find the node after which the node as to be inserted,lets call that node as X.First copy the address pointer of the X node to the new node.So now the new node will be pointing to next node of the X node.Now change the address pointer of X node to the newly created node.
  • To insert THE NODE AS THE FIRST NODE.Create the node.Now copy the address pointer of head node to new node.Then change the address pointer of the head node to point to the new node.

Deletion 

  • To delete the node in LAST.Traverse through the list and find the node that's before to the last node.Make the address pointer of that node as empty.
  • To delete the node in FIRST position.Copy the address pointer of the first node to the head node.Then make the address pointer of first node to NULL.
  • To delete the node IN MIDDLE.Traverse through the list,find the node that is previous to the node that as to be deleted.Copy the address pointer of the node that as to be deleted to its previous node and make the address pointer of the node to NULL.

Searching

  • Traverse through the node and find the data in a sequential manner.

Advantages of Singly linked list

  • Less memory wastage
  • We can insert and delete nodes to our wish.

Disadvantages of Singly linked list

  • It takes time to search through the list
  • We cannot traverse back through the list.We can traverse in only one way.

No comments:

Post a Comment