How to Edit a Linked List in C Programming - dummies

How to Edit a Linked List in C Programming

By Dan Gookin

Because a linked list in C programming is chained together by referencing memory locations, editing is done by modifying those memory locations. For example, if you want to remove the third item from the list, you simply dodge around it by linking the second item to the fourth item. The third item is effectively removed (and lost) by that operation.

image0.jpg

Likewise, you can insert an item into the list by editing the next pointer from the previous item.

image1.jpg

The best way to alter items in a linked list is to have an interactive program that lets you view, add, insert, delete, and edit the various structures. Such a program would be quite long and complex, which is why you’ll find it shown in An Interactive Linked-List Program.

AN INTERACTIVE LINKED-LIST PROGRAM

/* An interactive linked-list program */
/* Dan Gookin, Beginning Programming with C For Dummies */
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
struct typical {
 int value;
 struct typical *next;
};
struct typical *first;
struct typical *current;
struct typical *new;
int menu(void);
void add(void);
void show(void);
void delete(void);
struct typical *create(void);
/* The main function works with input only
 Everything else is handled by a function */
int main()
{
 int choice=''; /* get the while loop to spin */
 first=NULL;
 while(choice!='Q')
 {
 choice=menu();
 switch (choice)
 {
 case 'S':
 show();
 break;
 case 'A':
 add();
 break;
 case 'R':
 delete();
 break;
 case 'Q':
 break;
 default:
 break;
 }
 }
 return(0);
}
/* Display the main menu and collect input */
int menu(void)
{
 int ch;
 printf("S)how, A)dd, R)emove, Q)uit: ");
 ch=getchar();
 while(getchar()!='n') /* remove excess input */
 ;
 return(toupper(ch));
}
/* Add an item to the end of the linked list */
void add(void)
{
 if(first==NULL) /* special case for the first item */
 {
 first=create();
 current=first;
 }
 else  /* find the last item */
 {
 current=first;
 while(current->next) /* last item == NULL */
 current=current->next;
 new=create();
 current->next=new; /* update link */
 current=new;
 }
 printf("Type a value: ");
 scanf("%d",&current->value);
 current->next=NULL;
 while(getchar()!='n') /* remove excess input */
 ;
}
/* Display all structures in the linked list */
void show(void)
{
 int count=1;
 if(first==NULL) /* this list is empty */
 {
 puts("Nothing to show");
 return;
 }
 puts("Showing all records:");
 current=first;
 while(current) /* last record == NULL */
 {
 printf("Record %d: %dn",count,current->value);
 current=current->next;
 count++;
 }
}
/* Remove a record from the list */
void delete(void)
{
 struct typical *previous; /* save previous record */
 int r,c;
 if(first==NULL) /* check for empty list */
 {
 puts("No records to remove");
 return;
 }
 puts("Choose a record to remove:");
 show();
 printf("Record: ");
 scanf("%d",&r);
 while(getchar()!='n') /* remove excess input */
 ;
 c=1;
 current=first;
 previous=NULL; /* first record has no previous */
 while(c!=r)
 {
 if(current==NULL) /* ensure that 'r' is in range */
 {
 puts("Record not found");
 return;
 }
 previous=current;
 current=current->next;
 c++;
 }
 if(previous==NULL) /* special case for first record */
 first=current->next;
 else  /* point previous record at next */
 previous->next=current->next;
 printf("Record %d removed.n",r);
 free(current); /* release memory */
}
/* Build an empty structure and return its address */
struct typical *create(void)
{
 struct typical *a;
 a=(struct typical *)malloc(sizeof(struct typical));
 if(a==NULL)
 {
 puts("Some kind of malloc() error");
 exit(1);
 }
 return(a);
}

Exercise 1: If you have the time, type the source code from An Interactive Linked-List Program into your editor. Typing it in helps you better understand the code. There are comments included to help you see what’s going on. Build and run a few times to get the hang of it.

Be aware that delete is a C++ keyword and cannot be used as a function (or variable) name when compiling C++ code.