Top Banner
1 Assist. Prof. Dr. Caner Özcan Week 9 Singly Linked Linear Lists CME 112- Programming Languages II Understanding is the beginning of love. B. Spinoza
43

CME 112- Programming Languages II Week 9 Singly Linked ...canerozcan.net/files/CME112/Week9.pdf · o Inserting/deleting an element at the end. o Randomly accessing any element. o

Jul 31, 2020

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: CME 112- Programming Languages II Week 9 Singly Linked ...canerozcan.net/files/CME112/Week9.pdf · o Inserting/deleting an element at the end. o Randomly accessing any element. o

1

Assist. Prof. Dr. Caner Özcan

Week 9Singly Linked Linear Lists

CME 112- Programming Languages II

Understanding is the beginning of love. B. Spinoza

Page 2: CME 112- Programming Languages II Week 9 Singly Linked ...canerozcan.net/files/CME112/Week9.pdf · o Inserting/deleting an element at the end. o Randomly accessing any element. o

2

► Linked lists are useful to study for some reasons.

► Most obviously, linked lists are a data structure for real programming.

► Knowing the strengths and weaknesses of linked lists will help you thinking about complexity of processing time and memory space of algorithms.

► Linked lists are a good way of understanding the pointers.

► Linked list problems are a nice combination of algorithms and pointer manipulation.

Linked Lists

2

Page 3: CME 112- Programming Languages II Week 9 Singly Linked ...canerozcan.net/files/CME112/Week9.pdf · o Inserting/deleting an element at the end. o Randomly accessing any element. o

3

► A linked list is a data structure that can be changed during execution.

► Consecutive elements are connected with the pointer.

► Last element points to NULL.

► Size can grow or shrink during the execution of the program (It can be made just as long as required)

► It doesn’t made waste memory

Linked Lists

3

Page 4: CME 112- Programming Languages II Week 9 Singly Linked ...canerozcan.net/files/CME112/Week9.pdf · o Inserting/deleting an element at the end. o Randomly accessing any element. o

4

► Arrays are suitable for:

o Inserting/deleting an element at the end.

o Randomly accessing any element.

o Searching the array for a particular value.

► Linked lists are suitable for:

o Inserting an element.

o Deleting an element.

o Applications where sequential access is required.

o In situations where the number of elements can not be predicted beforehand.

Arrays vs. Linked Lists

4

Page 5: CME 112- Programming Languages II Week 9 Singly Linked ...canerozcan.net/files/CME112/Week9.pdf · o Inserting/deleting an element at the end. o Randomly accessing any element. o

5

► List is an abstract data type

► This data type is defined by the user.

► Typically more complex than simple data types like int, float, etc.

► Main aim is;

Linked Lists

5

Page 6: CME 112- Programming Languages II Week 9 Singly Linked ...canerozcan.net/files/CME112/Week9.pdf · o Inserting/deleting an element at the end. o Randomly accessing any element. o

6

► Creating a list

► Traversing the list

► Inserting an item to the list

►Deleting an item from the list

► Concatenating two lists into one

Basic Operations on a List

6

Page 7: CME 112- Programming Languages II Week 9 Singly Linked ...canerozcan.net/files/CME112/Week9.pdf · o Inserting/deleting an element at the end. o Randomly accessing any element. o

7

► Consider the structure of a node in the list as follows:

Linked Lists

7

Page 8: CME 112- Programming Languages II Week 9 Singly Linked ...canerozcan.net/files/CME112/Week9.pdf · o Inserting/deleting an element at the end. o Randomly accessing any element. o

8

► First, a node must be created and the head must be provided to point that node.

Creating a Linear List

8

no

name

age

next

Page 9: CME 112- Programming Languages II Week 9 Singly Linked ...canerozcan.net/files/CME112/Week9.pdf · o Inserting/deleting an element at the end. o Randomly accessing any element. o

9

► If number of nodes is n in the initial linked list:

o Allocate n records, one by one.

o Read in the fields of the records.

o Modify the links of the records so that the chain is formed.

Creating a Linear List

9

Page 10: CME 112- Programming Languages II Week 9 Singly Linked ...canerozcan.net/files/CME112/Week9.pdf · o Inserting/deleting an element at the end. o Randomly accessing any element. o

10Creating a Linear List

10

Page 11: CME 112- Programming Languages II Week 9 Singly Linked ...canerozcan.net/files/CME112/Week9.pdf · o Inserting/deleting an element at the end. o Randomly accessing any element. o

11

► Once the linked list has been constructed and head points to the first node of the list:

o Follow the pointers.

o Display the contents of the nodes as they are traversed.

o Stop when the next pointer points to NULL

