Workshop: A Menu-Driven Linked List Manager | Programming in C

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

← Workshop: A Stack and Queue System

Earn 3 college credits for Programming in CRecommended for credit by NCCRS · Computer Science 240 · self-paced · one payment, lifetime access

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.

Compare: C vs C++

Why take it here

Why students take Programming in C with UPI Study

How programs work close to the machineProgramming in C teaches how a program uses memory: variables and data types, control flow, functions, arrays and strings, pointers, files, structures and dynamic memory. C is the base for operating systems, embedded devices and many computer-science courses.
Credit at a fraction of the priceThe same 3 credits can cost $750 or more at a US college. Here it is one payment, self-paced, with lifetime access.
Finish your degree soonerTransfer credits reduce the courses you still owe, so you can graduate earlier or lighten a heavy semester.
Strengthen an applicationA completed college-level course on an official transcript shows admissions and scholarship committees you can do the work.
Study on your own scheduleNo start dates, no deadlines. Watch the lessons, practice, and take the final when you are ready.
130,000+students helped with college creditsACE and NCCRS have over 2,100 cooperating colleges that will consider accepting credits; the final decision is with the universities.

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.

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?
Adding at the front changes one pointer: the new node points at the old head and becomes the new head, with no walking. Adding at the end walks to the last node first and hooks the new node on, unless the list is empty, when the new node becomes the head. Adding Maya and Leo at the end, then Priya at the front, gives Priya, Maya, Leo.
How do you find a node by name in a linked list in C?
Walk the list from the head and compare each node's name with strcmp, returning a pointer to the first node where strcmp gives 0, or NULL if none match. Put the NULL test first, as in while (cur != NULL && strcmp(cur->name, name) != 0), so strcmp never touches a missing node on an empty list or past the tail.
Which node is removed when a linked list has duplicate names?
A remove function that stops at the first match deletes the duplicate nearest the head. If a Leo with 100 points was added at the end before a Leo with 200, deleting Leo removes the one with 100 and leaves the one with 200. To remove one particular record, give each player something unique, such as a player number.
How should a linked list menu handle an add command with a missing score?
Check the value scanf returns. When add should read a name and a score, scanf must return 2; anything less means the score was missing or not a number. The program then prints a usage message, discards the rest of the line, and adds nothing. For example, typing add Sam abc produces the usage line, and Sam does not join the roster.

About the Programming in C course

How many college credits is Programming in C?
Programming in C (Computer Science 240) carries a NCCRS credit recommendation for 3 semester credits. ACE and NCCRS have over 2,100 cooperating colleges that will consider accepting credits; the final decision is with the universities.
How much does Programming in C cost and what is included?
$250, paid once, with lifetime access: all 78 video lessons, a practice quiz after each, the graded work and final exam, and an official transcript when you finish. Students who want several courses choose UPI Study PRO instead.
Which universities accept UPI Study credits?
UPI Study has signed partner agreements with universities such as Thomas Edison State University, University of the People and Charter Oak State College, and works with universities such as Penn State, SNHU and SUNY. ACE and NCCRS have over 2,100 cooperating colleges that will consider accepting credits; the final decision is with the universities. Use "Find my college" on this page to check yours.
How long does Programming in C take?
It is self-paced with no deadlines. The 78 lessons total about 9 hours of video; most students add reading and practice time and finish in a few weeks to a few months, on their own schedule.
How are the exams done?
Quizzes and assignments are online in the course. The final exam is taken online with proctoring; a person on the UPI Study team checks assignments, usually within two days.
How do my credits reach my college?
When the course shows 100% complete, request the official transcript from your dashboard. It is sent to the college you name, and you can send it to more than one. Check your college first with "Find my college" on this page, or ask Athena.

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.

Programming in C
3 credits · NCCRS
Enroll →