Well the first Data Structure that we studied is

**Linked List**and it also provides a way to implement various Advance Data Structures. It can be defined in the following way --**"**

**A Linked List is a Data Structure that consists of a sequence of Data Records such that in each record there is a field that contains a reference or link to the next record in the sequence.**

**"**

Above is the

**Singly Linked List**in which each node has data and a link to the next node in the List. Head node is the start of the List. Next of the last node in the List is NULL.

To implement Singly Linked List, I used following way (C++) --

**template <class Etype>**

**class List**

**{**

**struct Node**

**{**

**Etype element;**

**Node *next;**

**Node(Etype E = 0, Node *N = NULL) : element ( E ), next ( N ) { }**

**};**

**Node *head; // start position of head**

**Node *curr; // To optimize and support many operation, think what we can do with this**

**// then various operations, constructors, destructors etc.**

**};**

* In My implementation, head is always in the list even in the case of empty list and head will remain same for lifetime of a list object.

**Insert ::**

**//Insert after curr**

template < class Etype >

void List < Etype > :: insert ( const Etype & data )

{

curr->next = new Node (data, curr->next );

}

See the following figure for more clarification, it shows insert( 30 ) --

**Remove ::**

template < class Etype >

int List < Etype > :: remove ( const Etype & data )

{

if ( Find_Prev( data ) ) // set curr to the node whose next node's element is data

{

Node *toDelete = curr->next;

curr->next = toDelete->next;

delete toDelete;

return 1;

}

return 0;

}

Following figure shows remove( 8 )

## No comments:

## Post a Comment