[CS61C FA20] Lecture 05.2 - C Memory Management: Linked List Example — Transcript
Full transcript
- 0:00and welcome back now let's take a look
- 0:03at a linked list example
- 0:05to see more c code and actually see
- 0:07what's going to happen so let's actually
- 0:08play with this right now
- 0:10so we saw structs before here's a struct
- 0:13node
- 0:14so if you have a linked list you have a
- 0:16often a recursive
- 0:17data type right so this is a node that
- 0:19has a character
- 0:21which is a pointer to a value which is
- 0:23some
- 0:24word or something is a linked list node
- 0:26okay and
- 0:28you also have a next pointer and then
- 0:29next pointer is struct node star next
- 0:32that says
- 0:32it's a pointer to a struct node so that
- 0:34means this could be appointed to the
- 0:36next guy and that's to the next guy and
- 0:38then when it's null that means you're at
- 0:39the end of that linked list
- 0:40beautiful right let's do a typedef
- 0:42typedef struct node
- 0:44star list i can now say list and what
- 0:46that means is going to be a pointer to
- 0:48a struct node sorry a list new returns a
- 0:51new
- 0:52pointer to a new guy and that's going to
- 0:53be null so that's a simple way to make a
- 0:55new empty list
- 0:57here's my code add a string to an
- 1:00existing like an existing list okay
- 1:02so list list says capital l list
- 1:06list means that's a pointer to one of
- 1:08these nodes it's going to have my
- 1:10who knows how big that is care star
- 1:12string says this is this is the pointer
- 1:14to the string i want to add okay
- 1:15am i passing in the string itself just a
- 1:17pointer to it so this is great
- 1:18a lot of storage for the linked list a
- 1:20lot of storage for the string these
- 1:21pointers are very lightweight way to
- 1:22pass it into a function
- 1:24first line says struck node star node i
- 1:28probably could have said
- 1:29list here so list list node equals and
- 1:33this probably could have said list
- 1:35list this guy could say list malik size
- 1:38of struck node this makes a new node
- 1:40blue now i got a new node okay
- 1:43and node is that new guy so let's do it
- 1:47there's my linked list here see this
- 1:49first guy
- 1:50and now it points to the next guy points
- 1:51the next side here it's only too long
- 1:53but it's pretty cool
- 1:55i made the new node what's the contents
- 1:57of node remember i have two fields in
- 1:58there i've got
- 1:59uh i've got a value and i've got a next
- 2:01they're both garbage they both garbage i
- 2:03made a new struct but i don't initialize
- 2:05them we know that right
- 2:06but now i'm going to set the first field
- 2:08no there's my arrow notation i mentioned
- 2:10before you have a dot notation or error
- 2:11notation which is a pointer based
- 2:12so no devalue says follow that go to
- 2:15that field for value and
- 2:16you need to make room for the new string
- 2:20so let's actually go forward that's
- 2:22actually so this is
- 2:23now i'm on this line that just finished
- 2:26okay
- 2:28now i need to make room for that new
- 2:31string
- 2:32so node value is find out how long
- 2:35sterling means
- 2:36how long is it but it doesn't include
- 2:38the null
- 2:39terminator so if it's a b c it'll say
- 2:41three because it's only a b and c
- 2:43but you really need a b c and the null
- 2:45terminator so this is extra plus one you
- 2:46always have when you kind of do these
- 2:48mallets of strings
- 2:49so string length of string is the abc
- 2:51that's the three plus one for the null
- 2:52there's the four i'm gonna reserve four
- 2:54if i'm gonna copy abc over
- 2:57okay and i point to that uninitialized
- 2:59that's i'll get an uninitialized value
- 3:02then i call stir copy star copy says
- 3:05let's bring in that value bring it over
- 3:07and here's my abc and there's my
- 3:09my whoops let me do it here just so i
- 3:11draw look right there's my backslash
- 3:14zero as my fourth guy okay so all four
- 3:16get copied over abc
- 3:17and the zero get copied over so does it
- 3:19right and you better have enough room
- 3:20for the zero as i mentioned before okay
- 3:21so i'm good that's not by stir copy here
- 3:24we go let's keep running
- 3:25node next is list watch this
- 3:30node next is this list points to that
- 3:32node next now shit's called sharing
- 3:34they point to the same list okay
- 3:37there's a node there's a node and
- 3:39there's only three nodes now
- 3:41but watch when i return node i'm now
- 3:44returning
- 3:45the head i'm returning the head
- 3:49of the whole thing and now i've just
- 3:51added abc
- 3:52as the front of this list of strings
- 3:54i've now added abc to the front of that
- 3:56pretty cool that makes sense the only
- 3:57thing i would change if i change again
- 3:59this could this could have said
- 4:00list node equals list malik size of
- 4:02struck node but other than that
- 4:03pretty cool code i like it nice so
- 4:06there's again we're going to keep
- 4:07showing you examples the c codes would
- 4:08get more fluent with how to do this but
- 4:09here's an example of how to work
- 4:10with structs and pointers see the next
- 4:12lecture
About this transcript
This page contains the full transcript of [CS61C FA20] Lecture 05.2 - C Memory Management: Linked List Example by CS 61C Departmental, generated from the public captions YouTube serves with the video. The transcript has 883 words across 132 segments, with the original timestamps preserved so you can click any line to jump to that moment in the embedded player.
What you can do with it
Use the transcript to take notes, quote the speaker, build a study guide, generate a summary with ChatGPT or Claude via the YouTube Summary tool, or export it as a timed subtitle file with YouTube to SRT. You can also re-open it in the transcriber to translate the transcript into 100+ languages.
Free YouTube transcript tool
YouTube2Text is a free YouTube transcript generator — no signup, no daily limit. Paste any YouTube link and get the full transcript instantly, with timestamps, click-to-jump, translation to 100+ languages, AI prompts for ChatGPT, Claude, and Gemini, and exports to TXT, SRT, VTT, or Markdown.