Advertisement
apl-mhd

Bappy sir MidFinal question

Mar 18th, 2017
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.79 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdio.h>
  3. using namespace std;
  4.  
  5. struct list{
  6.     int data;
  7.  
  8.     struct list *next;
  9.  
  10. };
  11. typedef struct list node;
  12.  
  13. node *Insert(node *head, int n){
  14.  
  15.         node *temp, *prev;
  16.  
  17.         for(int i=1; i<=n; i++){
  18.  
  19.             if(head == NULL){
  20.  
  21.                 head = new node();
  22.                 head->data = i;
  23.                 head->next = NULL;
  24.                 prev = head;
  25.             }
  26.  
  27.             else{
  28.  
  29.                 temp = new node();
  30.                 temp->data = i;
  31.                 temp->next = prev->next;
  32.                 prev->next = temp;
  33.                 prev = temp;
  34.  
  35.  
  36.             }
  37.     }
  38. return head;
  39. }
  40.  
  41. void Display(node *head, int n){
  42.  
  43.         int len,count =0, flag=0;;
  44.  
  45.             if(n ==1){
  46.                 printf("sublist 1:");
  47.                 printf("%d\n", head->data);
  48.  
  49.                 return;
  50.             }
  51.  
  52.         if(n%2 == 1)
  53.             len = (n/2)+1;
  54.         else
  55.             len = n/2;
  56.  
  57.         while(head != NULL){
  58.  
  59.  
  60.             if(flag == 0){
  61.                  printf("Sublist 1: ");
  62.                  flag++;
  63.             }
  64.             if(count == len){
  65.                 printf("\nSublist 2: ");
  66.                 cout<<endl;
  67.             }
  68.             printf("%d ", head->data);
  69.             count++;
  70.             head = head->next;
  71.  
  72.  
  73.         }
  74.  
  75. }
  76.  
  77. node *DeletHead(node *head){
  78.  
  79.     node *prev;
  80.         while(head != NULL){
  81.  
  82.             prev = head;
  83.             delete prev;
  84.  
  85.             head= head->next;
  86.  
  87.  
  88.         }
  89.  
  90. return head;
  91. }
  92.  
  93. int main()
  94. {
  95.  
  96.     node *head;
  97.     head = NULL;
  98.  
  99.     int n;
  100.     cout<<"No of element\n"<<endl;
  101.     cin>>n;
  102.  
  103.     head= Insert(head , n);
  104.  
  105.     Display(head, n);
  106.  
  107.     head = DeletHead(head);
  108.  
  109.    // printf("%d", head);
  110.  
  111.  
  112.  
  113.     return 0;
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement