Create a Linked List in an Interactive Session - C Data Structure

C examples for Data Structure:Linked List

Description

Create a Linked List in an Interactive Session

Demo Code

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

struct members {// w w w  .j  ava2  s . co m
      char name[20];
      struct members *next;
};

typedef struct members node;

void display(node *start);
void create(node *start);

int main(){
      node *start, *temp;
    
      start = (node *) malloc(sizeof(node));
      temp = start;
      create(start);
    
      start = temp;
      printf("\nNames of all the members:\n");
      display(start);
    
      return(0);
}
void display(node *start)
{
     int flag = 1;
    
     do {
           printf("%s\n", start->name);
           if(start->next == NULL)
             flag = 0;
           start = start->next;
      } while (flag);
    
     return;
}

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;
}

Result


Related Tutorials