在不遍历整个列表的情况下,到达链表的最后一个节点



我在一次采访中被问到这个问题:

您得到了指向标头的指针,但不允许遍历整个列表。你将如何到达最后一个节点?

我试了试,但没有找到任何答案。有人能帮忙吗?

术语"header"不仅仅意味着指向head节点的指针。假设典型的链表头包含指针*head*current*tail,我认为这个问题是对惯用知识的测试,而不是对算法知识的测试。

看,我没有找到一种方法可以快速遍历到链表中的最后一个节点
也许有办法做到这一点,但那不是直接的。您可以使用一个循环链表。循环列表必须是双向循环链表
以下是双向循环链表实现的代码

#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("%dt",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);
}

现在,我向您介绍了这个概念或实现方法(从头节点移动到最后一个节点)。希望这个答案能满足你和我个人的感受,面试官可能在测试你的概念
谢谢

您还可以跟踪一个指针,比如"lastNode",该指针在列表中的每次插入操作时都会更新。

相关内容

  • 没有找到相关文章

最新更新