Advertisement
Vladpepe

Untitled

Oct 25th, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.65 KB | None | 0 0
  1. // linked list -> Delete a node at nth position
  2.  
  3. #define _CRT_SECURE_NO_WARNINGS
  4. #include "stdio.h"
  5. #include "stdlib.h"
  6.  
  7. struct Node {
  8.     int data;
  9.     struct Node* next;
  10. };
  11.  
  12. struct Node* head;
  13.  
  14. void Insert(int data) {                                         // insert in the end of the list
  15.     struct Node* temp1 = malloc(sizeof(struct Node));           //alloc memory for a new node
  16.     temp1->data = data;                                         //the new node will contain the data that we asked to store
  17.     temp1->next = NULL;                                         //and for now we assume that this is the last node, so it points to NULL
  18.     if (head == NULL) {                                         // head == NULL means the the list is empty now
  19.         head = temp1;                                           // so we have to tell to our list that the head is the node that we have just inserted
  20.     }
  21.     else {
  22.         struct Node* temp2 = head;                              // if the list was no empty, create a new node that store the start of our list
  23.         while (temp2->next != NULL) {                           //search for the end of the list by controlling if the next node is NULL,
  24.             temp2 = temp2->next;                                //if the next adress is NULL it means that the list is ended.
  25.         }
  26.         temp2->next = temp1;                                    //whem we found the last node, we have to put there the adress of our node that we want to insert
  27.         temp1->next = NULL;                                     // of course the last node have to point to NULL
  28.     }
  29.  
  30. }
  31. void Print() {
  32.     struct Node* temp = head;                   //it's the beginning of the list
  33.     printf("list is : ");      
  34.     while (temp != NULL) {                      //while list is not end
  35.         printf("%d  ", temp->data);             //print the data of the node
  36.         temp = temp->next;                      //go to the next node
  37.     }
  38.     printf("\n");
  39. }
  40.  
  41. void Delete(int n) {
  42.     struct Node* temp1 = head;
  43.  
  44.     if (n == 1) {                               //if we have to delete the first node of the list
  45.         head = temp1->next;                     //we had to tell that the new head is the second node of the list
  46.         free(temp1);                            //next we have to dealocate the memory of the deleted node
  47.         return;
  48.     }
  49.     int i;                         
  50.     for (i = 0; i < n - 2;i++) {                //if the node to delete isn't on the first place,we have to scroll the list until we find the right nth position
  51.         temp1 = temp1->next;
  52.  
  53.         struct Node* temp2 = temp1->next;       //save the adress of the node to delete
  54.         temp1->next = temp2->next;              //put the adress of the node after the one to delete in the adress of the node to delete
  55.         free(temp2);                            // free the memory of the deleted node
  56.     }
  57.  
  58.  
  59. }
  60.  
  61. int main() {
  62.  
  63.     head = NULL; // at this stage the list is empty
  64.     Insert(2);
  65.     Insert(4);
  66.     Insert(6);
  67.     Insert(5);      // insert at the end of the list -> List : 2,4,6,5
  68.     Print();
  69.     int n;             
  70.     printf("enter a position\n");
  71.     scanf("%d", &n);                // store in n the position where to delete the node
  72.     Delete(n);
  73.     Print();
  74.  
  75.  }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement