Insert into linked list random position

/******************************************************************************

Welcome to GDB Online.
GDB online is an online compiler and debugger tool for C, C++, Python, PHP, Ruby,
C#, OCaml, VB, Perl, Swift, Prolog, Javascript, Pascal, COBO…


This content originally appeared on DEV Community and was authored by Arry L

/******************************************************************************

Welcome to GDB Online.
  GDB online is an online compiler and debugger tool for C, C++, Python, PHP, Ruby, 
  C#, OCaml, VB, Perl, Swift, Prolog, Javascript, Pascal, COBOL, HTML, CSS, JS
  Code, Compile, Run and Debug online from anywhere in world.

*******************************************************************************/
#include <stdio.h>
#include<stdlib.h>

struct Node {
    int data;
    struct Node* next;
};

struct Node* head;
void Print(){
    Node* temp = head;
    while(temp != NULL){
        printf("%d ",temp->data);
        temp = temp->next;
    }
    printf("\n");
}

void Insert(int data,int n){
    Node* temp1 = new Node();

    temp1->data = data;
    temp1->next = NULL;

    if (n==1){
        temp1->next = head;
        head = temp1;
        return;
    }

    Node* temp2 = head;
    for(int i=0;i<n-2;i++){
        temp2 = temp2->next;
    }

    temp1->next = temp2->next;
    temp2->next = temp1;
}


int main()
{
   head = NULL; //empty list
   Insert(2,1);
   Print();
   Insert(3,2);
   Print();
   Insert(4,1);
   Print();
   Insert(5,2);
   Print();

    return 0;
}


This content originally appeared on DEV Community and was authored by Arry L


Print Share Comment Cite Upload Translate Updates
APA

Arry L | Sciencx (2025-01-11T05:23:07+00:00) Insert into linked list random position. Retrieved from https://www.scien.cx/2025/01/11/insert-into-linked-list-random-position/

MLA
" » Insert into linked list random position." Arry L | Sciencx - Saturday January 11, 2025, https://www.scien.cx/2025/01/11/insert-into-linked-list-random-position/
HARVARD
Arry L | Sciencx Saturday January 11, 2025 » Insert into linked list random position., viewed ,<https://www.scien.cx/2025/01/11/insert-into-linked-list-random-position/>
VANCOUVER
Arry L | Sciencx - » Insert into linked list random position. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2025/01/11/insert-into-linked-list-random-position/
CHICAGO
" » Insert into linked list random position." Arry L | Sciencx - Accessed . https://www.scien.cx/2025/01/11/insert-into-linked-list-random-position/
IEEE
" » Insert into linked list random position." Arry L | Sciencx [Online]. Available: https://www.scien.cx/2025/01/11/insert-into-linked-list-random-position/. [Accessed: ]
rf:citation
» Insert into linked list random position | Arry L | Sciencx | https://www.scien.cx/2025/01/11/insert-into-linked-list-random-position/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.