Traversing the List

11

Page 12: CME 112- Programming Languages II Week 9 Singly Linked ...canerozcan.net/files/CME112/Week9.pdf · o Inserting/deleting an element at the end. o Randomly accessing any element. o

12

► For insertion:

o A record is created holding the new item.

o The next pointer of the new record is set to link it to the item which is to follow it in the list.

o The next pointer of the item which is to precede it must be modified to point to the new item.

► The problem is to insert a node before a specified node.

o Specified means some value is given for the node (called key).

o In this example, we consider it to be number.

Adding a Node to a List

12

Page 13: CME 112- Programming Languages II Week 9 Singly Linked ...canerozcan.net/files/CME112/Week9.pdf · o Inserting/deleting an element at the end. o Randomly accessing any element. o

13Adding a Node to a List

13

Page 14: CME 112- Programming Languages II Week 9 Singly Linked ...canerozcan.net/files/CME112/Week9.pdf · o Inserting/deleting an element at the end. o Randomly accessing any element. o

14

► When a node is added to the beginning,

o Only one «next» pointer needs to be modified.

• Head is made to point to the new node.

• New node points to the previously first element.

► When a node is added to the end,

o Two «next» pointers need to be modified.

• Last node now points to the new node.

• New node points to NULL

► When a node is added to the middle,

o Two «next» pointers need to be modified.

• Previous node now points to the new node.

• New node points to the next node.

Adding a Node to a List

14

Page 15: CME 112- Programming Languages II Week 9 Singly Linked ...canerozcan.net/files/CME112/Week9.pdf · o Inserting/deleting an element at the end. o Randomly accessing any element. o

15Adding a Node to a List

15

Page 16: CME 112- Programming Languages II Week 9 Singly Linked ...canerozcan.net/files/CME112/Week9.pdf · o Inserting/deleting an element at the end. o Randomly accessing any element. o

16Adding a Node to a List

16

Page 17: CME 112- Programming Languages II Week 9 Singly Linked ...canerozcan.net/files/CME112/Week9.pdf · o Inserting/deleting an element at the end. o Randomly accessing any element. o

17

► The next pointer of the item immediately preceding the one to be deleted is altered, and made to point to the item following the deleted item.

Deleting a Node from the List

17

Page 18: CME 112- Programming Languages II Week 9 Singly Linked ...canerozcan.net/files/CME112/Week9.pdf · o Inserting/deleting an element at the end. o Randomly accessing any element. o

18

► To delete a specified node (give the node whose number field is given)

► Three conditions arise:

o Deleting the first node.

o Deleting the last node.

o Deleting an intermediate node.

Deleting a Node from the List

18

Page 19: CME 112- Programming Languages II Week 9 Singly Linked ...canerozcan.net/files/CME112/Week9.pdf · o Inserting/deleting an element at the end. o Randomly accessing any element. o

19Deleting a Node from the List

19

Page 20: CME 112- Programming Languages II Week 9 Singly Linked ...canerozcan.net/files/CME112/Week9.pdf · o Inserting/deleting an element at the end. o Randomly accessing any element. o

20Deleting a Node from the List

20

Page 21: CME 112- Programming Languages II Week 9 Singly Linked ...canerozcan.net/files/CME112/Week9.pdf · o Inserting/deleting an element at the end. o Randomly accessing any element. o

21Main Function

21

Page 22: CME 112- Programming Languages II Week 9 Singly Linked ...canerozcan.net/files/CME112/Week9.pdf · o Inserting/deleting an element at the end. o Randomly accessing any element. o

22

►A linear list application that has capability of listing nodes in alphabetical order, inserting nodes, deleting a specified node and finding the record that has maximum number of characters in the list.

Singly Linked List Application-1

22

Page 23: CME 112- Programming Languages II Week 9 Singly Linked ...canerozcan.net/files/CME112/Week9.pdf · o Inserting/deleting an element at the end. o Randomly accessing any element. o

23Node Structure

23

Page 24: CME 112- Programming Languages II Week 9 Singly Linked ...canerozcan.net/files/CME112/Week9.pdf · o Inserting/deleting an element at the end. o Randomly accessing any element. o

24Searching Record

24

Page 25: CME 112- Programming Languages II Week 9 Singly Linked ...canerozcan.net/files/CME112/Week9.pdf · o Inserting/deleting an element at the end. o Randomly accessing any element. o

25Adding a New Record

25

Page 26: CME 112- Programming Languages II Week 9 Singly Linked ...canerozcan.net/files/CME112/Week9.pdf · o Inserting/deleting an element at the end. o Randomly accessing any element. o

26Delete Record

26

