linked list - t3houd.files.wordpress.com · basic operations for linear lists: ... • find data in...

18
LINKED LIST

Upload: others

Post on 21-Aug-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: LINKED LIST - t3houd.files.wordpress.com · Basic operations for linear lists: ... • Find data in the list. Linear list. Node. The simple linked list is commonly known as a singly

LINKED LIST

Page 2: LINKED LIST - t3houd.files.wordpress.com · Basic operations for linear lists: ... • Find data in the list. Linear list. Node. The simple linked list is commonly known as a singly

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

Element 1 Element 4Element 3Element 2

Page 3: LINKED LIST - t3houd.files.wordpress.com · Basic operations for linear lists: ... • Find data in the list. Linear list. Node. The simple linked list is commonly known as a singly

Linear list

Linear list

General

Unordered

Ordered

Restricted

Stack

Queue

Page 4: LINKED LIST - t3houd.files.wordpress.com · Basic operations for linear lists: ... • Find data in the list. Linear list. Node. The simple linked list is commonly known as a singly

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

Page 5: LINKED LIST - t3houd.files.wordpress.com · Basic operations for linear lists: ... • Find data in the list. Linear list. Node. The simple linked list is commonly known as a singly

Node

Page 6: LINKED LIST - t3houd.files.wordpress.com · Basic operations for linear lists: ... • Find data in the list. Linear list. Node. The simple linked list is commonly known as a singly

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

Page 7: LINKED LIST - t3houd.files.wordpress.com · Basic operations for linear lists: ... • Find data in the list. Linear list. Node. The simple linked list is commonly known as a singly

single linked list

Empty linked list

linked list with one node

linked list with four node

Page 8: LINKED LIST - t3houd.files.wordpress.com · Basic operations for linear lists: ... • Find data in the list. Linear list. Node. The simple linked list is commonly known as a singly

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

Page 9: LINKED LIST - t3houd.files.wordpress.com · Basic operations for linear lists: ... • Find data in the list. Linear list. Node. The simple linked list is commonly known as a singly

Node declaration

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

Page 10: LINKED LIST - t3houd.files.wordpress.com · Basic operations for linear lists: ... • Find data in the list. Linear list. Node. The simple linked list is commonly known as a singly

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

Page 11: LINKED LIST - t3houd.files.wordpress.com · Basic operations for linear lists: ... • Find data in the list. Linear list. Node. The simple linked list is commonly known as a singly

´ 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

Page 12: LINKED LIST - t3houd.files.wordpress.com · Basic operations for linear lists: ... • Find data in the list. Linear list. Node. The simple linked list is commonly known as a singly

´ The value of current is 2000

´ current->info=17

´ current->link=2800

´ current->link->info=92

Linked list code

Page 13: LINKED LIST - t3houd.files.wordpress.com · Basic operations for linear lists: ... • Find data in the list. Linear list. Node. The simple linked list is commonly known as a singly

´ 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

Page 14: LINKED LIST - t3houd.files.wordpress.com · Basic operations for linear lists: ... • Find data in the list. Linear list. Node. The simple linked list is commonly known as a singly

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

Page 15: LINKED LIST - t3houd.files.wordpress.com · Basic operations for linear lists: ... • Find data in the list. Linear list. Node. The simple linked list is commonly known as a singly

´ 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

Page 16: LINKED LIST - t3houd.files.wordpress.com · Basic operations for linear lists: ... • Find data in the list. Linear list. Node. The simple linked list is commonly known as a singly

´ The list is

´ Now, insert the new node in the list

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

´ P->link = newNode;

Insertion node

Page 17: LINKED LIST - t3houd.files.wordpress.com · Basic operations for linear lists: ... • Find data in the list. Linear list. Node. The simple linked list is commonly known as a singly

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

P->link = newNode;

newNode->link = p->link;

Insertion node

Page 18: LINKED LIST - t3houd.files.wordpress.com · Basic operations for linear lists: ... • Find data in the list. Linear list. Node. The simple linked list is commonly known as a singly

Deletion node

´ See the list

´ To delete q node:

P->link= q->link;

´ delete q;