fork(3) download
  1. #include<iostream>
  2. using namespace std;
  3.  
  4. struct node
  5. {
  6. int data;
  7. node *next;
  8. };
  9. typedef node *list;
  10.  
  11. int main()
  12. {
  13. int dat;
  14. char ch;
  15. list first, last;
  16. first=NULL;
  17. last=NULL;
  18. cout<<"Do you want to enter data?(y/n)"<<endl;
  19. cin>>ch;
  20. while(ch=='y'||ch=='Y')
  21. {
  22. cout<<"Enter data"<<endl;
  23. cin>>dat;
  24. if(last==NULL)
  25. {
  26. last=new node;
  27. last->data=dat;
  28. last->next=last;
  29. first=last;
  30. }
  31. else
  32. {
  33. last->next=new node;
  34. last->next->data=dat;
  35. last->next->next=first;
  36. }
  37. cout<<"Enter more data?(y/n)"<<endl;
  38. cin>>ch;
  39.  
  40. }
  41. cout<<"Displaying the ciruclar linked list"<<endl;
  42. cout << "Note: first is at " << first << " and last is at " << last << endl;
  43. while(first!=last)
  44. {
  45. cout<<"Data is "<<last->data<<"\tAt Address "<<last->next<<endl;
  46. last=last->next;
  47. }
  48. return 0;
  49. }
  50.  
Success #stdin #stdout 0s 3476KB
stdin
y 1 y 2 y 3 y 4 n
stdout
Do you want to enter data?(y/n)
Enter data
Enter more data?(y/n)
Enter data
Enter more data?(y/n)
Enter data
Enter more data?(y/n)
Enter data
Enter more data?(y/n)
Displaying the ciruclar linked list
Note: first is at 0x9418008 and last is at 0x9418008