Linked List in C/C++ - Inserting a node at beginning

preview_player
Показать описание
See complete series on data structures here:
In this lesson we will write C program to insert a node at the beginning of a linked list.

Pre-requisites:
Good knowledge of pointers, scope of variables and dynamic memory allocation.
Check this lesson on dynamic memory allocation:
See complete series on pointers here:

Please drop your questions (if any) in comments. We will be glad to answer.
Рекомендации по теме
Комментарии
Автор

after 6 years of creating this videos ... no one created something better ! thanks alot teacher

youssefrezkou
Автор

For anybody getting the error "node not defined" -> after initial definition of the node and before the semicolon just put Node it looks something like this


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


If anybody wants the whole code to the one in this lesson:


//INSERTION OF NODES AT THE BEGINNING AND IT'S DISPLAY
#include<stdlib.h>
#include<stdio.h>
struct Node
{
int data;
struct Node* next;
}Node;
struct Node* head;
void insert(int x);
void print();
void main()
{
head=NULL;
printf("how many numbers?\n");
int n, i, x;
scanf("%d", &n);
for(i=0;i<n;i++)
{
printf("enter the number\n");
scanf("%d", &x);
insert(x);
print();
}
}

void insert(int x)
{
struct Node* temp=(struct Node*)malloc(sizeof(struct Node));
temp->data=x;
temp->next=head;
head=temp;
}
void print()
{
struct Node* temp=head;
printf("List is:");
while(temp!=NULL)
{
printf("%d ", temp->data);
temp=temp->next;
}
printf("\n");
}

AkshayKumar-dzts
Автор

Even if you are programming in C you can avoid to write "struct Node ..." every time by declaring the struct like this:

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

This way you will be able to declare Node variables the same way you do in C++.

Gaggio
Автор

Hi Siddharth,
Yes, we will continue creating more videos in data structures series. :)

mycodeschool
Автор

After 9:50, No thanx I'll keep my head global.

khedubaba
Автор

the way you changed the implementation, one using global variable, others using return and pointer to pointer, dope sir. now I know how proficient I need to be to just participate in competitive programming. thanks again.

rohitaswasarbhangia
Автор

The most AMAZING course explained brilliantly! So grateful!

thestarinthesky_
Автор

Complete code for: Insertion, Deletion and Print working:

#include <iostream>

using namespace std;

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

Node* Insert(Node* head, int x)
{
Node* newN=new Node();
newN->data=x;
newN->next=NULL;
if(head!=NULL) newN->next=head;
head=newN;
return head;
}
Node* InsertNth(Node* head, int x, int nt)
{
Node* temp1=new Node();
temp1->data=x;
temp1->next=NULL;
if(nt==1)
{
temp1->next=head;
head=temp1;
return head;
}
Node* temp2=head;
for(int i=0; i<nt-2; i++)
{
temp2=temp2->next;
}
temp1->next=temp2->next;
temp2->next=temp1;
return temp2;

}
Node *DeleteNth(Node* head, int nt)
{

Node* temp1=head;
if(nt==1){
head=temp1->next;
delete temp1;
return head;
}
for(int i=0; i<nt-2; i++)
{
temp1=temp1->next;
}
Node *temp2=temp1->next;
temp1->next=temp2->next;
delete temp2;
return temp1;

}


void Print(Node* head)
{
while(head!=NULL)
{
cout<<head->data<<" ";
head=head->next;
}
cout<<endl;
}
int main()
{
Node* head=NULL;
int n, i, x, y, nth;
char m;
cout<<"Please enter the size of the list"<<endl;
cin>>n;
for(int i=0; i<n; i++)
{
cout<<"Please enter the numbers: "<<endl;
cin>>x;
head=Insert(head, x);
Print(head);
}

cout<<"Do you want to insert a node in nth position? y/n?"<<endl;
cin>>m;

if(m=='y')
{
cout<<"Please enter the #:"<<endl;
cin>>y;
cout<<"Please insert the position where you want to insert new node"<<endl;
cin>>nth;
head=InsertNth(head, y, nth);
Print(head);
}
cout<<"Do you want to delete a # in nth position? y/n"<<endl;
cin>>m;
if(m=='y'){

cout<<"Please enter the position of te # to be deleted"<<endl;
cin>>nth;
head=DeleteNth(head, nth);
Print(head);
}

}

randomvinesvids
Автор

better than books, teachers, those udemy courses. You are the best. These tutorials will never get outdated.

MrAkshatmahajan
Автор

Yes, that's true. You should free any memory allocated using malloc once you are done using it. In this lesson, the intent was to show the implementation of insert function. But despite that, not having an explicit free is not doing any bad here. As soon as we are done printing all the elements, we are anyway done in the main function and the program execution will finish. And once program finishes, all of its memory is freed anyway.

mycodeschool
Автор

This was my most daunting concept. It took me 3 hours to understand this but at last I did it.

AJAYLIKEEMO
Автор

quality of content is amazing, I am just started learning from this playlist.

ramanjangu
Автор

35 dislikes are those people who use ARRAY all the time XD

mashable
Автор

Amazing, this is the first time that I realize the beauty of pointer!

tuongdihoc
Автор

Your insertion logic is correct. You may have a bug in main or print functions. Try to debug. Print your list every-time you insert a new element. So like after every call to insert_node, make a call to print. You should be able to nail down the exact issue.

mycodeschool
Автор

THANK YOU, JUST, THANK YOU!

Your work is amazing, your videos are great and the way you teach is just incredibly good.
I really appreciate what you have done and have been doing.

GabrielSousathe
Автор

Thank you so much.
I finally understood this after watching it so many times.

You guys are doing great work.👍🏽

AjaySharma-ledf
Автор

i think you're the best when it comes to c-related material, dude. thanks.

jamesbones
Автор

I was using a C++ compiler. You do not need to write struct keyword in C++. Moreover, this thing is testing whether someone running this code in C can debug simple compilation errors or not. :) Anyway, thanks for noticing.

mycodeschool
Автор

Exceptional video man. I was in a complete blank about how to do a linked list, but this video explained everything perfectly. Thank you.

licenseBort