LINKED LIST - t3houd.files.wordpress.com · Basic operations for linear lists: ... • Find data in...

Post on 21-Aug-2020

0 views 0 download

Transcript of LINKED LIST - t3houd.files.wordpress.com · Basic operations for linear lists: ... • Find data in...

LINKED LIST

Linear listThe sequential property of a linear list is basic to its definition and use.

Element 1 Element 4Element 3Element 2

Linear list

Linear list

General

Unordered

Ordered

Restricted

Stack

Queue

Basic operations for linear lists:

• Insert new data in the lists.

• Delete data from a lists.

• Update data in the list.

• Sort data in the lists and

• Find data in the list.

Linear list

Node

The simple linked list is commonly known as a singly linked list because it contains only one link to a single successor.

single linked list

single linked list

Empty linked list

linked list with one node

linked list with four node

Declare node:• Key word class or struct• Name of node• { …………….};

The node contains: • Information • Link

How do write node code

Declare link:Link is pointer with return type same name of node

Node declaration

Struct nodeType{ int info; nodeType *link;};

Linked list codeSee the example

´ 2000 is the address of first node, 2800 is the address of next nod ...etc

´ The information in first node is 17, The information in second node is 92 …etc

´ The 0 in last node means the value is NULL

´ The value of head is 2000

´ head->info=17

´ head->link=2800

´ head->link->info=92

´ Lets current is pointer with the same type of head pointer

´ current=head; // this statement copy the head value to current

Linked list code

´ The value of current is 2000

´ current->info=17

´ current->link=2800

´ current->link->info=92

Linked list code

´ current= current->link;

This statement means the value of current->link copies in current (do not forget they are pointer)

´ current = 2800

´ After execute this statement the current point to second node

Linked list code

Insertion node

´ Steps:

1. Allocate memory for the new node

2. Insert the element-data

3. Point the new node to its successor

4. Point the new node’s predecessor to the new node

´ See this list

´ P is first node and q is second ,now we want to insert new node between p and q with 50 value(info)

´ To create new node use

newNode = new nodeType;

´ This statement Assert(newNode != NULL);

Means if unable to allocate memory space, terminate the program

´ newNode->info=50; // store the 50 in the new node

Insertion node

´ The list is

´ Now, insert the new node in the list

newNode->link = p->link; OR newNode->link = q;

´ P->link = newNode;

Insertion node

´ What do happen for the list when use following statement:

P->link = newNode;

newNode->link = p->link;

Insertion node

Deletion node

´ See the list

´ To delete q node:

P->link= q->link;

´ delete q;