我在接受采访时被问到这个问题:
您将获得指向标题的指针,但您不被允许 遍历整个列表。你将如何到达最后一个节点?
我试过自己,却未能得出任何答案。有人可以帮忙吗?
答案 0 :(得分:2)
术语“head er ”意味着不仅仅是指向头节点的指针。鉴于典型的链表头包含指针*head
,*current
和*tail
,我建议这个问题是对惯用语而不是算法知识的测试。
答案 1 :(得分:0)
请参阅,我没有找到一种方法来遍历链接列表中的最后一个节点。
也许有办法做到这一点,但这不是直接的。您可以随时使用循环链表。该循环列表必须是双向循环链表
这是双向循环链表实现的代码
#include<stdio.h>
#include<conio.h>
//Structure for Two Way Circular Linked List
struct node
{
int data;
struct node *prev;
struct node *next;
}*start=NULL;
//Prototype Declaration
void createList(int);
void display(void);
void addbeg(int);
void addspec(int,int);
void search(int);
void evenodd(void);
//Main Function Start From Here
void main()
{
int ch,num,pos;
clrscr();
do
{
printf("\n1.Create Linked List");
printf("\n2.Display Linked List");
printf("\n3.Add At Beginning");
printf("\n4.Add At Specific Location");
printf("\n5.Search Element in List");
printf("\n6.Even Odd Calculations");
printf("\n7.Exit");
printf("\nEnter Your Choice");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("Enter any Number");
scanf("%d",&num);
createList(num);
break;
case 2:
display();
break;
case 3:
printf("Enter any Number to add at Beginning");
scanf("%d",&num);
addbeg(num);
break;
case 4:
printf("Enter any Number and its Position");
scanf("%d%d",&num,&pos);
addspec(num,pos);
break;
case 5:
printf("Enter any Number to Search");
scanf("%d",&num);
search(num);
break;
case 6:
evenodd();
break;
case 7:
exit(0);
default:
printf("Invalid Choice");
}
}while(1);
}
//Create Two Way Circular Linked List
void createList(int num)
{
struct node *q,*temp;
if(start==NULL)
{
start=(struct node*)malloc(sizeof(struct node));
start->data=num;
start->prev=start;
start->next=start;
}
else
{
q=start;
//Traverse List Till End
while(q->next!=start)
q=q->next;
//Add New Node At the End
temp=(struct node*)malloc(sizeof(struct node));
temp->data=num;
temp->next=start;
start->prev=temp;
q->next=temp;
q->next->prev=q;
}
}
//Display Elements of List
void display(void)
{
struct node *q=start;
do
{
printf("%d\t",q->data);
q=q->next;
}while(q!=start);
}
//Add At Beginning in Two Way Circular Linked List
void addbeg(int num)
{
struct node *q=start,*temp;
//Traverse List Till End
while(q->next!=start)
q=q->next;
//Add New Node At Beginning
temp=(struct node*)malloc(sizeof(struct node));
temp->data=num;
temp->next=start;
q->next=temp;
start->prev=temp;
temp->prev=q;
start=temp;
}
//Add At Specific Position in Two Way Circular Linked List
void addspec(int num,int pos)
{
struct node *q=start,*temp;
int i;
for(i=1;i<pos-1;i++)
q=q->next;
//Add New Node At Specific Position
temp=(struct node*)malloc(sizeof(struct node));
temp->data=num;
temp->next=q->next;
q->next->prev=temp;
q->next=temp;
q->next->prev=q;
}
//Search Element into List
void search(int num)
{
struct node *q=start;
int count,i=0,flag=0;
do
{
if(q->data==num)
{
flag=1;
count=i;
break;
}
i++;
q=q->next;
}while(q!=start);
if(flag==1)
printf("Element Found At Location %d",count+1);
else
printf("No Element Found");
}
//Even Odd Calculation
void evenodd(void)
{
struct node *q=start;
int esum=0,osum=0,tsum=0,ec=0,oc=0,tc=0;
do
{
if(q->data%2==0)
{
esum+=q->data;
ec++;
}
else
{
osum+=q->data;
oc++;
}
tsum+=q->data;
tc++;
q=q->next;
}while(q!=start);
printf("\nEven Sum=%d",esum);
printf("\nOdd Sum=%d",osum);
printf("\nTotal Sum=%d",tsum);
printf("\nEven Count=%d",ec);
printf("\nOdd Count=%d",oc);
printf("\nTotal Count=%d",tc);
}
现在,我向您提供了概念或方法(从头到尾移动)。希望这个答案足以让你和我个人觉得面试官可能会测试你的概念。
谢谢
答案 2 :(得分:0)
你也可以跟踪一个指针,说'lastNode&#39;在列表中的每个插入操作都会更新。