Wednesday, 4 April 2018

Linked LIst Practice

The program for the same i show you before but this time i use character as my data instead of integers just for the fun.
The program will just polish your skills .

#include<stdio.h>  /* input / output funtion*/
#include<malloc.h> /*dynamic memory allocation*/

struct node   /*structure of the node*/
{
    char data[56];
    struct node *ref;
};

void create(struct node **);  /*function to create linked list*/
void display(struct node *);   /*display the same on the output screen */

int main()
{
    struct node *head=NULL;
     int i;
    printf("\nEnter no. of element for your linked list:- ");
    scanf("%d",&i);
    while(i>0)
    {
        create(&head);
        i--;
    }
    display(head);
    return 0;
}

void create(struct node **q)
{
    struct node *temp ,*r;
    temp=*q;
    if(*q==NULL)
    {
        temp=(struct node *)malloc(sizeof(struct node));
        printf("\n\tEnter value:- ");
        scanf("%s",&temp->data);
        temp->ref=NULL;
        *q=temp;
    }
    else
    {
        r=(struct node *)malloc(sizeof(struct node));
        printf("\n\tEnter value:- ");
        scanf("%s",&r->data);
        r->ref=NULL;
        while(temp->ref!=NULL)
        {
            temp=temp->ref;
        }
        temp->ref=r;
    }
}

void display(struct node *q)
{
    while(q!=NULL)
    {
        printf("\n\t\tOutput:= %s",q->data);
        q=q->ref;
    }
}


No comments:

Post a Comment