cis 190: c/c++ programming - seas.upenn.edu
TRANSCRIPT
![Page 1: CIS 190: C/C++ Programming - seas.upenn.edu](https://reader033.vdocument.in/reader033/viewer/2022042617/62642f2b503a922ab930fe5c/html5/thumbnails/1.jpg)
CIS 190: C/C++ Programming
Lecture 5
Linked Lists
1
![Page 2: CIS 190: C/C++ Programming - seas.upenn.edu](https://reader033.vdocument.in/reader033/viewer/2022042617/62642f2b503a922ab930fe5c/html5/thumbnails/2.jpg)
Outline • (from last class) Memory and Functions
• Linked Lists & Arrays
• Anatomy of a Linked List
– Details On Handling headPtr
• Using Linked Lists
– Creation
– Traversal
– Inserting a Node
– Deleting a Node
• Homework 4B
2
![Page 3: CIS 190: C/C++ Programming - seas.upenn.edu](https://reader033.vdocument.in/reader033/viewer/2022042617/62642f2b503a922ab930fe5c/html5/thumbnails/3.jpg)
Memory and Functions
• how do different types of variables get passed to and returned from functions?
• passing by value
• passing by reference
– implicit: arrays, strings
– explicit: pointers
3
![Page 4: CIS 190: C/C++ Programming - seas.upenn.edu](https://reader033.vdocument.in/reader033/viewer/2022042617/62642f2b503a922ab930fe5c/html5/thumbnails/4.jpg)
Memory and Functions
• some simple examples: int Add(int x, int y);
int answer = Add(1, 2);
void PrintMenu(void);
PrintMenu();
int GetAsciiValue(char c);
int ascii = GetAsciiValue (‘m’);
• all passed by value
4
![Page 5: CIS 190: C/C++ Programming - seas.upenn.edu](https://reader033.vdocument.in/reader033/viewer/2022042617/62642f2b503a922ab930fe5c/html5/thumbnails/5.jpg)
Memory and Functions
• passing arrays to functions void TimesTwo(int array[], int size);
int arr [ARR_SIZE];
/* set values of arr */
TimesTwo(arr, ARR_SIZE);
• arrays of any type are passed by reference – changes made in-function persist
5
![Page 6: CIS 190: C/C++ Programming - seas.upenn.edu](https://reader033.vdocument.in/reader033/viewer/2022042617/62642f2b503a922ab930fe5c/html5/thumbnails/6.jpg)
Memory and Functions
• passing arrays to functions
void TimesTwo(int array[], int size);
void TimesTwo(int * array, int size);
• both of these behave the same way
– they take a pointer to:
• the beginning of an array
• an int – that we (can) treat like an array
6
![Page 7: CIS 190: C/C++ Programming - seas.upenn.edu](https://reader033.vdocument.in/reader033/viewer/2022042617/62642f2b503a922ab930fe5c/html5/thumbnails/7.jpg)
Memory and Functions
• passing strings to functions void PrintName(char name[]);
void PrintName(char *name );
char myName [NAME_SIZE] = “Alice”;
PrintName(myName);
• strings are arrays (of characters)
– implicitly passed by reference
7
![Page 8: CIS 190: C/C++ Programming - seas.upenn.edu](https://reader033.vdocument.in/reader033/viewer/2022042617/62642f2b503a922ab930fe5c/html5/thumbnails/8.jpg)
Memory and Functions
• passing pointers to int to functions
void Square(int *n);
int x = 9;
Square(&x);
• pass address of an integer (in this case, x)
8
![Page 9: CIS 190: C/C++ Programming - seas.upenn.edu](https://reader033.vdocument.in/reader033/viewer/2022042617/62642f2b503a922ab930fe5c/html5/thumbnails/9.jpg)
Memory and Functions
• passing int pointers to function
void Square(int *n);
int x = 9;
int *xPtr = &x;
Square(???);
• pass ???
9
![Page 10: CIS 190: C/C++ Programming - seas.upenn.edu](https://reader033.vdocument.in/reader033/viewer/2022042617/62642f2b503a922ab930fe5c/html5/thumbnails/10.jpg)
Memory and Functions
• passing int pointers to function
void Square(int *n);
int x = 9;
int *xPtr = &x;
Square(xPtr);
• pass xPtr, which is an address to an integer (x)
10
![Page 11: CIS 190: C/C++ Programming - seas.upenn.edu](https://reader033.vdocument.in/reader033/viewer/2022042617/62642f2b503a922ab930fe5c/html5/thumbnails/11.jpg)
Memory and Functions
• returning pointers from functions
CAR* MakeCar(void) {
CAR temp;
return &temp; }
• temp is on the stack – so what happens?
11
![Page 12: CIS 190: C/C++ Programming - seas.upenn.edu](https://reader033.vdocument.in/reader033/viewer/2022042617/62642f2b503a922ab930fe5c/html5/thumbnails/12.jpg)
Memory and Functions
• returning pointers from functions
CAR* MakeCar(void) {
CAR temp;
return &temp; }
• temp is on the stack – so it will be returned to the process when MakeCar() returns!
12
![Page 13: CIS 190: C/C++ Programming - seas.upenn.edu](https://reader033.vdocument.in/reader033/viewer/2022042617/62642f2b503a922ab930fe5c/html5/thumbnails/13.jpg)
Memory and Functions
• returning pointers from functions
CAR* MakeCar(void) {
CAR* temp;
temp = (CAR*) malloc (sizeof(CAR));
return temp; }
• temp is on the heap – so what happens?
13
![Page 14: CIS 190: C/C++ Programming - seas.upenn.edu](https://reader033.vdocument.in/reader033/viewer/2022042617/62642f2b503a922ab930fe5c/html5/thumbnails/14.jpg)
Memory and Functions
• returning pointers from functions
CAR* MakeCar(void) {
CAR* temp;
temp = (CAR*) malloc (sizeof(CAR));
return temp; }
• temp is on the heap – so it belongs to you and will remain on the heap until you free() it
14
![Page 15: CIS 190: C/C++ Programming - seas.upenn.edu](https://reader033.vdocument.in/reader033/viewer/2022042617/62642f2b503a922ab930fe5c/html5/thumbnails/15.jpg)
Outline • (from last class) Memory and Functions
• Linked Lists & Arrays
• Anatomy of a Linked List
– Details On Handling headPtr
• Using Linked Lists
– Creation
– Traversal
– Inserting a Node
– Deleting a Node
• Homework 4B
15
![Page 16: CIS 190: C/C++ Programming - seas.upenn.edu](https://reader033.vdocument.in/reader033/viewer/2022042617/62642f2b503a922ab930fe5c/html5/thumbnails/16.jpg)
What is a Linked List?
16
![Page 17: CIS 190: C/C++ Programming - seas.upenn.edu](https://reader033.vdocument.in/reader033/viewer/2022042617/62642f2b503a922ab930fe5c/html5/thumbnails/17.jpg)
What is a Linked List?
• data structure
• dynamic
– allow easy insertion and deletion
• uses nodes that contain
– data
– pointer to next node in the list
• this is called singly linked, and is what we’ll be using
17
![Page 18: CIS 190: C/C++ Programming - seas.upenn.edu](https://reader033.vdocument.in/reader033/viewer/2022042617/62642f2b503a922ab930fe5c/html5/thumbnails/18.jpg)
Question
• What are some disadvantages of arrays?
• not dynamic
– size is fixed once created
– you can resize it, but you have to do it by hand
– same for insertion & deletion; it’s possible, but it’s difficult and there’s no built-in function for it
• require contiguous block of memory
18
![Page 19: CIS 190: C/C++ Programming - seas.upenn.edu](https://reader033.vdocument.in/reader033/viewer/2022042617/62642f2b503a922ab930fe5c/html5/thumbnails/19.jpg)
Question
• Can we fix these with linked lists? How?
• not dynamic
– linked lists can change size constantly
– can add nodes anywhere in a linked lists
– elements can be removed with no empty spaces
• require contiguous block of memory
– only one node needs to be held contiguously
19
![Page 20: CIS 190: C/C++ Programming - seas.upenn.edu](https://reader033.vdocument.in/reader033/viewer/2022042617/62642f2b503a922ab930fe5c/html5/thumbnails/20.jpg)
Question
• Are there any disadvantages to linked lists?
• harder to search/access than arrayz
• need to manage size/counter for size
• harder to manage memory
– in-list cycles, segfaults, etc.
• pointer to next node takes up more memory
20
![Page 21: CIS 190: C/C++ Programming - seas.upenn.edu](https://reader033.vdocument.in/reader033/viewer/2022042617/62642f2b503a922ab930fe5c/html5/thumbnails/21.jpg)
Outline • (from last class) Memory and Functions
• Linked Lists & Arrays
• Anatomy of a Linked List
– Details On Handling headPtr
• Using Linked Lists
– Creation
– Traversal
– Inserting a Node
– Deleting a Node
• Homework 4B
21
![Page 22: CIS 190: C/C++ Programming - seas.upenn.edu](https://reader033.vdocument.in/reader033/viewer/2022042617/62642f2b503a922ab930fe5c/html5/thumbnails/22.jpg)
Linked List Anatomy
data
next
headPtr
data
next
data
next
data
next
NULL
22
![Page 23: CIS 190: C/C++ Programming - seas.upenn.edu](https://reader033.vdocument.in/reader033/viewer/2022042617/62642f2b503a922ab930fe5c/html5/thumbnails/23.jpg)
Nodes
• a “node” is one element of a linked list
• nodes consist of two parts:
• typically represented as structs
data
next
23
![Page 24: CIS 190: C/C++ Programming - seas.upenn.edu](https://reader033.vdocument.in/reader033/viewer/2022042617/62642f2b503a922ab930fe5c/html5/thumbnails/24.jpg)
Nodes
• a “node” is one element of a linked list
• nodes consist of two parts:
– data stored in node
• typically represented as structs
data
next
24
![Page 25: CIS 190: C/C++ Programming - seas.upenn.edu](https://reader033.vdocument.in/reader033/viewer/2022042617/62642f2b503a922ab930fe5c/html5/thumbnails/25.jpg)
Nodes
• a “node” is one element of a linked list
• nodes consist of two parts:
– data stored in node
– pointer to next node in list
• typically represented as structs
data
next
25
![Page 26: CIS 190: C/C++ Programming - seas.upenn.edu](https://reader033.vdocument.in/reader033/viewer/2022042617/62642f2b503a922ab930fe5c/html5/thumbnails/26.jpg)
Node Definition
• nodes are typically represented as structs
typedef struct node {
int data;
NODEPTR next;
} NODE;
26
![Page 27: CIS 190: C/C++ Programming - seas.upenn.edu](https://reader033.vdocument.in/reader033/viewer/2022042617/62642f2b503a922ab930fe5c/html5/thumbnails/27.jpg)
Node Definition
• nodes are typically represented as structs
typedef struct node * NODEPTR;
typedef struct node {
int data;
NODEPTR next;
} NODE;
• typedef NODEPTR beforehand so that it can be used in the definition of the NODE structure
27
![Page 28: CIS 190: C/C++ Programming - seas.upenn.edu](https://reader033.vdocument.in/reader033/viewer/2022042617/62642f2b503a922ab930fe5c/html5/thumbnails/28.jpg)
Linked List Anatomy
data
next
headPtr
data
next
data
next
data
next
NULL
28
![Page 29: CIS 190: C/C++ Programming - seas.upenn.edu](https://reader033.vdocument.in/reader033/viewer/2022042617/62642f2b503a922ab930fe5c/html5/thumbnails/29.jpg)
List Head
• points to the first NODE in the list
– if there is no list, points to NULL
• headPtr does not contain any data of its own
– only a pointer to a NODE
29
headPtr
![Page 30: CIS 190: C/C++ Programming - seas.upenn.edu](https://reader033.vdocument.in/reader033/viewer/2022042617/62642f2b503a922ab930fe5c/html5/thumbnails/30.jpg)
Linked Lists
data
next
NODE
headPtr
NODEPTR
data
next
NODE
data
next
NODE
data
next
NODE
NULL
30
![Page 31: CIS 190: C/C++ Programming - seas.upenn.edu](https://reader033.vdocument.in/reader033/viewer/2022042617/62642f2b503a922ab930fe5c/html5/thumbnails/31.jpg)
Linked Lists
data
next
NODE @ 0xFFC4
headPtr
NODEPTR @ 0xFFC0
data
next
NODE @ 0xFFEE
data
next
NODE @ 0xFFDC
data
next
NODE @ 0xFFC8
NULL
31
![Page 32: CIS 190: C/C++ Programming - seas.upenn.edu](https://reader033.vdocument.in/reader033/viewer/2022042617/62642f2b503a922ab930fe5c/html5/thumbnails/32.jpg)
Linked Lists
data
0xFFC8
NODE @ 0xFFC4
0xFFC4
NODEPTR @ 0xFFC0
data
NULL
NODE @ 0xFFEE
data
0xFFEE
NODE @ 0xFFDC
data
0xFFDC
NODE @ 0xFFC8
NULL
32
![Page 33: CIS 190: C/C++ Programming - seas.upenn.edu](https://reader033.vdocument.in/reader033/viewer/2022042617/62642f2b503a922ab930fe5c/html5/thumbnails/33.jpg)
Linked Lists
data = 5;
0xFFC8
NODE @ 0xFFC4
0xFFC4
NODEPTR @ 0xFFC0
data = 2;
NULL
NODE @ 0xFFEE
data = 8;
0xFFEE
NODE @ 0xFFDC
data = 3;
0xFFDC
NODE @ 0xFFC8
NULL
33
![Page 34: CIS 190: C/C++ Programming - seas.upenn.edu](https://reader033.vdocument.in/reader033/viewer/2022042617/62642f2b503a922ab930fe5c/html5/thumbnails/34.jpg)
Outline • (from last class) Memory and Functions
• Linked Lists & Arrays
• Anatomy of a Linked List
– Details On Handling headPtr
• Using Linked Lists
– Creation
– Traversal
– Inserting a Node
– Deleting a Node
• Homework 4B
34
![Page 35: CIS 190: C/C++ Programming - seas.upenn.edu](https://reader033.vdocument.in/reader033/viewer/2022042617/62642f2b503a922ab930fe5c/html5/thumbnails/35.jpg)
More About headPtr
• headPtr is a pointer to a NODE
– it has a place where it’s stored in memory
– and it has where it points to in memory
35
![Page 36: CIS 190: C/C++ Programming - seas.upenn.edu](https://reader033.vdocument.in/reader033/viewer/2022042617/62642f2b503a922ab930fe5c/html5/thumbnails/36.jpg)
More About headPtr
• headPtr is a pointer to a NODE
– it has a place where it’s stored in memory
– and it has where it points to in memory
36
0xFFC4 0xDC44
value where it
points to in memory
address where it’s stored in memory
![Page 37: CIS 190: C/C++ Programming - seas.upenn.edu](https://reader033.vdocument.in/reader033/viewer/2022042617/62642f2b503a922ab930fe5c/html5/thumbnails/37.jpg)
Passing Pointers
• when we pass a pointer by value, we pass where it points to in memory
• so we can change the value(s) stored in the memory location to which it points
– but we can’t alter the pointer itself
37
![Page 38: CIS 190: C/C++ Programming - seas.upenn.edu](https://reader033.vdocument.in/reader033/viewer/2022042617/62642f2b503a922ab930fe5c/html5/thumbnails/38.jpg)
Passing Pointers by Value Example
void SquareNum (int *intPtr) {
(*intPtr) = (*intPtr) *
(*intPtr);
}
38
![Page 39: CIS 190: C/C++ Programming - seas.upenn.edu](https://reader033.vdocument.in/reader033/viewer/2022042617/62642f2b503a922ab930fe5c/html5/thumbnails/39.jpg)
Passing Pointers by Value Example
void SquareNum (int *intPtr) {
(*intPtr) = (*intPtr) *
(*intPtr);
}
int x = 4;
int *xPtr = &x;
39
![Page 40: CIS 190: C/C++ Programming - seas.upenn.edu](https://reader033.vdocument.in/reader033/viewer/2022042617/62642f2b503a922ab930fe5c/html5/thumbnails/40.jpg)
Passing Pointers by Value Example
void SquareNum (int *intPtr) {
(*intPtr) = (*intPtr) *
(*intPtr);
}
int x = 4;
int *xPtr = &x;
SquareNum (xPtr);
/* value of x is now 16 */
40
![Page 41: CIS 190: C/C++ Programming - seas.upenn.edu](https://reader033.vdocument.in/reader033/viewer/2022042617/62642f2b503a922ab930fe5c/html5/thumbnails/41.jpg)
Passing Pointers
• when we pass a pointer by reference, we are passing where it is stored in memory
• so we can change both
– where it points to in memory
and
– the values that are stored there
41
![Page 42: CIS 190: C/C++ Programming - seas.upenn.edu](https://reader033.vdocument.in/reader033/viewer/2022042617/62642f2b503a922ab930fe5c/html5/thumbnails/42.jpg)
Passing Pointers by Reference Example
void Reassign (int **ptr,
int *newAddress) {
*ptr = newAddress;
}
42
![Page 43: CIS 190: C/C++ Programming - seas.upenn.edu](https://reader033.vdocument.in/reader033/viewer/2022042617/62642f2b503a922ab930fe5c/html5/thumbnails/43.jpg)
Passing Pointers by Reference Example
void Reassign (int **ptr,
int *newAddress) {
*ptr = newAddress;
}
int x = 3, y = 5;
int *intPtr = &x;
43
![Page 44: CIS 190: C/C++ Programming - seas.upenn.edu](https://reader033.vdocument.in/reader033/viewer/2022042617/62642f2b503a922ab930fe5c/html5/thumbnails/44.jpg)
Passing Pointers by Reference Example
void Reassign (int **ptr,
int *newAddress) {
*ptr = newAddress;
}
int x = 3, y = 5;
int *intPtr = &x;
ReassignPointer (&intPtr, &y);
/* intPtr now points to y */
44
![Page 45: CIS 190: C/C++ Programming - seas.upenn.edu](https://reader033.vdocument.in/reader033/viewer/2022042617/62642f2b503a922ab930fe5c/html5/thumbnails/45.jpg)
headPtr Naming Conventions
• two variable names for headPtr inside functions
• when we pass headPtr by value
– we pass where it points to in memory
– NODEPTR head = address of first node
• when we pass headPtr by reference
– we pass where it’s stored in memory
– NODEPTR *headPtr = where headPtr is stored
45
![Page 46: CIS 190: C/C++ Programming - seas.upenn.edu](https://reader033.vdocument.in/reader033/viewer/2022042617/62642f2b503a922ab930fe5c/html5/thumbnails/46.jpg)
Outline • (from last class) Memory and Functions
• Linked Lists & Arrays
• Anatomy of a Linked List
– Details On Handling headPtr
• Using Linked Lists
– Creation
– Traversal
– Inserting a Node
– Deleting a Node
• Homework 4B
46
![Page 47: CIS 190: C/C++ Programming - seas.upenn.edu](https://reader033.vdocument.in/reader033/viewer/2022042617/62642f2b503a922ab930fe5c/html5/thumbnails/47.jpg)
Building a Linked List from Scratch
47
![Page 48: CIS 190: C/C++ Programming - seas.upenn.edu](https://reader033.vdocument.in/reader033/viewer/2022042617/62642f2b503a922ab930fe5c/html5/thumbnails/48.jpg)
Building a Linked List from Scratch
headPtr
NODEPTR
NULL
48
1. declare a headPtr, and set equal to NULL
![Page 49: CIS 190: C/C++ Programming - seas.upenn.edu](https://reader033.vdocument.in/reader033/viewer/2022042617/62642f2b503a922ab930fe5c/html5/thumbnails/49.jpg)
Building a Linked List from Scratch
headPtr
NODEPTR
NULL
49
1. declare a headPtr, and set equal to NULL
2. allocate space for a node and set to a temporary variable
data
next
NODE
tempNode NULL
![Page 50: CIS 190: C/C++ Programming - seas.upenn.edu](https://reader033.vdocument.in/reader033/viewer/2022042617/62642f2b503a922ab930fe5c/html5/thumbnails/50.jpg)
Building a Linked List from Scratch
headPtr
NODEPTR
NULL
50
1. declare a headPtr, and set equal to NULL
2. allocate space for a node and set to a temporary variable
3. initialize node’s data data = 17;
next
NODE
tempNode NULL
![Page 51: CIS 190: C/C++ Programming - seas.upenn.edu](https://reader033.vdocument.in/reader033/viewer/2022042617/62642f2b503a922ab930fe5c/html5/thumbnails/51.jpg)
Building a Linked List from Scratch
headPtr
NODEPTR
51
1. declare a headPtr, and set equal to NULL
2. allocate space for a node and set to a temporary variable
3. initialize node’s data
4. insert node in list tempNode
NULL
data = 17;
next
NODE
![Page 52: CIS 190: C/C++ Programming - seas.upenn.edu](https://reader033.vdocument.in/reader033/viewer/2022042617/62642f2b503a922ab930fe5c/html5/thumbnails/52.jpg)
Building a Linked List from Scratch
headPtr
NODEPTR
52
• insert another node
tempNode
NULL
data = 17;
next
NODE
data = 2;
next
NODE
![Page 53: CIS 190: C/C++ Programming - seas.upenn.edu](https://reader033.vdocument.in/reader033/viewer/2022042617/62642f2b503a922ab930fe5c/html5/thumbnails/53.jpg)
Building a Linked List from Scratch
headPtr
NODEPTR
53
• insert another node
• and another, etc.
tempNode
NULL
data = 17;
next
NODE
data = 2;
next
NODE
data = 9;
next
NODE
![Page 54: CIS 190: C/C++ Programming - seas.upenn.edu](https://reader033.vdocument.in/reader033/viewer/2022042617/62642f2b503a922ab930fe5c/html5/thumbnails/54.jpg)
Outline • (from last class) Memory and Functions
• Linked Lists & Arrays
• Anatomy of a Linked List
– Details On Handling headPtr
• Using Linked Lists
– Creation
– Traversal
– Inserting a Node
– Deleting a Node
• Homework 4B
54
![Page 55: CIS 190: C/C++ Programming - seas.upenn.edu](https://reader033.vdocument.in/reader033/viewer/2022042617/62642f2b503a922ab930fe5c/html5/thumbnails/55.jpg)
Creating a Node
NODEPTR CreateNode (void)
1. create and allocate memory for a node newNode = (NODEPTR) malloc (sizeof(NODE));
55
![Page 56: CIS 190: C/C++ Programming - seas.upenn.edu](https://reader033.vdocument.in/reader033/viewer/2022042617/62642f2b503a922ab930fe5c/html5/thumbnails/56.jpg)
Creating a Node
NODEPTR CreateNode (void)
1. create and allocate memory for a node newNode = (NODEPTR) malloc (sizeof(NODE));
– cast as NODEPTR, but space for NODE – why?
56
![Page 57: CIS 190: C/C++ Programming - seas.upenn.edu](https://reader033.vdocument.in/reader033/viewer/2022042617/62642f2b503a922ab930fe5c/html5/thumbnails/57.jpg)
Creating a Node
NODEPTR CreateNode (void)
1. create and allocate memory for a node newNode = (NODEPTR) malloc (sizeof(NODE));
– cast as NODEPTR, but space for NODE – why?
2. ensure that memory was allocated
3. initialize data
57
![Page 58: CIS 190: C/C++ Programming - seas.upenn.edu](https://reader033.vdocument.in/reader033/viewer/2022042617/62642f2b503a922ab930fe5c/html5/thumbnails/58.jpg)
Setting a Node’s Data
void SetData (NODEPTR temp,
int data)
• temp is a pointer, but it points to a struct
– use arrow notation to access elements
• or dot star notation
temp->data = data;
(*temp).data = data;
58
![Page 59: CIS 190: C/C++ Programming - seas.upenn.edu](https://reader033.vdocument.in/reader033/viewer/2022042617/62642f2b503a922ab930fe5c/html5/thumbnails/59.jpg)
When “data” Itself is a Struct
typedef struct node {
CIS_CLASS class;
NODEPTR next;
} NODE;
int classNum; char room[ROOM_STR]; char title [TITLE_STR];
next
NODE STRUCT
CIS_CLASS STRUCT (“data”)
59
![Page 60: CIS 190: C/C++ Programming - seas.upenn.edu](https://reader033.vdocument.in/reader033/viewer/2022042617/62642f2b503a922ab930fe5c/html5/thumbnails/60.jpg)
Setting Data when “data” is a Struct
void SetData (NODEPTR temp, int classNum,
char room [ROOM_STR],
char title [TITLE_STR])
temp->class.classNum = classNum;
strcpy(temp->class.room, room);
strcpy(temp->class.title, title);
• the class struct is not a pointer, so we can use just dot notation
60
![Page 61: CIS 190: C/C++ Programming - seas.upenn.edu](https://reader033.vdocument.in/reader033/viewer/2022042617/62642f2b503a922ab930fe5c/html5/thumbnails/61.jpg)
Outline • (from last class) Memory and Functions
• Linked Lists & Arrays
• Anatomy of a Linked List
– Details On Handling headPtr
• Using Linked Lists
– Creation
– Traversal
– Inserting a Node
– Deleting a Node
• Homework 4B
61
![Page 62: CIS 190: C/C++ Programming - seas.upenn.edu](https://reader033.vdocument.in/reader033/viewer/2022042617/62642f2b503a922ab930fe5c/html5/thumbnails/62.jpg)
Traversing a Linked List
• used for many linked list operations
• check to see if list is empty
• use two temporary pointers to keep track of current and previous nodes (prev and curr)
• move through list, setting prev to curr and curr to the next element of the list – continue until you hit the end (or conditions met)
62
![Page 63: CIS 190: C/C++ Programming - seas.upenn.edu](https://reader033.vdocument.in/reader033/viewer/2022042617/62642f2b503a922ab930fe5c/html5/thumbnails/63.jpg)
Special Cases with Linked Lists
• always a separate rule when dealing with the first element in the list (where headPtr points)
– and a separate rule for when the list is empty
• laid out in the code available online, but keep it in mind whenever working with linked lists
– make sure you understand the code before you start using it in your programs
63
![Page 64: CIS 190: C/C++ Programming - seas.upenn.edu](https://reader033.vdocument.in/reader033/viewer/2022042617/62642f2b503a922ab930fe5c/html5/thumbnails/64.jpg)
• set curr to *headPtr, the first NODE in the list
Traversing a Linked List – Step 1 curr
NULL prev
NULL
headPtr
NODEPTR
data
next
NODE
data
next
NODE
data
next
NODE
64
![Page 65: CIS 190: C/C++ Programming - seas.upenn.edu](https://reader033.vdocument.in/reader033/viewer/2022042617/62642f2b503a922ab930fe5c/html5/thumbnails/65.jpg)
headPtr
NODEPTR
data
next
NODE
data
next
NODE
data
next
NODE
NULL
• check if curr == NULL (end of list)
– if it doesn’t, continue
Traversing a Linked List – Step 2 curr
prev
NULL 65
![Page 66: CIS 190: C/C++ Programming - seas.upenn.edu](https://reader033.vdocument.in/reader033/viewer/2022042617/62642f2b503a922ab930fe5c/html5/thumbnails/66.jpg)
headPtr
NODEPTR
data
next
NODE
data
next
NODE
data
next
NODE
NULL
• check if curr == NULL (end of list)
– if it doesn’t, continue
Traversing a Linked List – Step 2 curr
prev
NULL or if your conditions have been met! but you
must always check that curr != NULL first 66
![Page 67: CIS 190: C/C++ Programming - seas.upenn.edu](https://reader033.vdocument.in/reader033/viewer/2022042617/62642f2b503a922ab930fe5c/html5/thumbnails/67.jpg)
• set prev = curr
Traversing a Linked List – Step 3 prev curr
NULL
headPtr
NODEPTR
data
next
NODE
data
next
NODE
data
next
NODE
67
![Page 68: CIS 190: C/C++ Programming - seas.upenn.edu](https://reader033.vdocument.in/reader033/viewer/2022042617/62642f2b503a922ab930fe5c/html5/thumbnails/68.jpg)
headPtr
NODEPTR
data
next
NODE
data
next
NODE
data
next
NODE
NULL
• set curr = curr->next
Traversing a Linked List – Step 4 prev curr
68
![Page 69: CIS 190: C/C++ Programming - seas.upenn.edu](https://reader033.vdocument.in/reader033/viewer/2022042617/62642f2b503a922ab930fe5c/html5/thumbnails/69.jpg)
headPtr
NODEPTR
data
next
NODE
data
next
NODE
data
next
NODE
NULL
• set curr = curr->next
Traversing a Linked List – Step 4 prev curr
69
![Page 70: CIS 190: C/C++ Programming - seas.upenn.edu](https://reader033.vdocument.in/reader033/viewer/2022042617/62642f2b503a922ab930fe5c/html5/thumbnails/70.jpg)
NULL
• continue to repeat steps 2-4 until you reach the end
Traversing a Linked List – Step 5 prev curr
headPtr
NODEPTR
data
next
NODE
data
next
NODE
data
next
NODE
70
![Page 71: CIS 190: C/C++ Programming - seas.upenn.edu](https://reader033.vdocument.in/reader033/viewer/2022042617/62642f2b503a922ab930fe5c/html5/thumbnails/71.jpg)
headPtr
NODEPTR
data
next
NODE
data
next
NODE
data
next
NODE
NULL
• check if curr == NULL (end of list)
– if it doesn’t, continue
Traversing a Linked List – Step 5… curr prev
71
![Page 72: CIS 190: C/C++ Programming - seas.upenn.edu](https://reader033.vdocument.in/reader033/viewer/2022042617/62642f2b503a922ab930fe5c/html5/thumbnails/72.jpg)
• set prev = curr
Traversing a Linked List – Step 5… prev
NULL
curr
headPtr
NODEPTR
data
next
NODE
data
next
NODE
data
next
NODE
72
![Page 73: CIS 190: C/C++ Programming - seas.upenn.edu](https://reader033.vdocument.in/reader033/viewer/2022042617/62642f2b503a922ab930fe5c/html5/thumbnails/73.jpg)
headPtr
NODEPTR
data
next
NODE
data
next
NODE
data
next
NODE
NULL
• set curr = curr->next
Traversing a Linked List – Step 5… prev curr
73
![Page 74: CIS 190: C/C++ Programming - seas.upenn.edu](https://reader033.vdocument.in/reader033/viewer/2022042617/62642f2b503a922ab930fe5c/html5/thumbnails/74.jpg)
headPtr
NODEPTR
data
next
NODE
data
next
NODE
data
next
NODE
NULL
• set curr = curr->next
Traversing a Linked List – Step 5… prev
curr
74
![Page 75: CIS 190: C/C++ Programming - seas.upenn.edu](https://reader033.vdocument.in/reader033/viewer/2022042617/62642f2b503a922ab930fe5c/html5/thumbnails/75.jpg)
headPtr
NODEPTR
data
next
NODE
data
next
NODE
data
next
NODE
NULL
• check if curr == NULL (end of list)
Traversing a Linked List – Step 5…
curr
prev
– if it doesn’t, continue
75
![Page 76: CIS 190: C/C++ Programming - seas.upenn.edu](https://reader033.vdocument.in/reader033/viewer/2022042617/62642f2b503a922ab930fe5c/html5/thumbnails/76.jpg)
• set prev = curr
Traversing a Linked List – Step 5…
NULL
curr prev headPtr
NODEPTR
data
next
NODE
data
next
NODE
data
next
NODE
76
![Page 77: CIS 190: C/C++ Programming - seas.upenn.edu](https://reader033.vdocument.in/reader033/viewer/2022042617/62642f2b503a922ab930fe5c/html5/thumbnails/77.jpg)
NULL
• set curr = curr->next
Traversing a Linked List – Step 5…
curr prev headPtr
NODEPTR
data
next
NODE
data
next
NODE
data
next
NODE
77
![Page 78: CIS 190: C/C++ Programming - seas.upenn.edu](https://reader033.vdocument.in/reader033/viewer/2022042617/62642f2b503a922ab930fe5c/html5/thumbnails/78.jpg)
NULL
• set curr = curr->next
Traversing a Linked List – Step 5…
curr prev headPtr
NODEPTR
data
next
NODE
data
next
NODE
data
next
NODE
78
![Page 79: CIS 190: C/C++ Programming - seas.upenn.edu](https://reader033.vdocument.in/reader033/viewer/2022042617/62642f2b503a922ab930fe5c/html5/thumbnails/79.jpg)
NULL
• check if curr == NULL (end of list)
Traversing a Linked List – Step 5…
curr prev headPtr
NODEPTR
data
next
NODE
data
next
NODE
data
next
NODE
79
![Page 80: CIS 190: C/C++ Programming - seas.upenn.edu](https://reader033.vdocument.in/reader033/viewer/2022042617/62642f2b503a922ab930fe5c/html5/thumbnails/80.jpg)
NULL
• check if curr == NULL (end of list)
Traversing a Linked List – Step 5…
– it does!
– we’ve reached the end of the list
curr prev headPtr
NODEPTR
data
next
NODE
data
next
NODE
data
next
NODE
80
![Page 81: CIS 190: C/C++ Programming - seas.upenn.edu](https://reader033.vdocument.in/reader033/viewer/2022042617/62642f2b503a922ab930fe5c/html5/thumbnails/81.jpg)
Printing the Entire Linked List
void PrintList (NODEPTR head)
• check to see if list is empty
– if so, print out a message
• if not, traverse the linked list
– print out the data of each node
81
![Page 82: CIS 190: C/C++ Programming - seas.upenn.edu](https://reader033.vdocument.in/reader033/viewer/2022042617/62642f2b503a922ab930fe5c/html5/thumbnails/82.jpg)
Outline • (from last class) Memory and Functions
• Linked Lists & Arrays
• Anatomy of a Linked List
– Details On Handling headPtr
• Using Linked Lists
– Creation
– Traversal
– Inserting a Node
– Deleting a Node
• Homework 4B
82
![Page 83: CIS 190: C/C++ Programming - seas.upenn.edu](https://reader033.vdocument.in/reader033/viewer/2022042617/62642f2b503a922ab930fe5c/html5/thumbnails/83.jpg)
Inserting a Node
void Insert (NODEPTR *headPtr,
NODEPTR temp)
• check if list is empty
– if so, temp becomes the first node
• if list is not empty
– traverse the list and insert temp at the end
83
![Page 84: CIS 190: C/C++ Programming - seas.upenn.edu](https://reader033.vdocument.in/reader033/viewer/2022042617/62642f2b503a922ab930fe5c/html5/thumbnails/84.jpg)
NULL
• check if curr == NULL (end of list)
Inserting a Node
curr prev headPtr
NODEPTR
data
next
NODE
84
![Page 85: CIS 190: C/C++ Programming - seas.upenn.edu](https://reader033.vdocument.in/reader033/viewer/2022042617/62642f2b503a922ab930fe5c/html5/thumbnails/85.jpg)
NULL
Inserting a Node
curr prev headPtr
NODEPTR
data
next
NODE
85
• check if curr == NULL (end of list)
• insert the new node by changing where prev->next points to
![Page 86: CIS 190: C/C++ Programming - seas.upenn.edu](https://reader033.vdocument.in/reader033/viewer/2022042617/62642f2b503a922ab930fe5c/html5/thumbnails/86.jpg)
NULL
• check if curr == NULL (end of list)
• insert the new node by changing where prev->next points to
– address of new node
Inserting a Node
prev headPtr
NODEPTR
data
next
NODE
86
data
next
NODE
curr
![Page 87: CIS 190: C/C++ Programming - seas.upenn.edu](https://reader033.vdocument.in/reader033/viewer/2022042617/62642f2b503a922ab930fe5c/html5/thumbnails/87.jpg)
NULL
• check if curr == NULL (end of list)
• insert the new node by changing where prev->next points to
– address of new node
• new node is successfully inserted at end of the list!
Inserting a Node
prev headPtr
NODEPTR
data
next
NODE
87
data
next
NODE
curr
![Page 88: CIS 190: C/C++ Programming - seas.upenn.edu](https://reader033.vdocument.in/reader033/viewer/2022042617/62642f2b503a922ab930fe5c/html5/thumbnails/88.jpg)
Inserting a Node in the Middle
int Insert (NODEPTR *headPtr,
NODEPTR temp,
int where)
• traverse list until you come to place to insert
– CAUTION: don’t go past the end of the list!
• insert temp at appropriate spot
– CAUTION: don’t “lose” any pointers!
• return an integer to convey success/failure
88
![Page 89: CIS 190: C/C++ Programming - seas.upenn.edu](https://reader033.vdocument.in/reader033/viewer/2022042617/62642f2b503a922ab930fe5c/html5/thumbnails/89.jpg)
Inserting a Node – Step 1
• traverse the list until you find where you want to insert temp
data next
NODE
data next
NODE
data next
NODE
NULL
prev
temp
curr
89
![Page 90: CIS 190: C/C++ Programming - seas.upenn.edu](https://reader033.vdocument.in/reader033/viewer/2022042617/62642f2b503a922ab930fe5c/html5/thumbnails/90.jpg)
data next
NODE
data next
NODE
data next
NODE
Inserting a Node – Step 2
• first have temp->next point to what will be the node following it in the list (curr)
temp->next = curr;
prev
temp
curr
90
![Page 91: CIS 190: C/C++ Programming - seas.upenn.edu](https://reader033.vdocument.in/reader033/viewer/2022042617/62642f2b503a922ab930fe5c/html5/thumbnails/91.jpg)
data next
NODE
data next
NODE
data next
NODE
Inserting a Node – Step 3
• then you can have prev->next point to temp as the new next node in the list
prev->next = temp;
prev
temp
curr
91
![Page 92: CIS 190: C/C++ Programming - seas.upenn.edu](https://reader033.vdocument.in/reader033/viewer/2022042617/62642f2b503a922ab930fe5c/html5/thumbnails/92.jpg)
Inserting a Node – Done
data next
NODE
prev
data next
NODE
curr
• temp is now stored in the list between prev and curr
data
next
NODE
temp
92
![Page 93: CIS 190: C/C++ Programming - seas.upenn.edu](https://reader033.vdocument.in/reader033/viewer/2022042617/62642f2b503a922ab930fe5c/html5/thumbnails/93.jpg)
Inserting a Node – Done
data next
NODE
data next
NODE
prev temp
data next
NODE
curr
• temp is now stored in the list between prev and curr
– return a successful code (insert worked)
93
![Page 94: CIS 190: C/C++ Programming - seas.upenn.edu](https://reader033.vdocument.in/reader033/viewer/2022042617/62642f2b503a922ab930fe5c/html5/thumbnails/94.jpg)
Outline • (from last class) Memory and Functions
• Linked Lists & Arrays
• Anatomy of a Linked List
– Details On Handling headPtr
• Using Linked Lists
– Creation
– Traversal
– Inserting a Node
– Deleting a Node
• Homework 4B
94
![Page 95: CIS 190: C/C++ Programming - seas.upenn.edu](https://reader033.vdocument.in/reader033/viewer/2022042617/62642f2b503a922ab930fe5c/html5/thumbnails/95.jpg)
Deleting a Node
int Delete (NODEPTR *headPtr,
int target)
• code is similar to insert
• pass in a way to find the node you want to delete
– traverse list until you find the correct node:
curr->data == target
• return an integer to convey success/failure
95
![Page 96: CIS 190: C/C++ Programming - seas.upenn.edu](https://reader033.vdocument.in/reader033/viewer/2022042617/62642f2b503a922ab930fe5c/html5/thumbnails/96.jpg)
data next
NODEPTR
data next
NODEPTR
data next
NODEPTR
curr
Deleting a Node – Step 1
prev
• traverse the list, searching until curr->data
matches target
96
![Page 97: CIS 190: C/C++ Programming - seas.upenn.edu](https://reader033.vdocument.in/reader033/viewer/2022042617/62642f2b503a922ab930fe5c/html5/thumbnails/97.jpg)
data next
NODEPTR
data next
NODEPTR
data next
NODEPTR
curr
Deleting a Node – Step 1
prev
• traverse the list, searching until curr->data
matches target
97
but don’t forget, you must always check that curr != NULL first
![Page 98: CIS 190: C/C++ Programming - seas.upenn.edu](https://reader033.vdocument.in/reader033/viewer/2022042617/62642f2b503a922ab930fe5c/html5/thumbnails/98.jpg)
data next
NODEPTR
data next
NODEPTR
data next
NODEPTR
curr
Deleting a Node – Step 2
prev
• “remove” curr from the list by setting prev->next to curr->next
98
![Page 99: CIS 190: C/C++ Programming - seas.upenn.edu](https://reader033.vdocument.in/reader033/viewer/2022042617/62642f2b503a922ab930fe5c/html5/thumbnails/99.jpg)
data next
NODEPTR
data next
NODEPTR
data next
NODEPTR
curr
Deleting a Node – Step 3
prev
• free the memory used by curr and set pointers to NULL
99
![Page 100: CIS 190: C/C++ Programming - seas.upenn.edu](https://reader033.vdocument.in/reader033/viewer/2022042617/62642f2b503a922ab930fe5c/html5/thumbnails/100.jpg)
data next
NODEPTR
data next
NODEPTR
data next
NODEPTR
curr
Deleting a Node – Step 3
prev
• free the memory used by curr and set pointers to NULL
curr->next = NULL;
NULL
100
![Page 101: CIS 190: C/C++ Programming - seas.upenn.edu](https://reader033.vdocument.in/reader033/viewer/2022042617/62642f2b503a922ab930fe5c/html5/thumbnails/101.jpg)
data next
NODEPTR
data next
NODEPTR
data next
NODEPTR
curr
Deleting a Node – Step 3
prev
• free the memory used by curr and set pointers to NULL
curr->next = NULL;
free(curr);
NULL
101
![Page 102: CIS 190: C/C++ Programming - seas.upenn.edu](https://reader033.vdocument.in/reader033/viewer/2022042617/62642f2b503a922ab930fe5c/html5/thumbnails/102.jpg)
data next
NODEPTR
data next
NODEPTR
curr
Deleting a Node – Step 3
prev
• free the memory used by curr and set pointers to NULL
curr->next = NULL;
free(curr);
102
![Page 103: CIS 190: C/C++ Programming - seas.upenn.edu](https://reader033.vdocument.in/reader033/viewer/2022042617/62642f2b503a922ab930fe5c/html5/thumbnails/103.jpg)
data next
NODEPTR
data next
NODEPTR
curr
Deleting a Node – Step 3
prev
• free the memory used by curr and set pointers to NULL
curr->next = NULL;
free(curr);
curr = NULL;
NULL
103
![Page 104: CIS 190: C/C++ Programming - seas.upenn.edu](https://reader033.vdocument.in/reader033/viewer/2022042617/62642f2b503a922ab930fe5c/html5/thumbnails/104.jpg)
data next
NODEPTR
data next
NODEPTR
curr
Deleting a Node – Step 3
prev
• free the memory used by curr and set pointers to NULL
curr->next = NULL;
free(curr);
curr = NULL;
NULL
104
![Page 105: CIS 190: C/C++ Programming - seas.upenn.edu](https://reader033.vdocument.in/reader033/viewer/2022042617/62642f2b503a922ab930fe5c/html5/thumbnails/105.jpg)
Outline • (from last class) Memory and Functions
• Linked Lists & Arrays
• Anatomy of a Linked List
– Details On Handling headPtr
• Using Linked Lists
– Creation
– Traversal
– Inserting a Node
– Deleting a Node
• Homework 4B
105
![Page 106: CIS 190: C/C++ Programming - seas.upenn.edu](https://reader033.vdocument.in/reader033/viewer/2022042617/62642f2b503a922ab930fe5c/html5/thumbnails/106.jpg)
Homework 4B
• Karaoke
• heavy on pointers and memory management
• think before you attack
• start early
• test often (don’t forget edge cases)
• use a debugger when needed 106
![Page 107: CIS 190: C/C++ Programming - seas.upenn.edu](https://reader033.vdocument.in/reader033/viewer/2022042617/62642f2b503a922ab930fe5c/html5/thumbnails/107.jpg)
Linked List Code for HW4B
• code for all of these functions is available on the Lectures page
• comments explain each step
• you can use this code in your Homework 4B, or as the basis for similar functions
107