在C语言中,如何将链表从最后一个列表移动到最前面的列表?

在C语言中,如何将链表从最后一个列表移动到最前面的列表?,c,linked-list,C,Linked List,基本上,这是我为项目编写的所有代码: #include <stdio.h> #include <stdlib.h> #define TRUE 1 #define FALSE 0 void newThesis(); void listThesis(); void moveThesis(); struct thesis { int thesis; char stud_name[30]; char stud_id[10]; char the

基本上,这是我为项目编写的所有代码:

#include <stdio.h>
#include <stdlib.h>

#define TRUE 1
#define FALSE 0

void newThesis();
void listThesis();
void moveThesis();

struct thesis
{
    int thesis;
    char stud_name[30];
    char stud_id[10];
    char thesis_title[40];
    int thesis_year;
    struct thesis *ptrnext;
};

struct thesis *headptr, *newptr, *currentptr, *previousptr;

int main()
{
    char ch;
    int choice=TRUE;
    headptr=(struct thesis *)NULL;
    while(choice==TRUE)
    {
        printf("\n\nE - Enter thesis information");
        printf("\nL - List all thesis");
        printf("\nM - Move last node to first node");
        printf("\nX - Exit\n");
        printf("\nEnter choice: ");
        scanf(" %c",&ch);
        switch(ch)
    {
        case 'E':newThesis();break;
        case 'L':listThesis();break;
        case 'M':moveThesis();break;
        case 'X':choice=FALSE; break;
        default: printf("\nEnter only one from the above");
    }
}
return 0;
}

void newThesis()
{
    newptr=(struct thesis *)malloc(sizeof (struct thesis));
    if (headptr== NULL)
    {
        headptr=newptr;
        newptr->ptrnext= NULL;
    }
    else
    {
        newptr->ptrnext=headptr;
        headptr=newptr;
    }
    printf("\nTHESIS CODE:");
    printf("\n1 - Online Information Management System");
    printf("\n2 - Cursor Movement Using Finger Gesture");
    printf("\n3 - Tomato Maturity Estimator ");

    printf("\n\nEnter thesis code: ");
    scanf("%d",&newptr->thesis);
    printf("\nEnter student name: ");
    scanf("%s",&newptr->stud_name);
    printf("\nEnter student id: ");
    scanf("%s",&newptr->stud_id);
    printf("\nEnter thesis title: ");
    scanf("%s",&newptr->thesis_title);
    fflush(stdin);
    printf("\nEnter thesis year: ");
    scanf("%d",&newptr->thesis_year);
    fflush(stdin);

    listThesis();
}

void listThesis()
{
    if (headptr==NULL)
    {
        printf("\nEmpty list");
        return;
    }
    currentptr=headptr;
    do
    {
        printf("\n\n%d",currentptr->thesis);
        printf("\n%s",currentptr->stud_name);
        printf("\n%s",currentptr->stud_id);
        printf("\n%s",currentptr->thesis_title);
        printf("\n%d",currentptr->thesis_year);
        printf("\n");
        currentptr=currentptr->ptrnext;
    }
    while(currentptr != NULL);
}
*编辑:我已经得到了答案,而且效果非常好

void moveThesis()
{
    currentptr = headptr;
    do
    {
    previousptr=currentptr;
    currentptr=currentptr->ptrnext;
    }while(currentptr->ptrnext !=NULL);

    currentptr->ptrnext=headptr;
    headptr=currentptr;
    previousptr->ptrnext=NULL;

    listThesis();
}

谢谢你们帮我,伙计们

试试这个。这是一个简化版本,但是代码应该是一样的

#include <stdio.h>
#include <stdlib.h>

struct thesis
{
    int data;
    struct thesis *ptrnext;
};

struct thesis *headptr=NULL, *newptr, *currentptr, *previousptr;

void printList() {
    currentptr = headptr;
    while(currentptr!=NULL) {
        printf("%d, ", currentptr->data);
        currentptr = currentptr->ptrnext;
    }
    printf("\n");
}

void newThesis(int number)
{
    newptr=(struct thesis *)malloc(sizeof (struct thesis));
    newptr->data = number;
    if (headptr== NULL)
    {
        headptr=newptr;
        newptr->ptrnext= NULL;
    }
    else
    {
        newptr->ptrnext=headptr;
        headptr=newptr;
    }
}

void moveThesis() {
    currentptr = headptr;
    if(currentptr == NULL || currentptr->ptrnext==NULL)
        return;
    while(currentptr->ptrnext != NULL) {
        previousptr = currentptr;
        currentptr = currentptr->ptrnext;
    }

    struct thesis *lastNode = currentptr;
    previousptr->ptrnext = NULL;
    lastNode->ptrnext = headptr;
    headptr = lastNode;
}

int main()
{
    newThesis(2);
    newThesis(5);
    newThesis(-3);
    newThesis(1);
    printList();
    moveThesis();
    printList();
}
#包括
#包括
结构论文
{
int数据;
结构论文*ptrnext;
};
结构主题*headptr=NULL,*newptr,*currentptr,*previousptr;
作废打印列表(){
currentptr=headptr;
while(currentptr!=NULL){
printf(“%d”,当前ptr->data);
currentptr=currentptr->ptrnext;
}
printf(“\n”);
}
void newThesis(整数)
{
newptr=(结构论文*)malloc(sizeof(结构论文));
新建PTR->数据=编号;
如果(headptr==NULL)
{
headptr=新PTR;
newptr->ptrnext=NULL;
}
其他的
{
newptr->ptrnext=headptr;
headptr=新PTR;
}
}
无效论文(){
currentptr=headptr;
如果(currentptr==NULL | | currentptr->ptrnext==NULL)
返回;
而(currentptr->ptrnext!=NULL){
先前的PTR=当前的PTR;
currentptr=currentptr->ptrnext;
}
结构文件*lastNode=currentptr;
上一个ptr->ptrnext=NULL;
lastNode->ptrnext=headptr;
headptr=lastNode;
}
int main()
{
纽特西斯(2);
纽特西斯(5);
纽特西斯(-3);
纽特西斯(1);
打印列表();
移动论文();
打印列表();
}

每次您创建一个结构空间来插入您的数据时,它被称为节点非列表

请尝试将最后一个节点移动到第一个节点的代码

      void moveThesis()
      {
        currentptr = headptr;
        if(currentptr == NULL || currentptr->ptr == NULL)
              return;
        while(currentptr->ptrnext->ptrnext != NULL)
              currentptr = currentptr->ptrnext;
        currentptr->ptrnext->ptrnext = headptr;
        headptr = curerntptr->ptrnext;
        curerntptr->ptrnext = NULL;
      }

并尝试返回一个变量以获取操作完成的状态

这是C#?看起来CI刚刚测试了代码,它工作得很好。有一个问题,
int main(){newThesis(2);newThesis(5);newThesis(-3);newThesis(1);printList();moveThesis();printList();}
你能解释一下吗?这只是在函数
newThesis()
中将节点推到链表头部的一个测试。所以在这个测试中,在所有四次推送之后,列表是
{1,-3,5,2}
。调用
moveThesis()
函数后,最后一个将移动到列表的开头。因此,您将看到列表变成
{2,1,-3,5}
      void moveThesis()
      {
        currentptr = headptr;
        if(currentptr == NULL || currentptr->ptr == NULL)
              return;
        while(currentptr->ptrnext->ptrnext != NULL)
              currentptr = currentptr->ptrnext;
        currentptr->ptrnext->ptrnext = headptr;
        headptr = curerntptr->ptrnext;
        curerntptr->ptrnext = NULL;
      }