Node swap in Single Linked List | Data Structures Tutorial

preview_player
Показать описание
Node swap in Single Linked List
Data Structures Tutorial

►Call: India- 8179191999, USA- 404-232-9879

► Visit Our Website:

► About NareshIT:

"Naresh IT is the Best Software Training Institute for Hadoop, Salesforce, AWS, DevOps, Sprak, Data Science, Python, Tableau, RPA ,Java, C#.NET, ASP.NET, Oracle, Testing Tools, Silver light, Linq, SQL Server, Selenium, Android, iPhone, C Language, C++, PHP and Digital Marketing in Hyderabad, Chennai and Vijayawada, India which provides online and classroom training classes"

►Call:
India- 8179191999, USA- 404-232-9879

►Our Online Training Features:
1.Training with Real-Time Experts
2.Industry Specific Scenario’s
3.Flexible Timings
4.Soft Copy of Material
5.Share Video's of each and every session.

► Visit Our Website:
Рекомендации по теме
Комментарии
Автор

ameerpet lo unna anni software training institutes ki i.e.., to all the pre job tech training centres ki especially naresh it ki oka i.e.., ek big salute

saipraneethtalluri
Автор

sir please upload "how to revers the single linked list and also double linked list revers the links" this two programs videos

kirankumar
Автор

What about swapping nth and mth node? user could want to delete first and last node.

AnkitSaiyan
Автор

u teach so well.. so damn well.. hats off

shigo
Автор

whole program






/* single list operationd*/
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node* link;
};
struct node* root = NULL;//default value v null hota h no need to do
int len ;
void append(void);
void addatbegin(void);
void addafter(void);
int lenghth(void);
void display(void);
void delete(void);
void swap(void);
void main()
{
int ch;
while(1)
{
printf("single linked list operations :\n");
printf("1.Append\n");
printf("2.Addatbegin\n");
printf("3.Addafter\n");
printf("4.Length\n");
printf("5.Display\n");
printf("6.Delete\n");
printf("7.swap\n");
printf("8.Quit\n");

printf("Enter your choice :");
scanf("%d", &ch);

switch(ch)
{
case 1 : append();
break;

case 2 : addatbegin();
break;

case 3 : addafter();
break;

case 4 : len = length();
printf("Length : %d\n\n", len);
break;

case 5 : display();
break;

case 6 : delete();
break;

case 7 : swap();
break;

case 8 : exit(1);
default: printf("Invalid input\n\n");
}

}
}
void append()
{
struct node* temp;
temp = (struct node*)malloc(sizeof(struct node));//node creation
printf("Enter node data:");
scanf("%d", &temp->data);
temp->link = NULL;

if(root == NULL)//list is empty
{
root = temp;
}
else
{
struct node* p;//local variable
p = root;

while(p->link != NULL)
{
p = p->link;
}
p->link = temp;
}
}
int length()
{
int count = 0;
struct node* temp;
temp = root ;

while(temp != NULL)
{
count++;
temp = temp->link;
}
return count ;//agar null hua toa i mean node empty hua toa directly count value return krega
}

void display()
{
struct node* temp;
temp = root;
if(temp == NULL)
{
printf("List is empty \n\n");
}
else
{
while(temp !=NULL)
{
printf("%d-->", temp->data);
temp = temp->link;
}
printf("\n\n");
}
}
void addatbegin(void)
{
struct node* temp;
temp = (struct node*)malloc(sizeof(struct node));//node creation
printf("Enter node data:");
scanf("%d", &temp->data);
temp->link=NULL;
if(root == NULL)
{
root = temp;
}
else
{
temp->link=root;
root = temp;
}
}
void addafter(void)
{
struct node* temp, *p;
int loc, len, i=1;
printf("enter location:");
scanf("%d", &loc);
len=length();
if(loc>len)
{
printf("invalid location\n\n");
printf("currently list is having %d nodes\n\n", len);
}
else
{
p=root;
while(i<loc)
{
p=p->link;
i++;
}
}
temp=(struct node*)malloc(sizeof(struct node));
printf("enter the node data:");
scanf("%d", &temp->data);
temp->link=NULL;
if(root == NULL)
{
root = temp;
}
else
{
temp->link=p->link;
p->link=temp;
}

}
void delete(void)
{
struct node* temp;
int loc;
printf("enter location to delete:");
scanf("%d", &loc);
if(loc>length())
{
printf("INVALID LOCATION\n\n");
}
else if(loc==1)
{
temp=root;
root=temp->link;
temp->link=NULL;
free(temp);//memory release
}
//if loc is any middle or at the end of single linked list
else
{
struct node* p=root, *q;
int i=1;
while(i<loc-1)
{
p=p->link;
i++;
}
q=p->link;
p->link=q->link;
q->link=NULL;
free(q);

}
}
void swap(void)
{
struct node* p, *q, *r;
int loc, i=1;

printf("ENTER THE LOCATION TO BE SWAPPED: ");
scanf("%d", &loc);
if(loc>length())
{
printf("invalid input\n\n");
}
else
{
p=root;
while(i<loc-1)
{
p=p->link;
i++;
}
q=p->link;
r=q->link;
}
q->link=r->link;
r->link=q;
p->link=r;

}

