Advertisement
AshfaqFardin

Practice Problem (Find Max in linked list)

Aug 25th, 2021
1,470
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.27 KB | None | 0 0
  1. #include<iostream>
  2. using namespace std;
  3.  
  4. struct Node{
  5.     int data;
  6.     Node *next;
  7. };
  8. Node *head = NULL;
  9.  
  10. void appendNode(int);
  11. void display();
  12.  
  13. int find_Max(Node *current, int max){
  14.     if(current == NULL){
  15.         return max;
  16.     }
  17.     if(current == head){
  18.         max = head->data;
  19.     }
  20.     else{
  21.         if(current->data > max){
  22.             max = current->data;
  23.         }
  24.     }
  25.     return find_Max(current->next , max);
  26. }
  27.  
  28. int main()
  29. {appendNode(70);
  30.     appendNode(10);
  31.     appendNode(20);
  32.     appendNode(30);
  33.     appendNode(40);
  34.    
  35.     appendNode(50);
  36.     appendNode(60);
  37.    
  38.  
  39.     int max;
  40.     cout<<find_Max(head, max);
  41.    
  42.    
  43.     return 0;
  44. }
  45.  
  46. void appendNode(int value){
  47.     Node *newNode = new Node;
  48.     newNode->data = value;
  49.     newNode->next = NULL;
  50.     if(head == NULL){
  51.         head = newNode;
  52.     }
  53.     else{
  54.         Node *tNode = head;
  55.         while(tNode->next != NULL){
  56.             tNode = tNode->next;
  57.         }
  58.         tNode->next = newNode;
  59.     }
  60. }
  61.  
  62. void display(){
  63.     if(head == NULL){
  64.          cout<<"No Nodes there";
  65.     }
  66.     else{
  67.         Node *tNode = head;
  68.         while(tNode != NULL){
  69.             cout<<tNode->data<<" ";
  70.             tNode = tNode->next;
  71.         }
  72.     }
  73.     cout<<endl;
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement