Process a Linear Linked List - C Data Structure

C examples for Data Structure:Linked List

Description

Process a Linear Linked List

Demo Code

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct members {//  w ww.j a v a2  s .  c  o  m
   char name[20];
   struct members *next;
};

typedef struct members node;

int menu(void);
void create(node *start);
void display(node *start);
node *insert(node *start);
node *delete1(node *start);
node *location(node *start, char target[]);

int main() {
   node *start = NULL, *temp;
   int selection;

   do {
      selection = menu();
      switch (selection) {
      case 1:
         start = (node *)malloc(sizeof(node));
         temp = start;
         create(start);
         start = temp;
         display(start);
         continue;

      case 2:
         if (start == NULL) {
            printf("\nList is empty! Select the option 1.\n");
            continue;
         }
         start = insert(start);
         display(start);
         continue;

      case 3:
         if (start == NULL) {
            printf("\nList is empty! Select the option 1.\n");
            continue;
         }
         start = delete1(start);
         display(start);
         continue;

      default:
         printf("\nEnd of session.\n");
      }
   } while (selection != 4);

   return(0);
}

int menu(void)
{
   int selection;
   do {
      printf("Enter 1 to create a new linked list\n");
      printf("Enter 2 to insert a component in the list\n");
      printf("Enter 3 to delete a component from the list\n");
      printf("Enter 4 to end the session.\n");
      printf("\nNow enter a number(1, 2, 3, or 4): ");
      scanf("%d", &selection);
      if ((selection < 1) || (selection > 4))
         printf("Invalid Number! Please try again.\n");
   } while ((selection < 1) || (selection > 4));
   return(selection);
}

void create(node *start)
{
   int flag = 1;
   char ch;
   printf("Enter name: ");

   do {
      scanf(" %[^\n]", start->name);
      printf("Any more name?(y/n): ");
      scanf(" %c", &ch);
      if (ch == 'n') {
         flag = 0;
         start->next = NULL;
      }
      else {
         start->next = (node *)malloc(sizeof(node));
         start = start->next;
         printf("Enter name: ");
      }
   } while (flag);

   return;
}

void display(node *start)
{
   int flag = 1;
   if (start == NULL) {
      printf("\nList is empty! Select the option 1.\n");
      return;
   }
   printf("\nNames of all the members in the list:\n");

   do {
      printf("%s\n", start->name);
      if (start->next == NULL)
         flag = 0;
      start = start->next;
   } while (flag);

   return;
}
node *insert(node *start)
{
   int flag = 1;
   node *new1, *before, *tmp;
   char newName[20];
   char target[20];

   printf("Enter name to be inserted: ");
   scanf(" %[^\n]", newName);
   printf("Before which name to place? Type \"last\" if last: ");
   scanf(" %[^\n]", target);

   if (strcmp(target, "last") == 0) {
      tmp = start;

      do {
         start = start->next;
         if (start->next == NULL) {
            new1 = (node *)malloc(sizeof(node));
            strcpy(new1->name, newName);
            start->next = new1;
            new1->next = NULL;
            flag = 0;
         }
      } while (flag);

      start = tmp;
      return(start);
   }

   if (strcmp(start->name, target) == 0) {
      new1 = (node *)malloc(sizeof(node));
      strcpy(new1->name, newName);
      new1->next = start;
      start = new1;
   }
   else {
      before = location(start, target);
      if (before == NULL)
         printf("\nInvalid entry! Please try again\n");
      else {
         new1 = (node *)malloc(sizeof(node));
         strcpy(new1->name, newName);
         new1->next = before->next;
         before->next = new1;
      }
   }
   return(start);
}

node *delete1(node *start)
{
   node *before, *tmp;
   char target[20];

   printf("\nEnter name to be deleted: ");
   scanf(" %[^\n]", target);

   if (strcmp(start->name, target) == 0)
      if (start->next == NULL) {
         free(start);
         start = NULL;
      }
      else
      {
         tmp = start->next;
         free(start);
         start = tmp;
      }
   else {
      before = location(start, target);
      if (before == NULL)
         printf("\nInvalid entry. Please try again.\n");
      else {
         tmp = before->next->next;
         free(before->next);
         before->next = tmp;
      }
   }
   return(start);
}

node *location(node *start, char target[])
{
   int flag = 1;
   if (strcmp(start->next->name, target) == 0)
      return(start);
   else if (start->next == NULL)
      return(NULL);
   else {

      do {
         start = start->next;
         if (strcmp(start->next->name, target) == 0)
            return(start);
         if (start->next == NULL) {
            flag = 0;
            printf("Invalid entry. Please try again.\n");
         }
      } while (flag);

   }
   return(NULL);
}

Result


Related Tutorials