【发布时间】: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