Linked N?de * next; }; struct N?de *

 

 

Linked
List

 

 

A linked list is a way t? st?re
a c?llecti?n ?f elements. Like an array these can be character ?r integers. Each element in
a linked list is st?red in the f?rm ?f a n?de.

We Will Write a Custom Essay Specifically
For You For Only $13.90/page!


order now

 

  

 

A n?de is a c?llecti?n ?f tw? sub-elements ?r ?arts.

Data – Each n?de has a data element (value st?red
in the n?de).

Next- This ?art ?f
the n?de c?ntains the address ?f the next n?de.

 

 

 

SOLUTION-1

 

 

Representati?n:

 

 

 

 

 

 

 

 

 

 

Basic ?perati?ns

 

 

 Create a
n?de by using the structure called N?de. It c?ntains tw? field ?ne is the data
t? st?re the value ?f the n?de and the ?ther ?art ?f the n?de c?ntains the ??inter
s? it st?res the address ?f the next n?de.

 

 

struct N?de {

    int data;

    struct N?de * next;

};

struct N?de * head = NULL;

 

 

Inserti?n ?perati?n-

Step-1: –

                                    Tem?->data = newData;

                                    Tem?->next = NULL;

 

Step-2:

                                    Tem?->next = rightN?de;

 

Ste?-3:

leftN?de
-> next = tem?;

 

 

Implementati?n:

 

v?id insert(int x){

    

    struct N?de * tem? = new N?de;

    tem? -> data = x;

    tem? -> next = head;

    head = tem?;

}

 

Deleti?n ?perati?n:

Ste?-1:

                                    leftN?de -> next = targetN?de -> next;

 

This
will rem?ve the link that was p?inting t? the target n?de.

Ste?-2:

targetN?de
-> next = NULL

 

 

 

//P?siti?n = n

v?id deleteNode(int n){

        struct N?de * tem?1 = head;

        for(int i=0; i<2; i++){         tem?1 = tem?1 -> next;

        

        struct N?de * tem?2 = tem?1 -> next;

        tem?1 -> next = tem?2 -> next;

        

        delete tem?2;

    }

}

 

 

Print ?perati?n

 

Ste?-1: Create a tem? n?de and initialize with the head ??inter i.e. the address ?f
the first n?de.

Ste?-2: N?w traverse the tem? n?de and ?rint data ?f the current n?de.

 

 

v?id printLL(){

    struct N?de * tem? = head;

    //Traverse

    while(tem? != NULL){

        cout<<" "<data;

        tem? = tem? -> next;

    }

}

 

SOLUTION-2

Types ?f Linked List-

1-   
Singly Linked
List 

2-   
D?ubly Linked
List 

3-   
Circular
Linked List

 

1-   Singly Linked List: Each n?de has a data ?art
and a ??inter t? kee? the address ?f the next n?de. It can be navigated in ?ne
directi?n-

 

 

1-   D?ubly Linked List: In this type ?f linked list each n?de c?ntains
tree ?art,

                        part-1(?rev
??inter): c?ntains the address ?f the previ?us n?de.

                        part-2(data):
c?ntains the data(value).

                        part-3(next
??inter): c?ntains the address ?f the next n?de.

 

 

N?de is implemented as:

 

struct N?de {

    int data;

    struct N?de * next;

    struct N?de * ?rev;

};

 

 

 

SOLUTION-3

 

Array

Linked List

Access ?f elements
 

Rand?m access, we can access the element ?f the array
rand?mly by index number ?f that element.

We can’t access the elements rand?mly we need t?
traverse t? that element since we have address ?f first element ?nly.

Mem?ry Structure
 

All the elements are st?red in mem?ry in contiguous
manner.

Elements ?f linked list can be st?red anywhere in mem?ry,
as each n?de has the address ?f the next n?de.

Inserti?n & Deleti?n
 

It takes m?re time in array as the elements st?red in contiguous
manner in mem?ry.

Easy and efficient as we ?nly need t? add new element
and m?dify the address ?f 2 n?des.

 

Example

pr?blem 1: Su???se y?u
need t? pr?gram an applicati?n that has a pre-defined number ?f categ?ries, but
the exact items in each categ?ry is unkn?wn.

 

S?luti?n: pre-defined
number ?f categ?ries implies that we can use a sim?le static structure like
array t? re?resent the categ?ries. Since we d? n?t kn?w the number ?f items in
each categ?ry, we can re?resent items in each categ?ry using a linked list. S?
what we need is an array ?f linked lists