UPI Study › Courses › Programming in C › Free lessons › Lesson 11.8 Free with a free account
Workshop: A Menu-Driven Linked List Manager
Programming in C · Chapter 11: Dynamic Memory, Linked Lists, Stacks, and QueuesSection 11.8 · lesson 78 of 785 min · free with a free account
Free preview
Create a free account to keep watching
It takes a minute and costs nothing. Your free account unlocks all of Chapter 1 and 3 more lessons of Programming in C.
Lesson complete
Earn the 3 college credits for Programming in C
Enrolling adds a quiz after each lesson, the graded work and the official transcript. Recommended for credit by NCCRS.
← Workshop: A Stack and Queue System
In this lesson
Building an interactive roster manager on a singly linked list.
- Add nodes at the front or the end of a list and store the returned head
- Find and remove nodes by name with strcmp, checking for NULL first
- Validate menu input that needs both a name and a score
- Free every node on quit or at the end of input and report the count
Free study notes
Get this lesson's notes by email
The key points and 4 practice questions with answers, in your inbox. One email, free.
One email. Unsubscribe in one click.
Transcript
Everything said in the video, section by section (710 words). Read along, or search it.
The Challenge
A Roster That Changes All Day
Every night, the Starlight Arcade needs a roster: who's playing, and how many points each player has. Players arrive, leave and get looked up all day, so the list keeps changing. In this last workshop, Priya builds a roster manager out of linked nodes. You type commands to add, delete, find and print. And when you quit, every node goes back to the system.
Program Plan
One Head Pointer for the Whole Roster
Plan. Each node is a struct with a name, a score, and a next pointer. The program keeps just one variable for the list: head. The last next is NULL, which is how every loop knows where to stop. Every change returns the new head, as in 11.6, and the menu stores it.
Roster Module
Adding at the Front or the End
roster dot h holds the Player node and the basic tools. new_player asks malloc for one node. snprintf copies the name safely, and cuts it short if it won't fit in sixteen characters with its end mark. add_front is the quick insert. The new node points at the old head, and becomes the head. add_end walks to the last node first, then hooks the new one on. On an empty roster, the new node is the head. print_all numbers the players. And free_all saves next before each free, and counts the nodes it gives back.
Lookup Module
Finding and Removing Players by Name
rfind dot h has the lookups. find_player walks the list, comparing names with strcmp. It returns a pointer to the match, or NULL. remove_player is the prev and cur delete from 11.6, comparing names instead of numbers. A found flag tells the menu whether the name was there. Notice the loop condition: cur not equal NULL comes first, so strcmp never touches a missing node. Priya's early version read the head's name without checking for NULL. On an empty roster, it crashed.
Menu Loop
Commands for Add, Front, Delete, and Find
The menu loop is the one from 11.7. add and front both read a name and a score, and check that scanf found both. If the score is missing, it prints a usage message, and throws away the rest of the line. Otherwise, the first letter picks the function: a for add_end, and f for add_front. Both store the head they return. del and find each read a name, and print No player named when it's missing. And on quit, or when the input ends, free_all gives every node back, and the program says how many.
Quick Check
Front Puts the Last Arrival First
Quick check. Priya adds Maya, then Leo, with add. Then she adds herself with front, and types print. In what order does the roster print? Pause and predict. Run it, and type along. print on an empty roster says so. Then Maya and Leo go in at the end, and Priya at the front. add Sam abc has no score, so it prints the usage line, and Sam isn't added. Then print: Priya, Maya, Leo. front put Priya first, even though she arrived last. And quit frees all three nodes.
Second Session
Deleting the Head and a Missing Name
Now a second session. After the same three adds, find Leo prints his score, and find Zed says there's no such player. del Priya removes the head: the case where prev is NULL. del Zed, a missing name, changes nothing, and says so. print shows Maya and Leo. jump is an unknown command. And quit frees the two nodes that are left.
Quick Check
Two Players With the Same Name
Quick check. Two players named Leo are added: first one with 100, then one with 200. Then Sam types del Leo, and print. Which Leo is left? Pause and predict. Run it: one Leo is left, with 200. remove_player stops at the first match. The Leo with 100 was added first, so he's nearer the head, and he's the one removed. To remove one particular Leo, a real roster needs something unique for each player, like a player number.
Key Takeaways
Every Exit Path Frees Every Node
To sum up. Each node holds a player and a next pointer, and the program keeps only the head. add_front changes one pointer. add_end walks to the last node first. find and delete walk with strcmp, and check for NULL before touching a node. And every exit path frees every node.
Try It
Ideas for Growing the Roster
That completes the course. You can now build, test and free dynamic data structures in C. Keep practicing by growing the roster. Add a count command, a best player command, or an insert that keeps scores in order.
About Programming in C at UPI Study
Programming in C (Computer Science 240) is a self-paced online course of 78 video lessons in 11 chapters, from pseudocode and your first program to data types, decisions, loops, functions, arrays, strings, pointers, files, structures, dynamic memory and linked lists. It carries a NCCRS credit recommendation for 3 semester credits.
Every lesson transcript is free on pages like this one, and the videos are free with a free UPI Study account. Enrolling adds a practice quiz after each lesson, the graded work and final exam that earn the credit, lifetime access, and an official transcript on completion. UPI Study has helped over 130,000 students with college credits. ACE and NCCRS have over 2,100 cooperating colleges that will consider accepting credits; the final decision is with the universities.
Enroll now → Course details Download the syllabus All Computer Science courses Every course with PRO →
Compare: C vs C++
Why take it here
Why students take Programming in C with UPI Study
Where the credit goes
Universities that work with NCCRS
Programming in C carries a NCCRS credit recommendation. These universities work with NCCRS; some are UPI Study's signed partners. ACE and NCCRS have over 2,100 cooperating colleges that will consider accepting credits; the final decision is with the universities.
Thomas Edison State UniversitySigned partner
University of the PeopleSigned partner
Charter Oak State CollegeSigned partner
Strayer UniversitySigned partner
Columbia Southern UniversitySigned partner
Indiana TechSigned partner
Penn StateWorks with NCCRS
Southern New Hampshire UniversityWorks with NCCRS
SUNYWorks with NCCRS
+many more cooperating universitiesFind my college →Find my college → Not listed? Ask Athena in the corner of this page, or book a 15-minute call.
FAQ
Questions students ask us
About Workshop: A Menu-Driven Linked List Manager
What is the difference between adding at the front and at the end of a linked list?
How do you find a node by name in a linked list in C?
Which node is removed when a linked list has duplicate names?
How should a linked list menu handle an add command with a missing score?
About the Programming in C course
How many college credits is Programming in C?
How much does Programming in C cost and what is included?
Which universities accept UPI Study credits?
How long does Programming in C take?
How are the exams done?
How do my credits reach my college?
Ready for the full course? Earn 3 credits in Programming in C.
78 video lessons, a quiz after each one, and the graded work that earns the credit. Recommended for credit by NCCRS.
3 credits · NCCRSEnroll →