CME 112- Programming Languages II Week 9 Singly Linked ...canerozcan.net/files/CME112/Week9.pdf ·...
Transcript of CME 112- Programming Languages II Week 9 Singly Linked ...canerozcan.net/files/CME112/Week9.pdf ·...
![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](https://reader033.fdocuments.us/reader033/viewer/2022050405/5f825718c94efc157064e93a/html5/thumbnails/1.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022050405/5f825718c94efc157064e93a/html5/thumbnails/2.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022050405/5f825718c94efc157064e93a/html5/thumbnails/3.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022050405/5f825718c94efc157064e93a/html5/thumbnails/4.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022050405/5f825718c94efc157064e93a/html5/thumbnails/5.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022050405/5f825718c94efc157064e93a/html5/thumbnails/6.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022050405/5f825718c94efc157064e93a/html5/thumbnails/7.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022050405/5f825718c94efc157064e93a/html5/thumbnails/8.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022050405/5f825718c94efc157064e93a/html5/thumbnails/9.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022050405/5f825718c94efc157064e93a/html5/thumbnails/10.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022050405/5f825718c94efc157064e93a/html5/thumbnails/11.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022050405/5f825718c94efc157064e93a/html5/thumbnails/12.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022050405/5f825718c94efc157064e93a/html5/thumbnails/13.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022050405/5f825718c94efc157064e93a/html5/thumbnails/14.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022050405/5f825718c94efc157064e93a/html5/thumbnails/15.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022050405/5f825718c94efc157064e93a/html5/thumbnails/16.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022050405/5f825718c94efc157064e93a/html5/thumbnails/17.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022050405/5f825718c94efc157064e93a/html5/thumbnails/18.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022050405/5f825718c94efc157064e93a/html5/thumbnails/19.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022050405/5f825718c94efc157064e93a/html5/thumbnails/20.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022050405/5f825718c94efc157064e93a/html5/thumbnails/21.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022050405/5f825718c94efc157064e93a/html5/thumbnails/22.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022050405/5f825718c94efc157064e93a/html5/thumbnails/23.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022050405/5f825718c94efc157064e93a/html5/thumbnails/24.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022050405/5f825718c94efc157064e93a/html5/thumbnails/25.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022050405/5f825718c94efc157064e93a/html5/thumbnails/26.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022050405/5f825718c94efc157064e93a/html5/thumbnails/27.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022050405/5f825718c94efc157064e93a/html5/thumbnails/28.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022050405/5f825718c94efc157064e93a/html5/thumbnails/29.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022050405/5f825718c94efc157064e93a/html5/thumbnails/30.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022050405/5f825718c94efc157064e93a/html5/thumbnails/31.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022050405/5f825718c94efc157064e93a/html5/thumbnails/32.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022050405/5f825718c94efc157064e93a/html5/thumbnails/33.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022050405/5f825718c94efc157064e93a/html5/thumbnails/34.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022050405/5f825718c94efc157064e93a/html5/thumbnails/35.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022050405/5f825718c94efc157064e93a/html5/thumbnails/36.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022050405/5f825718c94efc157064e93a/html5/thumbnails/37.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022050405/5f825718c94efc157064e93a/html5/thumbnails/38.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022050405/5f825718c94efc157064e93a/html5/thumbnails/39.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022050405/5f825718c94efc157064e93a/html5/thumbnails/40.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022050405/5f825718c94efc157064e93a/html5/thumbnails/41.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022050405/5f825718c94efc157064e93a/html5/thumbnails/42.jpg)
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