c - 如何将链表从最后一个列表移动到 C 中的前面列表?

标签 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 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();

*已编辑:我找到了答案,而且效果很好!!

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();
}

关于c - 如何将链表从最后一个列表移动到 C 中的前面列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20894531/

相关文章:

c - 段错误 - 链表

c - Makefile 循环依赖

粘贴在浏览器地址栏中有效的相同网址时,curl 不起作用

c - 将 N^(n*n) 矩阵存储在 C 中

c - C 中带有三个点的#define

c - 链接列表不适用于 strtok

c - C中删除链表

java - 包含对象的节点的堆栈实现

c - 为什么popen不和mpg123通信?

c - 为单链表编写适当的push() 函数。