0% found this document useful (0 votes)
3 views5 pages

Lesson 5

The document provides an overview of a lecture on data structures and programming, focusing on linked lists. It explains the definition, advantages, and types of linked lists, along with comparisons to arrays and various operations that can be performed on linked lists. Additionally, it includes C++ implementation examples for creating, adding, and displaying elements in a linked list.

Uploaded by

Da Vy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views5 pages

Lesson 5

The document provides an overview of a lecture on data structures and programming, focusing on linked lists. It explains the definition, advantages, and types of linked lists, along with comparisons to arrays and various operations that can be performed on linked lists. Additionally, it includes C++ implementation examples for creating, adding, and displaying elements in a linked list.

Uploaded by

Da Vy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Lecture overview

Data structure & ❑ Overall lectures

Programming II 1.
2.
Introduction to algorithm
Basic data types and statements
7.
8.
Recursive
File IO
9. Pointers
3. Control structures and Loop
10. Linked Lists
4. Array
11. Stacks and Queues
Linked List data structure 5. Data structure 12. Sorting algorithms
6. Sub-programs 13. Trees

C++

Singly linked list Tail


Outline What is Linked list? Head
NULL
❑ A Brief of Outline ❑ Definition
▪ A linked list is a data structure that can store an indefinite amount of elements (dynamic size)
▪ What is linked list? struct Element
▪ In a linked list, each element is linked with each other. struct List
data: integer
▪ Single linked list? Double linked list? Elements in a linked list are accessed sequentially.
*next: Element
n: integer
*head: Element
▪ Each element contains End struct
*tail: Element
▪ What are the advantages of using linked list and array? ✓ Data End struct
✓ A link (pointer)

▪ Linked list implementation in C++ ✓ to its next element (successor)


✓ and/or to its previous element (predecessor)

▪ Examples
▪ Element = called a node

▪ In linked list, the first element is head and the last element is tail
Array Vs. Linked List What is Linked list?
❑ Pros and Con ❑ Type of Linked List

Array Linked List ▪ There are two types of linked lists:


▪ A single linked list is a linked list that has a link to either its successor or predecessor.
▪ Fixed size ▪ Dynamically shrink and grow
▪ A double linked list is a linked list that has both links to successor and predecessor.
▪ Once created, can’t add or reduce ▪ Dynamic memory management A pointer points to the next
Data
TAIL
element (successor)
number of elements to be stored ▪ No random access is allowed
▪ Can random access ▪ Slower access
myList1

▪ Faster access ▪ Elements not in contiguous memory locations myList2 TAIL Last node of the list points to NULL

▪ Elements in contiguous memory locations

Remark List Operations


❑ Operations with a list

▪ A single or double linked list can be called a circular linked list when the last ✓Creating a list ✓Display data in list
element (tail) points to the first element (head). ✓Insert a new element to a list ✓Reverse a list
✓ Insert to beginning, end, at a position
✓Combine two lists
✓Delete an element from a list
✓ Delete to beginning, end, at a position
✓… etc.

✓Search an element in a list

✓Update an element in a list


Circular linked list
Singly linked list
❑ Overview

▪ An example of how a singly linked list is stored List


Element

Singly Linked List (SLL)


Data
Address of next element

List operation Operation on linked list


❑ Operation with a list ❑ Operations

▪ All elements of a linked list can be accessed by Struct Element


data: data_type
▪ First setup a pointer pointing to the first element (node) of the list *next: Element
End struct
▪ Loop to traverse the list until NULL
Struct List ▪ n store number of elements in list.
*head: Element
▪ n is zero when list is first created.
*tail: Element
Then n is incremented by 1 when
▪ One of the disadvantage of the single linked list is n: Integer there is an element added to list.
End struct
▪ Given a pointer A to a node, we can not reach any of the nodes that precede the node (previous
element) to which A is pointing
Examples Examples
❑ Create an element ❑ Add and remove element
Var *head, *tmp : Element • Add a new element containing value 10 to the beginning of the list

tmp  new(size(Element))
• Create an empty list
tmp→ data  10
head  null tmp→ next  head
head  tmp

• Add an element of the list with value 5 Reserve/allocate


memory for this element • Delete the first element from the list
tmp  new(size(Element))
tmp→ data  5 tmp  head
tmp→ next  null head  head → next
head  tmp free(tmp)

Create a list Insertion


❑ A function to create an empty list ❑ Insert an element to the beginning of the list Old list
Procedure insert_be(*ls: List, d: data_type) Tail
Steps to create an empty list: var *E: Element
Function create_list( ) : Pointer of List E  new(size(Element))
1
var *ls : List 1. Create a list variable
E→data  d
2. Allocate memory
ls  new(size(List)) 2
E→next  ls→head
ls→n  0 3. Set 0 to n since we are creating an empty list 3 ls→head  E
ls→head  null if(ls→n ==0) then Tail
4. Head points to null
ls→tail  null 4 ls→tail  E Tail
5. Tail points to null end if
5 ls→n  ls→n + 1
return ls
End procedure

End function Steps to add element to beginning of list


1. Create a new element E
2. Make next pointer of E points to head of list
3. Update E to be head of list Tail
4. Update tail if needed
5. Increase n by 1 (n is number of elements in list)
Display elements in list Implementation

Steps to display element in list


Procedure void(*ls: List) 1. Start from head
2. Move to each element each time
var *tmp: Element 3. …
tmp  ls→head 4. …

while(tmp!=NULL) do
write(tmp→data)
tmp  tmp→next
end while
End procedure

You might also like