Advertisement
apl-mhd

huda sir Insert In last

Mar 12th, 2017
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.23 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. using namespace std;
  5. struct list{
  6.  
  7.     int data;
  8.     struct list *next;
  9. };
  10.  
  11. typedef list node;
  12.  
  13. node *insert(node *head){
  14.  
  15.     node *temp;
  16.     for (int i=1; i<5; i++){
  17.         if(head == NULL){
  18.  
  19.             head = new node();
  20.             head->data =i;
  21.             head->next = NULL;
  22.         }
  23.         else{
  24.  
  25.             temp = new node();
  26.             temp->data=i;
  27.             temp->next = head;
  28.             head = temp;
  29.         }
  30.  
  31.  
  32.     }
  33.  
  34.     return head;
  35. }
  36.  
  37. void insertLast(node *head){
  38.  
  39.     node *temp, *pushData;
  40.     temp = head;
  41.  
  42.     while(temp->next != NULL){
  43.  
  44.         temp = temp->next;
  45.     }
  46.  
  47.     pushData = new node();
  48.     pushData->data = 10;
  49.     pushData->next = temp->next;
  50.     temp->next = pushData;
  51.    // temp->next->data =10;
  52.  
  53.     //printf("\n%d", temp->data);
  54. }
  55. void display(node *head){
  56.  
  57.   //  node *temp;
  58.    // temp = head;
  59.     while(head != NULL){
  60.  
  61.  
  62.         printf("%d ", head->data);
  63.         head = head->next;
  64.     }
  65.  
  66. }
  67.  
  68.  
  69. int main()
  70. {
  71.   node *head;
  72.     head =NULL;
  73.   head = insert(head);
  74.  
  75. display(head);
  76. insertLast(head);
  77. cout<<endl;
  78. display(head);
  79.  // printf("%d\n", head->data);
  80.     return 0;
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement