【问题标题】:How to move the linked list from the last list to the front list in C?如何将链表从最后一个列表移动到C中的最前面?
【发布时间】:2014-01-03 00:36:17
【问题描述】:

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

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

谢谢你们帮助我!

【问题讨论】:

  • 这是C#?看起来像 C

标签: c linked-list


【解决方案1】:

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

#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 main() { newThesis(2); newThesis(5); newThesis(-3); newThesis(1); printList(); moveThesis(); printList(); }的目的是什么,能解释一下吗?
  • 在函数newThesis()中将节点推送到链表头部只是一个测试。所以在这个测试中,在所有四个推送之后,列表是{1,-3,5,2}。调用moveThesis() 函数后,最后一个将移动到列表的头部。因此,您将看到列表变为{2,1,-3,5}
【解决方案2】:

每次你创建一个结构空间来插入你的数据时,它被称为节点而不是列表。

尝试使用此代码将最后一个节点移动到第一个节点。

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多