link list - PowerPoint PPT Presentation

About This Presentation
Title:

link list

Description:

demo and working of linklist – PowerPoint PPT presentation

Number of Views:530
Slides: 14
Provided by: dyners
Tags:

less

Transcript and Presenter's Notes

Title: link list


1
  • Link list Link to the list
  • Link list contains DATA in form of NODES
  • Each node contain two parts
  • Data Part
  • Link to next node .

Data
Data
Data
Address To Next node
Address To Next node
Address To Next node
2
  • struct node
  • int data
  • struct node link
  • This structure create a node with int as data
    type to be saved and self linking pointer
  • Now just create node
  • node Node1 (in main program)

3
  • Void main ( )
  • node node1
  • node node2
  • node1-gtdata 32
  • node2-gtdata 64
  • node1-gtlinknode2
  • node2-gtlinkNULL

64
32
NULL
5000
6000
6000
4000
5000
Node 1
Node 2
Note 4000 ,5000 ,6000 are address of nodes in
memory
4
  • struct node temp
  • head temp
  • while(temp-gtlink !NULL)
  • temptemp-gtlink // TRAVERSAL
  • if(temp-gtdata item) // SEARCHING
  • break

Let given be the link list already created Let
item to be searched is 128(item) Head is the
first node of Link list
item ! 64
32
item 128
5000
Item found at third place You can also keep a
counter for that
64
256
512
32
128
5000
6000
7000
8000
NULL
6000
8000
5000
7000
4000
Head
Just increment value of i ie. i just after
while statement , and declare i 0 prior while.
5
  • To insert new node at start position
  • New node is called newnode with data given
  • Newnode-gtlink head
  • Head newnode

64
256
512
128
6000
7000
8000
NULL
5000
6000
8000
5000
7000
Head
6
  • To insert new node in between position
  • Make 2 temporary node i.e temp1, temp2.
  • Traverse these 2 nodes to nodes in which you have
    to insert nodes.
  • E.g we have to insert node in b/w 128 and 256
    (data) node.
  • Let new node to be inserted is newnode
  • newnode-gtlink temp2.
  • temp1-gtlink newnode

7000
temp1
temp2
4000
7
  • Let node to be insert is newnode
  • Create a temp node for traversal
  • Traverse upto last node
  • Temp-gtlink newnode
  • Newnode-gtlink NULL

Temp
Temp
Temp
Temp
64
256
512
128
6000
7000
8000
NULL
4000
NULL
6000
8000
5000
7000
8
  • Make a temp node for traversal
  • Traverse to second node
  • Temp head
  • Free memory of first node if wann do it

Temp
Head
64
256
512
128
6000
7000
8000
NULL
7000
8000
5000
6000
New LinkList
9
  • Make a temp node for traversal
  • Traverse to second last node
  • Temp-gtlink null
  • Free memory of last node if wann do it

Temp
Head
64
256
512
128
6000
7000
8000
NULL
NULL
7000
8000
5000
6000
New LinkList
10
  • Let in given link list we have to delete 3rd node
  • Create 2 temp node i.e. temp1 and temp2
  • Traverse these temp nodes to previous and next
    node of deleting node
  • Now
  • Temp1-gtlinktemp2
  • Free 3rd node if you wann do it

temp1
temp2
HEAD
64
512
128
8000
6000
7000
4000
6000
8000
5000
11
  • Let given be the link list.
  • To create circular link list.
  • Create a temp node i.e temp1 and traverse to last
    node.
  • Create link i.e
  • Temp1-gtlink head

temp
HEAD
32
64
256
512
128
6000
7000
8000
NULL
4000
5000
6000
8000
5000
7000
4000
12
  • Doubly link list is same like linklist except
    that it contains 2 address storing space as 1 in
    link list
  • Add front Address of next node , Add back
    Address of previous node
  • Data data to be save
  • One can also create doubly cyclic link list by
    pointing address of last and first node to each
    other

DATA
DATA
Add front
Add front
Add back
Add back
13
  • Create 2 node i.e head and newnode
  • Head-gtflinknewnode
  • newnode-gtblinkhead
  • Again create a newnode and link accordinginly by
    traversing with temp node
  • temp-gtflinknewnode
  • newnode-gtblinktemp

For cyclic doubly linklist just make flink of
last node to head , and blink of head to
last node
And make all others left pointers to NULL
temp
newnode
newnode
Head
5000
6000
NULL
4000
5000
NULL
Where blink is pointer to previous node and flink
means pointer to next node
Write a Comment
User Comments (0)
About PowerShow.com