C Program to Create and Display Linked List | Our PC Solution

C Program to Create and Display Linked List

Create and display linked list
Linked List is a dynamic data structure whose length can be changed (increased or decreased) at run time. Linked list located at random memory location. To create a linked list, malloc must be used for dynamic memory allocation and struct keyword to create data structure ( Such as Node). Node is a one kind of data structure which has two parts. One is data or information(such as array) and another is reference or link(such as pointer). In a singly linked list, first node known as Head and last node known as Tail. Reference part of a tail node must be NULL for singly linked list.



C programming code to create and display singly linked list:


1. Code to Create Linked List:

struct node
{
    int data;
    struct node *next;
}*head=NULL, *last;
/*Node (above code) must be define after the heading and before the main function) */

struct node *new_node;
    char ch;
    do
    {
        new_node=(struct node *)malloc(sizeof(struct node));
        printf("\n Enter the data:");
        scanf("%d",&new_node->data);
        new_node->next=NULL;
        if(head==NULL)
        {
            head=new_node;
            last=new_node;
        }
        else
        {
            last->next=new_node;
            last=new_node;
        }
        fflush(stdin); /*You may use it to remove flush effect if any*/
        printf("\n To create again press any key, if not press Space.\n");
        ch=getch();
    }
    while(ch!=32);// 32 use for Space

2. Code to Display Linked List:

struct node *temp;
    if(head==NULL)
        printf("\n Linked List is empty.");
    else
    {
        printf("\n\t Linked List:\n\t-------------\n\n");
        temp=head;
        while(temp!=NULL)
        {
            printf("\t=>\t%d\n",temp->data);
            temp=temp->next;
        }
    }

3. Source Code to Create and Display Linked List:

//Code by ismail@programmer.net
#include
#include
#define Esc 27
struct node
{
    int data;
    struct node *next;
}*head=NULL,*last;
void create();
//------------------------------------
int main()
{
    char ch;
    while(1)
    {
        printf("\n\n          <@> Main Menu <@>\n          ******************\n");
        printf("\n\t1.Create Linked List\n\t2.Display.\n\tPress ESC to Exit\n");
        printf("\n Please choose, what kind of work you want to do?");
        ch=getch();
		switch(ch)
        {
        case '1':
            create();
            break;
        case '2':
            display();
            break;
        case Esc:
            exit(1);
            return 0;
        default:
            printf("\n\n Invalid Input/ choice.");
        }
    }
    return 0;
}
//-------------------------------------------------
void create()/*Function to create a node or insert a node at last */
{
    struct node *new_node;
    char ch;
    do
    {
        new_node=(struct node *)malloc(sizeof(struct node));
        printf("\n Enter the data:");
        scanf("%d",&new_node->data);
        new_node->next=NULL;
        if(head==NULL)
        {
            head=new_node;
            last=new_node;
        }
        else
        {
            last->next=new_node;
            last=new_node;
        }
        fflush(stdin); /*You may use it to remove flush effect if any*/
        printf("\n To create again press any key, if not press Space.\n");
        ch=getch();
    }
    while(ch!=32);// 32 use for Space
}
//---------------------------------------------
void display()
{
    struct node *temp;
    if(head==NULL)
        printf("\n Linked List is empty.");
    else
    {
        printf("\n\t Linked List:\n\t-------------\n\n");
        temp=head;
        while(temp!=NULL)
        {
            printf("\t=>\t%d\n",temp->data);
            temp=temp->next;
        }
    }
}

4. Download Source Code for Linke List

Download source code to create and display singly linked list from below link:
Download source code to create and display linked list

Try this code, i am sure you will be able to create and display singly linked list. If you have any problems about this code, then write your problems in below, we will try to solve your problems. Don’t go in a hurry, if you enjoyed this article, please subscribe to feeds and drop a comment to show your throught.


0 Comments :

Post a Comment