DATA STRUCTURES INTERVIEW QUESTIONS
LINKED LIST INTERVIEW QUESTIONS
Here Is One Of Most Asked Interview Question On Linked List !
We Have Given A List With Following Data :-
1->2->3->4->5->6->7->8->9->10 and k=3
1->2->3->4->5->6->7->8->9->10 and k=3
Then Output Should Be :-
4->3->2->1->5->6->7->8->9->10
------------------------------------------------------------------------------------------------------------------
Here Is A Solution To This Problem:-
------------------------------------------------------------------------------------------------------------------
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
typedef struct ll
{
int data;
struct ll *next;
}mynode;
mynode *start;
void placestart(mynode **hd,int c);
int numofnodes(mynode *hd);
void addnode(mynode **hd,int data);
void revinstyle(mynode **hd,int nonr);
void printall(mynode *hd);
void main()
{
int ch,data,nonr,count;
mynode *head=NULL;
clrscr();
do
{
printf("\n1.Add Data");
printf("\n2.Enough! Continue To Reversing ");
printf("\n3.Traverse");
printf("\n4.Exit");
printf("\nEnter Choice ");
scanf("%d",&ch);
switch(ch)
{
case 1:
clrscr();
printf("\n Enter Data ");
scanf("%d",&data);
addnode(&head,data);
break;
case 3:
printall(head);
break;
case 4:
exit(0);
break;
}//switch
} while(ch!=2);
/////////////////////////////////////////////////////////////////////////////
clrscr();
printf("\nEnter Number Of Nodes Of To Be Reversed ");
scanf("%d",&nonr);
count=numofnodes(head);
printf("NUM OF NODES = %d And Number Of Nodes To Be Reversed=%d",count,nonr);
if(count<=nonr||count==1||count==0)
{
printf("\nOperation Can't Be Applied , Insufficient Data");
getch();
exit(0);
}
printf("\n\n");
printf("\nBefore Operation \n");
printall(head);
placestart(&head,nonr);
//printf("\nDATA FROM START=%d\n",start->data);
printf("\n\n");
revinstyle(&head,nonr);
printf("\nDATA FROM REVERSED LIST\n");
printall(head);
printf("\n\n");
printf("\nDATA FROM REMAINING OF LIST \n");
printall(start);
------------------------------------------------------------------------------------------------------------------
Here Is A Solution To This Problem:-
------------------------------------------------------------------------------------------------------------------
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
typedef struct ll
{
int data;
struct ll *next;
}mynode;
mynode *start;
void placestart(mynode **hd,int c);
int numofnodes(mynode *hd);
void addnode(mynode **hd,int data);
void revinstyle(mynode **hd,int nonr);
void printall(mynode *hd);
void main()
{
int ch,data,nonr,count;
mynode *head=NULL;
clrscr();
do
{
printf("\n1.Add Data");
printf("\n2.Enough! Continue To Reversing ");
printf("\n3.Traverse");
printf("\n4.Exit");
printf("\nEnter Choice ");
scanf("%d",&ch);
switch(ch)
{
case 1:
clrscr();
printf("\n Enter Data ");
scanf("%d",&data);
addnode(&head,data);
break;
case 3:
printall(head);
break;
case 4:
exit(0);
break;
}//switch
} while(ch!=2);
/////////////////////////////////////////////////////////////////////////////
clrscr();
printf("\nEnter Number Of Nodes Of To Be Reversed ");
scanf("%d",&nonr);
count=numofnodes(head);
printf("NUM OF NODES = %d And Number Of Nodes To Be Reversed=%d",count,nonr);
if(count<=nonr||count==1||count==0)
{
printf("\nOperation Can't Be Applied , Insufficient Data");
getch();
exit(0);
}
printf("\n\n");
printf("\nBefore Operation \n");
printall(head);
placestart(&head,nonr);
//printf("\nDATA FROM START=%d\n",start->data);
printf("\n\n");
revinstyle(&head,nonr);
printf("\nDATA FROM REVERSED LIST\n");
printall(head);
printf("\n\n");
printf("\nDATA FROM REMAINING OF LIST \n");
printall(start);
getch();
}
void placestart(mynode **hd,int c)
{
start=*hd;
while(c)
{
start=start->next;
c--;
}
start=start->next;
}
void revinstyle(mynode **hd,int nonr)
{
mynode *p,*q,*r;
if(*hd==NULL)
{
printf("\nNothing In List");
return;
}
p=*hd;
if((*hd)->next==NULL)
{
printf("Single Data Only");
return;
}
q=(*hd)->next;
//printf("nonor=%d",nonr);
if(q->next==NULL&&nonr==1)
{
//printf("\nhead=%d",(*hd)->next);
q->next=*hd;
(*hd)->next=NULL;
//printf("\nafter head=%d",(*hd)->next);
*hd=q;
return;
}//
r=q->next;
while(nonr)
{
q->next=p;
p=q;
q=r;
r=r->next;
nonr--;
}//while
(*hd)->next=NULL;
*hd=p;
}
int numofnodes(mynode *hd)
{
int c=0;
if(hd==NULL)
return 0;
while(hd!=NULL)
{
c++;
hd=hd->next;
}
return c;
}
void printall(mynode *hd)
{
printf("\n\n");
if(hd==NULL)
{
printf("\nNOTHING !");
return;
}
while(hd!=NULL)
{
printf("%d,",hd->data);
hd=hd->next;
}
}
void addnode(mynode **hd,int data)
{
mynode *p,*q;
p=(mynode*)malloc(sizeof(mynode));
p->data=data;
p->next=NULL;
if(*hd==NULL)
{
(*hd)=p;
return;
}
q=*hd;
while(q->next!=NULL)
q=q->next;
q->next=p;
}
}
void placestart(mynode **hd,int c)
{
start=*hd;
while(c)
{
start=start->next;
c--;
}
start=start->next;
}
void revinstyle(mynode **hd,int nonr)
{
mynode *p,*q,*r;
if(*hd==NULL)
{
printf("\nNothing In List");
return;
}
p=*hd;
if((*hd)->next==NULL)
{
printf("Single Data Only");
return;
}
q=(*hd)->next;
//printf("nonor=%d",nonr);
if(q->next==NULL&&nonr==1)
{
//printf("\nhead=%d",(*hd)->next);
q->next=*hd;
(*hd)->next=NULL;
//printf("\nafter head=%d",(*hd)->next);
*hd=q;
return;
}//
r=q->next;
while(nonr)
{
q->next=p;
p=q;
q=r;
r=r->next;
nonr--;
}//while
(*hd)->next=NULL;
*hd=p;
}
int numofnodes(mynode *hd)
{
int c=0;
if(hd==NULL)
return 0;
while(hd!=NULL)
{
c++;
hd=hd->next;
}
return c;
}
void printall(mynode *hd)
{
printf("\n\n");
if(hd==NULL)
{
printf("\nNOTHING !");
return;
}
while(hd!=NULL)
{
printf("%d,",hd->data);
hd=hd->next;
}
}
void addnode(mynode **hd,int data)
{
mynode *p,*q;
p=(mynode*)malloc(sizeof(mynode));
p->data=data;
p->next=NULL;
if(*hd==NULL)
{
(*hd)=p;
return;
}
q=*hd;
while(q->next!=NULL)
q=q->next;
q->next=p;
}
----------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------
!!! HOPE U LIKE IT !!!
!!! THANKS FOR READING !!!
!!!!!HAPPY PROGRAMMING !!!!!
U Can catch me at http://www.facebook.com/salokr!!!!!HAPPY PROGRAMMING !!!!!
----------------------------------------------------------------------------------------------------------------
Comments
Post a Comment