Guias

2.5 Insertion of a Node in Linked List(at Beginning,End,Specified Position)with Code | DSA Tutorials








In this video we will see how to Insert a node in linked list – at beginning, at end and at any given position with example. we will also write a C program for insertion operation.

DSA Full Course: https:

******************************************
See Complete Playlists:

C Programming Course:

C++ Programming:

Python Full Course:

Printing Pattern in C:

DAA Course:

Placement Series:

Dynamic Programming:

Operating Systems: //www.youtube.com/playlist?list=PLdo5W4Nhv31a5ucW_S1K3-x6ztBRD-PNa

DBMS:
*************************************************

Connect & Contact Me:

Facebook:
Quora:
Instagram:

#datastructurenotes
#whatislinkedlist
#insertioninlinkedlist
#algorithms
#dsa

Link do Vídeo






32 Comentários

  1. You are one of the finest teachers .
    I am completing my data structure subject as I am in Computer Engineering 2nd year , by watching your all the videos by sequentially to not to miss any concepts. And I am getting all the concepts very clearly and undoubtedly
    I would just like to say Thank you very much for all these video lectures .

    Thank you very very much Jenny Ma'am 🙏✌

  2. Arrays:

    Memory Requirement: Arrays are contiguous blocks of memory where elements are stored in adjacent locations. This means that, in order to store an array, you need to allocate a fixed-size block of memory to hold all its elements. The memory requirement is fixed and is determined by the size of the array.

    Memory Utilization: While arrays have a fixed size, they may not efficiently utilize memory, especially if the array size is larger than the actual number of elements it holds. This can lead to wasted memory if the array is declared with a size that is much larger than the number of elements it eventually stores.

    Linked Lists:

    Memory Requirement: Linked lists are composed of nodes, where each node contains data and a reference (or pointer) to the next node in the sequence. Unlike arrays, the memory for linked lists is dynamically allocated as nodes are added, allowing for more flexibility in memory usage. Each node can be located at a different memory location, and the size of the linked list can change dynamically.

    Memory Utilization: Linked lists can be more memory-efficient in certain scenarios. Since memory is allocated dynamically for each node, there is no need to allocate a fixed-size block in advance. This means that memory is used more efficiently, especially when the size of the data structure changes frequently. However, the presence of pointers in each node introduces some overhead in terms of memory usage.

    In summary, the key differences in memory requirements and utilization between arrays and linked lists are related to the fixed-size nature of arrays versus the dynamic allocation of memory in linked lists. Arrays may require more memory upfront, and their fixed size can lead to inefficiencies, while linked lists can adapt to the actual size of the data, potentially resulting in more efficient memory utilization.

  3. Ma'am once you complete with final explanation of single topic can you Please shift a side for a while to take screenshot of white board notes……
    they too are very important……

  4. ma'am what do we do if we want to add string instead of integers because malloc will provide us nodes of fixed size and the length of string will vary.. so what do we do

  5. // linked list with Insertion at beginning, at end, at position, to display and to exit.

    #include<stdio.h>

    #include<stdlib.h>

    struct Node{

    int data;

    struct Node* next;

    };

    int main(){

    struct Node *newnode, *head, *temp;

    int data, choice;

    int count, position;

    head = 0;

    while(1){

    printf("1 for insert at end, 2 for insert at beginning, 3 to insert at position, 4 for display, 5 for exit: ");

    scanf("%d", &choice);

    switch(choice){

    case 1: // Insertion at end.

    newnode = (struct Node*) malloc(sizeof(struct Node));

    printf("Enter the data: ");

    scanf("%d", &newnode->data);

    newnode->next = 0;

    if(head==0){

    head = temp = newnode;

    }

    else{

    temp = head;

    while(temp->next!=0){

    temp=temp->next;

    }

    temp->next = newnode;

    }

    break;

    case 2: // Insertion at beginning.

    temp = head;

    newnode = (struct Node*) malloc(sizeof(struct Node));

    printf("Enter the data: ");

    scanf("%d", &newnode->data);

    newnode->next = head;

    head = newnode;

    break;

    case 3: // Insertion at position.

    count = 0;

    temp = head;

    while(temp!=0){

    temp = temp->next;

    count++;

    }

    printf("Enter the position between 0 and %d: ", count);

    scanf("%d", &position);

    if (position>count){

    printf("Invalid position, total position exits is: %dn", position);

    }

    else{

    temp = head;

    int i=1;

    while(i<position){

    temp = temp->next;

    i++;

    }

    newnode = (struct Node*) malloc(sizeof(struct Node));

    printf("Enter the data: ");

    scanf("%d", &newnode->data);

    newnode->next = temp->next;

    temp->next = newnode;

    }

    break;

    case 4: // Display the linked list.

    temp = head;

    while(temp!=0){

    printf("%d ",temp->data);

    temp = temp->next;

    }

    printf("n");

    break;

    case 5: // Exit.

    return 0;

    default:

    printf("Invalid input!n");

    }

    }

    }

    // 🙂

  6. Mam if we write these codes inside different functions, how the head value will be common to all these functions and how the change in head value inside a function will be reflected in other functions haed values too?

  7. 03:24 Insertion in Linked List
    06:48 Inserting a node at the beginning of a linked list
    10:12 Insertion of a Node in Linked List
    13:36 To insert a new node in a linked list, you need to traverse the list and find the last node
    17:00 Insertion of a node at the end of a linked list
    20:24 Insertion of a node at a specified position in a linked list
    23:48 Insertion of a Node in Linked List
    27:07 Insertion of a Node in Linked List

  8. here is my Code::::
    #include <stdio.h>

    #include <stdlib.h>

    struct Node

    {

    int data;

    struct Node *next;

    };

    struct Node *head,*newnode,*temp;

    void begenning()

    {

    newnode = (struct Node*) malloc (sizeof(struct Node));

    printf("nEnter Data To Insert At Begenning:");

    scanf("%d",&newnode->data);

    newnode->next = head;

    head = newnode;

    }

    void end()

    {

    newnode = (struct Node*) malloc (sizeof(struct Node));

    printf("nEnter Data To Insert At End:");

    scanf("%d",&newnode->data);

    newnode->next = NULL;

    temp = head;

    while(temp->next!=NULL)

    {

    temp = temp->next;

    }

    temp ->next = newnode;

    }

    void anywhere()

    {

    int count = 0, i = 1, pos;

    temp = head;

    while (temp != NULL)

    {

    temp = temp->next;

    count++;

    }

    newnode = (struct Node *)malloc(sizeof(struct Node));

    printf("nEnter Position:");

    scanf("%d", &pos);

    if (pos > count + 1 || pos <= 0)

    {

    printf("nInvalid Positionn");

    }

    else

    {

    temp = head;

    for (i = 1; i < pos – 1; i++)

    {

    temp = temp->next;

    }

    }

    printf("nEnter Data at Position:");

    scanf("%d", &newnode->data);

    newnode->next = temp->next;

    temp->next = newnode;

    }

    void traverse(struct Node *ptr)

    {

    temp = head;

    while(temp!=NULL)

    {

    printf("%d ->",temp->data);

    temp = temp->next;

    }

    }

    int main()

    {

    int ch = 1;

    head = NULL;

    while(ch)

    {

    newnode = (struct Node*) malloc (sizeof(struct Node));

    printf("Enter Data:");

    scanf("%d",&newnode->data);

    newnode->next = NULL;

    if(head==NULL)

    {

    head = temp = newnode;

    }

    else

    {

    temp->next = newnode;

    temp=newnode;

    }

    printf("Enter 1 to Continue 0 to Exit:");

    scanf("%d",&ch);

    }

    traverse(head);

    begenning();

    traverse(head);

    end();

    traverse(head);

    anywhere();

    traverse(head)

    }

  9. // Full implementation all right are reserved to "Just curious"
    struct node {

    int data;
    struct node *next;
    };
    struct node *head, *new_node , *temp;
    void At_beginning(){
    new_node = malloc(sizeof(struct node));
    printf("Please enter the data : ");
    scanf("%d",&new_node->data);
    new_node->next = head;
    head = new_node;
    }
    void At_end(){
    new_node = malloc(sizeof(struct node));
    printf("Please enter the data : ");
    scanf("%d",&new_node->data);
    new_node->next = NULL;
    temp = head;

    while(temp->next){
    temp = temp->next;
    }
    temp->next = new_node;
    }

    // problem here maybe
    void At_position(int position){
    new_node = malloc(sizeof(struct node));
    printf("Please enter the data : ");
    scanf("%d",&new_node->data);
    int i = 1;
    temp = head;
    while(i<position){
    ++i;
    temp = temp->next;
    }
    if (position == 0){
    new_node->next = head;
    head = new_node;
    }
    else {
    new_node->next = temp->next;
    temp->next = new_node;
    }

    }
    void display(){
    temp = head ;
    while(temp){
    printf("%dt",temp->data);
    temp = temp->next;
    }
    }
    // I invented this, I'm not sure if it works
    // to free the whole memory that was allocated for the linked list
    void freee(){
    temp = head;
    int counter = 0;
    while(temp){
    ++counter;
    temp = temp->next;
    }
    int size = counter ;
    struct node **array = malloc(sizeof(struct node*)*size);
    temp = head;
    int i = 0;
    while(temp){
    array[i++] = temp;
    temp= temp->next;
    }
    i = 0 ;
    free(array);
    }
    int main () {

    int size;
    printf("Enter the number of nodes : ");
    scanf("%d",&size);
    struct node **nodes = malloc(sizeof (struct node*)*size);
    for(int i = 0 ; i < size ; i++){
    nodes[i] = malloc(sizeof(struct node));
    }
    head = nodes[0];
    int i = 0;
    while(i<size){
    printf("Enter the data for the nodes : ") ;
    scanf("%d",&nodes[i]->data);
    if(i<size-1) {
    nodes[i]->next = nodes[i+1];
    }
    rewind(stdin);
    if(i==size-1){
    nodes[i]->next = NULL;
    }
    ++i;
    }
    int choice ;
    while(true) {
    printf("Where do u want to insert the next node press :n1 to insert at the beginning.n2 to insert at the end.n"
    "3 to insert at a specific position.n4 0 to exit.n") ;
    scanf("%d",&choice);
    int data;
    if(!choice) break;
    if(choice == 3){
    printf("Please enter the position in which the next node will be inserted: ");
    int position;
    scanf("%d",&position);
    At_position(position);
    }
    else if(choice == 1){
    At_beginning();
    }
    else {
    At_end();
    }
    }
    // display the nodes
    display();
    // freeing the memory
    freee();
    free(nodes);
    printf("All goodn");

    }

  10. while inserting the node at specified position, if we iterate the loop till less than pos when i = 1, i.e. : while(i<pos), the new node is inserted at the position 1 ahead of the desired/entered position. to fix this issue, we need to iterate the loop till less than pos -1 while initializing i to 1 : while(i<pos-1)
    this gives the desires result.
    However, I am unable to figure out why this happens. Can anybody explain?

Comentários estão fechados.