Saturday, August 21, 2010

Memory allocation function

#include<stdio.h>
#include<conio.h>
#include<malloc.h>
void main()
{
struct node
{
int num;
struct node *ptr;
};
typedef struct node NODE;
NODE *head, *first, *temp;
int count=0;
int choice=1;
first=NULL;
while(choice)
{
    head=(NODE *)malloc(sizeof(NODE));
    printf("Enter the data item\n");
    scanf("%d",&head->num);
      if(first!=NULL)
        {
          temp->ptr=head;
          temp=head;
        }
else
        {
          first=temp=head;
        }
fflush(stdin);
printf("Do you want to continue(type 0 or 1)?\n");
scanf("%d",&choice);
}

temp->ptr=NULL;
temp=first;
printf("Status of the linked list is\n");
while(temp!=NULL)
    {
        printf("%d",temp->num);
        count++;
        temp=temp->ptr;
    }
printf("NULL");
printf("NO of nodes in the list =%d\n",count);
getch();
}

/*output of the program*/

Enter the data item
1
Do you want to continue(type 0 or 1)?
1

Enter the data item
2
Do you want to continue(type 0 or 1)?
1
Enter the data item
3
Do you want to continue(type 0 or 1)?
1
Enter the data item
4
Do you want to continue(type 0 or 1)?
0
Status of the linked list is
1234NULLNO of nodes in the list =4

No comments:

Post a Comment

Search This Blog