Sunday, October 10, 2010

Singly Linked List - Insertion At Front

//SINGLY LINKED LIST - Insertion At Front
#include < stdio.h >
#include< conio.h >

struct node
{
int info;
struct node *link;
}       ;

*start=NULL, *last=NULL;

int x;
char ch='y';

void display()
{
//TRAVERSAL
struct node *p;
p=start;
printf("\n LIST IS \n");
do
{
    printf("|%d|%X| --- >",p- >info, p- >link);
    p=p- >link;
} while (p->link!=NULL);
    printf("| %d | %X |",p- >info, p- >link);
}

//Function To Insert Node At Front
void atfront()
{
//dynamically allocate memory
struct node *new1;

      clrscr();

do
{
    printf("\n\n Enter Element ");
    scanf("%d",&x);
    //create new node
    new1=(struct node*) malloc(sizeof(new1));

    new1- >info=x;
    new1- >link=NULL;

    if (start==NULL)
    { printf("first node added\n\n");
      start=new1;
      last=new1;
    }
    else
    {
       new1- >link=start;
       start=new1;
    }
    printf("\n\nDo you want to contnue");
    ch=getch();
}  while (ch=='y') ;

printf("/n/nAddress of first  node :%X ",start);
printf("/n/nAddress of last node :%X \n\n\n",last);
}

void main()
{
//Call InsertionAtFront Function
atfront();
//Print List
display();
}

No comments:

Post a Comment