Page 27: CME 112- Programming Languages II Week 9 Singly Linked ...canerozcan.net/files/CME112/Week9.pdf · o Inserting/deleting an element at the end. o Randomly accessing any element. o

27List Nodes

27

Page 28: CME 112- Programming Languages II Week 9 Singly Linked ...canerozcan.net/files/CME112/Week9.pdf · o Inserting/deleting an element at the end. o Randomly accessing any element. o

28Finding Longest Record

28

Page 29: CME 112- Programming Languages II Week 9 Singly Linked ...canerozcan.net/files/CME112/Week9.pdf · o Inserting/deleting an element at the end. o Randomly accessing any element. o

29Main Function

29

Page 30: CME 112- Programming Languages II Week 9 Singly Linked ...canerozcan.net/files/CME112/Week9.pdf · o Inserting/deleting an element at the end. o Randomly accessing any element. o

30

►Do the following operations on a singly linked list application that holds several information about students such as number, name, midterm exam grade and final exam grade:

o Add / remove record

o Listing students with their final term grade (Midterm exam %40, Final Exam:%60)

o List all information of the student who has highest final term grade

o Calculate the average final term grades of the students in the list

Singly Linked List Application-2

30

Page 31: CME 112- Programming Languages II Week 9 Singly Linked ...canerozcan.net/files/CME112/Week9.pdf · o Inserting/deleting an element at the end. o Randomly accessing any element. o

31

Node Structure

31

Page 32: CME 112- Programming Languages II Week 9 Singly Linked ...canerozcan.net/files/CME112/Week9.pdf · o Inserting/deleting an element at the end. o Randomly accessing any element. o

32Creating List

32

Page 33: CME 112- Programming Languages II Week 9 Singly Linked ...canerozcan.net/files/CME112/Week9.pdf · o Inserting/deleting an element at the end. o Randomly accessing any element. o

33Listing Students

33

Page 34: CME 112- Programming Languages II Week 9 Singly Linked ...canerozcan.net/files/CME112/Week9.pdf · o Inserting/deleting an element at the end. o Randomly accessing any element. o

34Adding New Student

34

Page 35: CME 112- Programming Languages II Week 9 Singly Linked ...canerozcan.net/files/CME112/Week9.pdf · o Inserting/deleting an element at the end. o Randomly accessing any element. o

35Adding New Student

35

Page 36: CME 112- Programming Languages II Week 9 Singly Linked ...canerozcan.net/files/CME112/Week9.pdf · o Inserting/deleting an element at the end. o Randomly accessing any element. o

36Delete Student

36

Page 37: CME 112- Programming Languages II Week 9 Singly Linked ...canerozcan.net/files/CME112/Week9.pdf · o Inserting/deleting an element at the end. o Randomly accessing any element. o

37Delete Student

37

Page 38: CME 112- Programming Languages II Week 9 Singly Linked ...canerozcan.net/files/CME112/Week9.pdf · o Inserting/deleting an element at the end. o Randomly accessing any element. o

38Find Student with Highest Final Term

Grade

38

Page 39: CME 112- Programming Languages II Week 9 Singly Linked ...canerozcan.net/files/CME112/Week9.pdf · o Inserting/deleting an element at the end. o Randomly accessing any element. o

39Calculate Final Term Average of the

Class

39

Page 40: CME 112- Programming Languages II Week 9 Singly Linked ...canerozcan.net/files/CME112/Week9.pdf · o Inserting/deleting an element at the end. o Randomly accessing any element. o

40Main Function

40

Page 41: CME 112- Programming Languages II Week 9 Singly Linked ...canerozcan.net/files/CME112/Week9.pdf · o Inserting/deleting an element at the end. o Randomly accessing any element. o

41Next Week

41

►File Operations

►Sequential Access Files

Page 42: CME 112- Programming Languages II Week 9 Singly Linked ...canerozcan.net/files/CME112/Week9.pdf · o Inserting/deleting an element at the end. o Randomly accessing any element. o

42References

42

►Doç. Dr. Fahri Vatansever, “Algoritma Geliştirme ve

Programlamaya Giriş”, Seçkin Yayıncılık, 12. Baskı, 2015.

►Kaan Aslan, “A’dan Z’ye C Klavuzu 8. Basım”, Pusula

Yayıncılık, 2002.

►Paul J. Deitel, “C How to Program”, Harvey Deitel.

►“A book on C”, All Kelley, İra Pohl

Page 43: CME 112- Programming Languages II Week 9 Singly Linked ...canerozcan.net/files/CME112/Week9.pdf · o Inserting/deleting an element at the end. o Randomly accessing any element. o

43

CANER Ö[email protected] for listening

A n y

Q u e s t i o n s

?