0% found this document useful (0 votes)
13 views

Lecture # 4 Link List Lecture - PPTX (Autosaved)

link list in data structure

Uploaded by

ns9380550
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Lecture # 4 Link List Lecture - PPTX (Autosaved)

link list in data structure

Uploaded by

ns9380550
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 34

Link List

University of Sialkot
Before Link List
• Pointers
• Classes & objects
• Structures
• Arrays
• Memory allocation
Linked List
A linked list is a data structure which is built from struct
and pointers.
It forms a chain of "nodes" with pointers representing
links of the chain and holding the entire thing together.
A linked list can be represented by a diagram as follows
Start

Name: Ali Name: Talha Name: Bilal Name: Ahmad


Age: 23 Age: 24 Age: 22 Age: 25
Weight: 65 Weight: 62 Weight: 67 Weight: 69

NULL
This linked list has four nodes in it, each with a link to
next node in the series.
he last node has a link to the special value NULL, which a
pointer (whatever its type) can point to, to show that it
ast link in the chain.
There is also another special pointer, called Start, which
points to the first link in the chain so that we can keep
of it.
head

next next next next

element element element element


Baltimore Rome Seattle Toronto

link: The next reference inside a node is a link or pointer to


another node.
W e c a n s ta rt fro m a g iv e n n o d e , a n d m o v e fro m it to th e n e x t
a n d s o o n . T h is is c a lle d lin k h o p p in g o r p o in te r h o p p in g .

head

next next next next

e le m e n t e le m e n t e le m e n t e le m e n t
B a lt im o r e R om e S e a tt le T o ro n to
h e a d : T h e firs t n o d e o f a lin k e d lis t
ta il: T h e la s t n o d e o f a lin k e d lis t - it h a s a n u ll n e x t re fe re n c e .

head tail

next next next next

e le m e n t e le m e n t e le m e n t e le m e n t
B a lt im o r e R om e S e a tt le T o ro n to

S u c h a lin k e d lis t is c a lle d a s in g ly lin k e d lis t .


Read the word from given memory locations
Singly Linked Lists and Arrays
Singly linked list Array
Elements are stored in linear Elements are stored in linear
order, accessible with links. order, accessible with an
index.

Do not have a fixed size. Have a fixed size.

Cannot access the previous Can access the previous


element directly. element easily.

No binary search. Binary search.


arrayname
1.
1. Arrays
Arrays
contiguous
contiguous
direct
directaccess
accessof
ofelements
elements
insertion
insertion//deletion
deletiondifficult
difficult
2.
2. Linked
Linked Lists
Lists
noncontiguous
noncontiguous
must
mustscan
scanfor
forelement
element
insertion
insertion/deletion
/deletioneasy
easy
Types of linked list

• Singly
• Double
• Circular
Structure of linked list

Head pointer stores the address of first node of linked list

Only one pointer i-e Next, pointer to the next node.


C++ Code
Create a Node
class node
{ node(int val)
Public: {

data = val;
int data; Next= NULL;
node* next;
}
// node pointer to the next node in
the list };
Create object
int main()
{

node* n = new node(1);

Cout << n-> data << endl ;


Cout << n-> next;
Output :

Data : 1
Address: 0x0
Traversal in Singly Linked List
head A0 A1 A2

temp
tail
next

temp = temp->next;
Single linked list Operation:
• Traversing
• Deletion
• Insertion
• Searching
• Sorting
Traversing Single Linked List:
• Visited from beginning to end of the list
with Null occurs in the pointer object of the
list element.
• Display its elements or to count the number
of items or nodes in it.
Task
• Write a C++ code to count the number of
nodes in the linked list.

• Create a structure of node (using Struct).


• Crate a list class.
• Create a member function to add items into
list
• Create a member function to count nodes.
Member function to insert node
at the beginning
insert(int x)
{
Temp = new node;
Temp->data = x;
Temp->link = start;
Start = temp;
}
Member function to insert node
at the end
insertatend(int x)
{
Node* temp = head ;
While (temp->next!=NULL)
{
Temp = temp->next;
}
Temp1 = new node;
Temp1->data = x;
Temp1->link = NULL;

}
Member function to insert node
Specific Position
Deletion
Assignment # 2

Implement Link List to insert, display, update and delete


numbers.
Then test your link list with main function.

1. After implementing and testing link list write a function


that returns the data of kth node of the linked list. if no
such node exist, tell the user that node doesn't exist.

2. Write a function to delete the kth node of the linked list.


if no such node exist display the appropriate message.

Note: k is the position number that user enters.

You might also like