Implementing Linked Lists in C

/*this is a described program to implement Smple LL 
 ***inserting from the end and deleting from the begining & displaying***  
*/

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

// ***defining the node***

struct node                          
// creating data structure Node
{
    int data;                        // data type of the node-data
    struct node *link;                // pointer of the same node - points only to the defined data type
};

struct node *START = NULL; /* before creating the 1 st node the */START pointer is null

// ***creating a node***

struct node *createNode()            // returning type - struct node
{
    struct node *n;      
    n = (struct node*)malloc(sizeof(struct node));    
// malloc finction is to allocating space for the creating node   // (data_type*)malloc(sizeof(data_type));
// allocating 2 variables of size 2
    
    return (n);                                        
// returning the memory address of newly created node
}

//***inserting a node to the end***

void insertNode()
{
    struct node *temp, *t;
    temp = createNode();            
/* getting the output(newly created node address) of the createNode method*/

    printf("Insert a data for node ::  ");
    scanf("%d", &temp->data);        

// data will be inserted to the newly created node
    temp->link = NULL;                
// newly created node's pointer will be NULL

    // when it is the first node of the list

    if(START == NULL)
        START = temp;
    else             // when it is not the first node of the list
    {
        t = START;
        while(t->link != NULL)      
/* untill it comes to the newly created node which pointer is NULL*/
        {
            t = t->link;            
// if this expired 't' will get the address of last node
        }
        t->link = temp;               
/* then the pointer of the last node which is NULL will get the address of newly created node*/

        printf("Inserted element is %d\n", temp->data);
    }
}

//***displying the values of the node***

void displayNode()
{
    struct node *t;
    t = START;
    if(START == NULL)
    {
        printf("List is Empty! \n");
    }
    else
    {
        printf("Elements in the list are ::  ");
      
        while(t != NULL)        // when START != NULL
        {
            printf("%d\t", t->data); // printing DATA of the list
            t = t->link; 
/* incrementing link address till it becomes NULL*/
        }
    }
  
}

//***deleting nodes from the begining of the list***

void deleteNode()
{
    if(START == NULL)
        printf("List is Empty! \n");
    else
    {
        struct node *r;
        r = START;
        printf("Deleted node is %d \n", START->data);
        START = START->link;        // START will get the address of the next node
        free(r);                    // free the space of the removed node
    }
  
}

int main()
{
    int ch;
    while(1)
    {
        printf("\n\n1.insert node\n");
        printf("2.display node\n");
        printf("3.delete node\n");
        printf("4.Exit\n");

        printf("Enter choice :: ");

        scanf("%d", &ch);
        switch(ch)
        {
            case(1) : insertNode(); break;
            case(2) : displayNode(); break;
            case(3) : deleteNode(); break;
            case(4)    : exit(1); break;            // exit(1); is a inbuilt command to exit the program
            default : printf("invalid choice\n");

        }

    }

  

return 0;
}

Comments

Popular posts from this blog

An Overview of Sri Lanka's Multilingual Heritage

MS SQL Server with AWS RDS - Connect your local Relational Database to Cloud

Team Building activities to improve Motivation