#include<stdio.h>
#include<conio.h>
typedef struct nodetype
{
int info;
struct nodetype *link;
}node;
node *link=NULL;
int main()
{
void add(int);
void display();
int showcount();
void del();
int value,count=0;
char choice;
while(1)
{
printf("Do you wish to insert a node into the linked list ( Y/N )?\n\n");
scanf("%c",&choice);
if(choice=='y'||choice=='Y')
{
printf("Enter the info of the node: ");
scanf("%d",&value);
add(value);
fflush(stdin);
}
else
{
break;
}
}
printf("\nThe linked list is:\n\n");
display();
count=showcount();
printf("\nNumber of elements present in the list = %d",count);
del();
getch();
return 0;
}
void add(int data)
{
node *newptr=NULL;
node *ptr;
newptr=(node*)malloc(sizeof(node));
newptr->info=data;
newptr->link=NULL;
if(start==NULL)
{
start=newptr;
}
else
{
ptr=start;
while(ptr->link!=NULL)
{
ptr=ptr->NULL;
}
ptr->link=newptr;
}
}
void display()
{
node *ptr = start;
printf("->");
while(ptr)
{
printf("%d->",ptr->info);
ptr=ptr->link;
}
printf("NULL\n");
}
int showcount()
{
int ncount=0;
node *ptr = start;
while(ptr)
{
ncount++;
ptr=ptr->link;
}
return(ncount);
}
void del()
{
node *ptr;
while(start!=NULL)
{
ptr=start;
start=start->link;
free(ptr);
}
}
No comments:
Post a Comment