Advertisement
apl-mhd

Huda SIr insertData middle and last

Mar 12th, 2017
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.66 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=10; i<=100; i+=10){
  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. node  *insertLast(node *head, int item){
  38.  
  39.     node *temp, *pushData;
  40.     temp = head;
  41.  
  42.     if(head == NULL){
  43.  
  44.         head = new node();
  45.         head->data = item;
  46.         head->next = NULL;
  47.         return head;
  48.     }
  49.  
  50.     if(item >temp->data){
  51.  
  52.         pushData = new node();
  53.         pushData->data=item;
  54.         pushData->next = head;
  55.         head = pushData;
  56.         return head;
  57.     }
  58.  
  59. else{
  60.     while( temp->next !=NULL && temp->next->data >=item  ){
  61.  
  62.         temp = temp->next;
  63.  
  64.     }
  65.  
  66.  
  67.  
  68.     pushData = new node();
  69.     pushData->data = item;
  70.     pushData->next = temp->next;
  71.     temp->next = pushData;
  72.    // temp->next->data =10;
  73.  
  74.     //printf("\n%d", temp->data);
  75.     return head;
  76.     }
  77. }
  78. void display(node *head){
  79.  
  80.   //  node *temp;
  81.    // temp = head;
  82.     while(head != NULL){
  83.  
  84.  
  85.         printf("%d ", head->data);
  86.         head = head->next;
  87.     }
  88.  
  89. }
  90.  
  91.  
  92. int main()
  93. {
  94.   node *head;
  95.     head =NULL;
  96.  head = insert(head);
  97.  
  98. display(head);
  99. head = insertLast(head,2000);
  100. cout<<"after insert data"<<endl;
  101. display(head);
  102.  // printf("%d\n", head->data);
  103.     return 0;
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement