Facebook
From Torrid Lemur, 2 Months ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 90
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. class node{
  4. public:
  5.     int data;
  6.     node* next;
  7.  
  8.  
  9. };
  10. node*start=NULL;
  11. void create(int n)
  12. {
  13. node*newn;
  14. node*temp;
  15. for(int i=0;i<n;i++){
  16.     newn=new node;
  17.     cout<<"\nEnter Data: ";
  18.     cin>>newn->data;
  19.     newn->next=NULL;
  20.     if(start==NULL){
  21.         start=newn;
  22.         temp=start;
  23.  
  24.     }
  25.     else{
  26.         temp->next=newn;
  27.         temp=temp->next;
  28.     }
  29. }
  30.  
  31.  
  32.  
  33. }
  34. void first(){
  35.  
  36. node*newn=new node;
  37. int a;
  38. cout<<"Enter the data u want to insert at the first place:";
  39. cin>>a;
  40. newn->data=a;
  41. newn->next=start;
  42. start=newn;
  43.  
  44. }
  45. void last(){
  46. node*newn=new node;
  47. int b;
  48. cout<<"Enter the value you want to insert at the last place:";
  49. cin>>b;
  50. newn->data=b;
  51. newn->next=NULL;
  52. node*temp=start;
  53. while(temp->next!=NULL){
  54.     temp=temp->next;
  55. }
  56. temp->next=newn;
  57.  
  58.  
  59. }
  60.  
  61. void middle(){
  62. node*newn=new node;
  63. int c;
  64. int pos;
  65. cout<<"Enter position: ";
  66. cin>>pos;
  67. cout<<"enter the data you want to insert:";
  68. cin>>c;
  69. newn->data=c;
  70. node*temp=start;
  71. for(int i=1;i<pos-1;i++){
  72.     temp=temp->next;
  73.  
  74. }
  75. newn->next=temp->next;
  76. temp->next=newn;
  77.  
  78.  
  79.  
  80. }
  81. void display(){
  82. node*temp=start;
  83. while(temp!=NULL){
  84.     cout<<temp->data<<" "<<temp<<" "<<temp->next<<endl;
  85.     temp=temp->next;
  86. }
  87.  
  88. }
  89. int main(){
  90. int n;
  91. cout<<"enter how many data you want to see:";
  92. cin>>n;
  93. create(n);
  94. display();
  95. first();
  96. display();
  97. last();
  98. display();
  99. middle();
  100. display();
  101.  
  102. }
  103.