c++ linked list
c++ linked list
#include<bits/stdc++.h>
using namespace std;
struct node
{
int x,*p;
struct node *next;
};
void addNode(struct node *ptr)
{
int i;
cout<<"Enter the value of x[x>0]";
cin>>i;
if(i<=0)
{
ptr->next=NULL;
}
else
{
struct node *temp=(struct node*)malloc(sizeof(struct node));
ptr->x=i;
ptr->next=temp;
addNode(ptr->next);
}
}
void displayNodes(struct node *ptr)
{
while(ptr->next !=NULL)
{
cout<<ptr->x<<"";
ptr=ptr->next;
}
}
int main()
{
struct node *ptr;
ptr=(struct node*)malloc(sizeof(struct node));
addNode(ptr);
displayNodes(ptr);
return 0;
}

Comments
Post a Comment