nilanknikhil
Автор

Sir can we swap any two nodes in a single linked list? Or adjacent node swapping only possible?

sureshgoondla
Автор

Sir swaping should be perform on the data. Why swaping address?

vinodkashyap
Автор

Did you perform node swapping on your playlist.... Videos are all over the place

anujchouksey
Автор

Simple logic for node swap in single linked list

void swap(int pos1, int pos2)
{
int i, j, temp;
struct node *p, *q;

p = q = root;

for(i = 1; i < pos1; i++)
{
p = p->link;
}
for(j = 1; j < pos2; j++)
{
q = q->link;
}

temp = p->data;
p->data = q->data;
q->data = temp;
}

arungowda
Автор

Here's the code

void swap_adjascent_node()
{
struct node *p, *q, *r;
int i=1, loc;
p=root;
if(p==NULL)
{
printf("List is empty\n");
}
else
{
printf("Enter location to swap node:");
scanf("%d", &loc);

if(loc>length()-1)
{
printf("Invalid location\n");
}
else
{

while(i<loc-1)
{
p=p->link;
i++;
}
q=p->link;
r=q->link;

q->link=r->link;
r->link=q;
p->link=r;
}
}
}

truptisonawane
Автор

HERE IS THE COMPLETE CODE:
#include<stdio.h>
struct node {
int data;
struct node* link;
};
struct node* top=NULL;

void push(int);
void pop();
void traverse();
void swap(int);

void main()
{
int ch;
while(1)
{
printf("Choices are:\n");
printf("1.Push\n");
printf("2.Pop\n");
printf("3.Traverse\n");
printf("4.Swap\n");
printf("5.exit\n");
printf("Enter your choice: ");
scanf("%d", &ch);
printf("Your choice was: %d\n", ch);
switch(ch)
{
int ele, loc;
case 1: printf("enter element to be pushed: ");
scanf("%d", &ele);
push(ele);
break;
case 2: pop();
break;
case 3: traverse();
break;
case 4:
printf("Enter location to be swapped with next one: ");
scanf("%d", &loc);
swap(loc);
break;
case 5: exit(0);
default: printf("Invalid input\n");
}
printf("\n");
}
printf("ok\n\n");
}

void push(int ele)
{
struct node*temp;
temp=(struct node*)malloc(sizeof(struct node));
temp->data=ele;
temp->link=top;
top=temp;
}

void traverse()
{
struct node* temp;
if(top==NULL)
printf("Stack is empty: \n");
else
{
temp=top;
printf("Stack elements are: ");
while(temp!=NULL)
{
printf("%d ", temp->data);
temp=temp->link;
}
}
}

void pop()
{
struct node* temp;
if(top==NULL)
printf("Stack is empty: Underflow\n");
else
{
temp=top;
printf("Deleted element is: %d", temp->data);
top=top->link;
temp->link=NULL;
free(temp);
}
}

void swap(int loc)
{

struct node* p=top;
struct node* q=top;
struct node* r=top;
int i=1;
while(i<loc-1)
{
p=p->link;
i++;
}
q=p->link;
r=q->link;
q->link=r->link;
r->link=q;
p->link=r;
}

goyaldeekshant
Автор

Sir we swap only 3rd and 4th nodes only then why we changed the value in 2nd node

vinaymentor
Автор

thank you
What data structure should be used for finding all the restaurants in a location given a location and finding all the locations given a restaurant name?

SpiritOfIndiaaa
Автор

can anyone tell me how to run the append function in a loop please. i am getting 1 element less if im running append in a for loop!

MusicMachineForever
Автор

but the 1st node is not being swapped instead the 2nd and 3rd nodes are being swapped

karanvyas
Автор

why dont we just interchange the data instead of a whole node in swaping they both make the same sense

eeeitguy
Автор

Sir please post the circular link list

pooja
Автор

sir please provide a separate tutorial for every concept with program implementation

rajatagrawal
Автор

//Please check and help

//node swaping in double linked list


void nodeswap()
{
int loc;
printf("\nEnter location to swap with next:");
scanf("%d", &loc);
len=length();

if(loc==len)
{
printf("\nThere is no element available next to %d location for swapping\n", loc);
}
else if(loc>len)
{
printf("\nLocation is invalid\n");

printf("\nList contains :%d elements\n", len);
}
else
{ //For loc>1 and loc<len
struct node* temp;
struct node* p, *q, *r;
temp=root;
p=root;
int i=1;
while(i<loc-1)
{
p=p->right;
i++;
}
q=p->right;
r=q->right;


p->right=r;
r->left=p;
q->right=p;
p->left=q;
temp->right=q;
q->left=temp;

}

}

satyarth.pundir
Автор

Sir, please give notes on circular linked list

tajuddinmohmmad
welcome to shbcf.ru