bogdanNiculeasa

LinkedList example

Mar 30th, 2021 (edited)
317
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.32 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. struct Node {
  5.     int data;
  6.     struct Node* next;
  7. };
  8.  
  9. void addToHead(struct Node* &initialHead, int newHeadValue);
  10. void displayList(struct Node* head);
  11. void addToTail(struct Node* initialNode, int tailValue);
  12. int getSize(struct Node* head);
  13. void addAtPosition(struct Node* head, int position, int valueToBeAdded);
  14.  
  15. int main()
  16. {
  17.     struct Node* head = NULL; // Head-ul listei, primul element din lista! NU TE ATINGE DE EL decat daca iti cere programul.
  18.  
  19.     // alocare dinamica: (<tipul elementului unde vei stoca date>)malloc(sizeof(<tipul elementului>))
  20.     head = (struct Node*)malloc(sizeof(struct Node));
  21.     head->data = 6;
  22.  
  23.     addToHead(head, 1);
  24.     addToHead(head, 4);
  25.     addToTail(head, 45);
  26.     addToHead(head, 15);
  27.     addToTail(head, 12);
  28.  
  29.     displayList(head);
  30.     // 15 4 1 6 45 12
  31.     cout << "-------------------\n\n\n";
  32.    
  33.     addAtPosition(head, 3, 999);
  34.     displayList(head);
  35.     // 15 4 999 1 6 45 12
  36.     cout << "Our list has: " << getSize(head) << " elements";
  37.     return 0;
  38. }
  39.  
  40. void addAtPosition(struct Node* head, int position, int valueToBeAdded) {
  41.     int linkedListSize = getSize(head);
  42.     if (position > linkedListSize) {
  43.         cout << "There are fewer elements in the list, compared to the position";
  44.     } else {
  45.         if (position == 1) {
  46.             addToHead(head, valueToBeAdded);
  47.         } else {
  48.             int currentPosition = 1;
  49.             struct Node* currentNode = head;
  50.             while(currentPosition+1 < position) {
  51.                 currentPosition++;
  52.                 currentNode= currentNode->next;
  53.             }
  54.             struct Node* nodeToBeAdded = (struct Node*) malloc(sizeof(struct Node));
  55.             nodeToBeAdded->data = valueToBeAdded;
  56.  
  57.             nodeToBeAdded->next = currentNode->next;
  58.             currentNode->next = nodeToBeAdded;
  59.         }
  60.     }
  61.  
  62. }
  63.  
  64. void addToHead(struct Node* &initialHead, int newHeadValue) {
  65.     struct Node* newHead = (struct Node*)malloc(sizeof(struct Node));
  66.     newHead->data = newHeadValue;
  67.     newHead->next = initialHead;
  68.     initialHead = newHead;
  69. }
  70.  
  71. void addToTail(struct Node* initialNode, int tailValue) {
  72.     if(initialNode == NULL) {
  73.         addToHead(initialNode, tailValue);
  74.     } else {
  75.         struct Node* tempNode = initialNode;
  76.         while(tempNode->next != NULL) {
  77.             tempNode = tempNode->next;
  78.         }
  79.         struct Node* tail = (struct Node*) malloc(sizeof(struct Node)); // Dynamic memory allocation
  80.         tail->data = tailValue;
  81.         // Tempnode will hold the last node
  82.         tempNode->next = tail;
  83.         tail->next = NULL;
  84.     }
  85. }
  86.  
  87.  
  88. int getSize(struct Node* head) {
  89.     if (head == NULL) return 0;
  90.     int size = 0;
  91.     struct Node* tempHead = head;
  92.     while(tempHead != NULL) {
  93.         size++; // whatever operation u want
  94.         tempHead = tempHead->next;
  95.     }
  96.     return size;
  97. }
  98.  
  99. void displayList(struct Node* head){
  100.     if (head == NULL) {
  101.         // List is empty
  102.         cout << "List is empty";
  103.     } else {
  104.         struct Node* tempHead = head;
  105.  
  106.         while (tempHead != NULL) {
  107.             cout << tempHead->data;
  108.             if (tempHead->next != NULL) {
  109.                 cout<< "->";
  110.             }
  111.             tempHead = tempHead->next;
  112.         }
  113.         cout << endl;
  114.     }
  115. }
  116.  
Add Comment
Please, Sign In to add comment