Advertisement
rafid_shad

link list second last element

Oct 13th, 2018
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.65 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4.  
  5. struct node
  6. {
  7.     int a;
  8.     struct node *next;
  9. };
  10. struct node* second(struct node*temp)
  11. {
  12.  
  13.  
  14.     struct node*p;
  15.     p=temp;
  16.     while(temp->next!=NULL)
  17.     {
  18.         p=temp;
  19.         temp=temp->next;
  20.     }
  21.     cout<<p->a<<endl;
  22.  
  23. };
  24.  
  25.  
  26.  
  27. struct node *head=NULL,*temp;
  28.  
  29. int main()
  30. {
  31.     struct node *n1,*n2;
  32.     n1=(struct node*) malloc (sizeof(struct node));
  33.     cout<<"give information for n1"<<endl;
  34.     cin>>n1->a;
  35.     n1->next=NULL;
  36.     head=n1;
  37.     cout<<"give information for n2"<<endl;
  38.     cin>>n2->a;
  39.     n2->next=NULL;
  40.     n1->next=n2;
  41.  
  42.     temp=head;
  43.  
  44.     second(temp);
  45.  
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement