Saturday, April 19, 2014

Day One: Introduction to Linked list

                                     Single Linked List

On first day we will learn basics of linked list, we will first create structure of node(node is basic component of linked list,hope you know what is link list....:P). Following is structure of node:



                  typedef struct node{

                         int data;
                         struct node *next;
                             }node;


Following is program for adding node from front and for traversing...






#include<stdio.h>

#include<malloc.h>

typedef struct node{

    int data;

    struct node *next;

}node;





void show(int *head)            //for traversing linked list

{
    node *temp1;

    temp1=head;

    while(temp1!=NULL)

    {

        printf("%d ",temp1->data);

        temp1=temp1->next;

    }

}



int Insert(int *head)

{

    int data;



    node *temp;

    temp=(node*)malloc(sizeof(node));

    printf("enter data");

    scanf("%d",&data);


    temp->data=data;

    temp->next=head;



    head=temp;

    return head;



}


int main()

{

    node *head=NULL;

    int start=Insert(head);

    start=Insert(start);

    show(start);

}

No comments:

Post a Comment