#include<stdio.h>
#include<stdlib.h>
typedef struct node
{
int no;
int age;
int score;
struct node *next;
}node;
node *creatINFO(node *&head)
{
node *p,*tail;
int temp=1;
tail=head;
printf("----创建信息表----\n");
while(temp != 2)
{
p = (node*)malloc(sizeof node);
printf ("输入 No:");
scanf ("%d",&(p->no));
printf ("输入 age:");
scanf ("%d",&(p->age) );
printf ("输入 score:");
scanf ("%d",&(p->score) );
p->next = NULL;
tail -> next = p;
tail = p;
printf ("继续建立新的学生信息(1.确定 2.退出):");
scanf("%d",&temp);
}
return head;
}
void showINFO(node* studentINFO)
{
int temp1;
node *tail,*p,*q;
printf("----打印----\n");
tail=studentINFO;
p=tail->next;
q=p->next;
printf("是否按照score的递增打印?(1.确定 2.取消)");
scanf("%d",&temp1);
if(temp1 == 2)
{
studentINFO=studentINFO->next;
while(studentINFO!=NULL)
{
printf("no:%d\t",studentINFO->no);
printf("age:%d\t",studentINFO->age);
printf("score:%d\n",studentINFO->score);
studentINFO=studentINFO->next;
}
}
if(temp1 == 1)
{
for(studentINFO=studentINFO->next;studentINFO->next!=NULL;studentINFO=studentINFO->next)
{
if(p->score >= q->score)
{
tail->next=q;
p->next=q->next;
q->next=p;
}
}
studentINFO=studentINFO->next;
while(studentINFO!=NULL)
{
printf("no:%d\t",studentINFO->no);
printf("age:%d\t",studentINFO->age);
printf("score:%d\n",studentINFO->score);
studentINFO=studentINFO->next;
}
}
}
void findINFO(node* studentINFO)
{
int find;
printf("----查找----\n");
printf("请输入要查找学生的NO:");
scanf("%d",&find);
studentINFO=studentINFO->next;
while(studentINFO!=NULL)
{
if(find==studentINFO->no)
{ printf("no:%d\t",studentINFO->no);
printf("age:%d\t",studentINFO->age);
printf("score:%d\n",studentINFO->score);
break;
}
else
studentINFO=studentINFO->next;
}
}
node *delINFO(node *studentINFO)
{
int no;
node *fre;
printf("----删除----\n");
printf("请输入要删除的学生的no:");
scanf("%d",&no);
studentINFO=studentINFO->next;
while(studentINFO->next != NULL);
{
if(no==studentINFO->next->no)
{
fre=studentINFO->next;
studentINFO->next=studentINFO->next->next;
free(fre);
}
else
studentINFO=studentINFO->next;
}
return studentINFO;
}
node *inst(node *studentINFO)
{
node* temp;
int ins;
printf("----插入----\n");
temp=(node*)malloc(sizeof node);
temp->next=NULL;
printf ("输入 No:");
scanf ("%d",&(temp->no));
printf ("输入 age:");
scanf ("%d",&(temp->age) );
printf ("输入 score:");
scanf ("%d",&(temp->score) );
printf("你想在插在no多少前:");
scanf("%d",&ins);
studentINFO=studentINFO->next;
while(studentINFO->next != NULL);
{
if(ins==studentINFO->no)
{
temp->next=studentINFO->next->next;
studentINFO->next=temp;
}
}
return studentINFO;
}
void main()
{
node *studentINFO;
int i;
studentINFO=(node*)malloc(sizeof node);
studentINFO->next=NULL;
while(1)
{
printf("**********************请选择需要的操作**********************\n");
printf("0 退出\n");
printf("1 创建新的学生信息表\n");
printf("2 打印学生信息表\n");
printf("3 查找并打印单个学生信息\n");
printf("4 删除单个学生信息\n");
printf("5 创建单个学生信息并插入到学生信息表\n");
scanf("%d",&i);
switch(i)
{
case 0:
return;
case 1:
creatINFO(studentINFO); break;
case 2:
showINFO(studentINFO); break;
case 3:
findINFO(studentINFO); break;
case 4:
delINFO(studentINFO); break;
case 5:
inst(studentINFO);break;
}
}
}
| |
[此贴子已经被作者于2006-5-17 20:50:56编辑过]
----------------解决方案--------------------------------------------------------
改是改不了啊!把你的改完,两都写出来了,你要要,我给你写一个,很快的
----------------解决方案--------------------------------------------------------
好可以 那麻烦你了
谢谢了
----------------解决方案--------------------------------------------------------
等几分钟
----------------解决方案--------------------------------------------------------
ok 没问题
----------------解决方案--------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
typedef struct node
{
char data;
struct node *nextPtr;
}*LinkList, Lnode;
static void Instruction(void);
static void CreateList(LinkList *headPtr, LinkList *tailPtr);
static void DeleteElem(LinkList *headPtr);
static void VisitList(LinkList headPtr);
static void DestroyList(LinkList *headPtr, LinkList *tailPtr);
int main(void)
{
LinkList headPtr = NULL, tailPtr = NULL;
int choice;
Instruction();
scanf("%d", &choice);
while (choice != 0)
{
switch(choice)
{
case 1:
CreateList(&headPtr, &tailPtr);
VisitList(headPtr);
break;
case 2:
DeleteElem(&headPtr);
VisitList(headPtr);
break;
default:
printf("Invalid enter.\n");
break;
}
Instruction();
scanf("%d", &choice);
}
DestroyList(&headPtr, &tailPtr);
getch();
return 0;
}
static void Instruction(void)
{
printf("Enter your choice(0-quit):\n");
printf("1-CreateList function.\n");
printf("2-DeleteElem function.\n");
}
static void CreateList(LinkList *headPtr, LinkList *tailPtr)
{
char ch;
LinkList newPtr;
printf("Enter create character: ");
fflush(stdin);
while (1)
{
scanf("%c", &ch);
if (ch == '\n')
{
return ;
}
if ((newPtr = (LinkList)malloc(sizeof(Lnode))) == NULL)
{
exit(1);
}
newPtr -> data = ch;
newPtr -> nextPtr = NULL;
if (*headPtr == NULL)
{
newPtr -> nextPtr = *headPtr;
*headPtr = newPtr;
}
else
{
(*tailPtr) -> nextPtr = newPtr;
}
*tailPtr = newPtr;
}
}
static void DeleteElem(LinkList *headPtr)
{
LinkList prePtr, curPtr, tPtr;
char ch;
printf("Enter delete character: ");
fflush(stdin);
scanf("%c", &ch);
if ((*headPtr) -> data == ch)
{
*headPtr = (*headPtr) -> nextPtr;
return ;
}
for (prePtr = NULL, curPtr = *headPtr; curPtr != NULL; prePtr = curPtr, curPtr = curPtr -> nextPtr)
{
if (curPtr -> data == ch)
{
tPtr = curPtr;
prePtr -> nextPtr = curPtr -> nextPtr;
free(tPtr);
return ;
}
}
if (curPtr == NULL)
{
printf("no found delete node.\n");
}
}
static void VisitList(LinkList headPtr)
{
while (headPtr != NULL)
{
printf("%c", headPtr -> data);
headPtr = headPtr -> nextPtr;
}
printf("\n");
}
static void DestroyList(LinkList *headPtr, LinkList *tailPtr)
{
LinkList tempPtr;
while (*headPtr != NULL)
{
tempPtr = *headPtr;
*headPtr = (*headPtr) -> nextPtr;
free(tempPtr);
}
*tailPtr = NULL;
}
----------------解决方案--------------------------------------------------------
诶,TC写出来的东西粘贴山去怎么会是这个样子
早知道用C-FREE写了
不好意思,将就看吧
----------------解决方案--------------------------------------------------------
千言万语的我此时只能用两个字表达
谢谢
----------------解决方案--------------------------------------------------------
----------------解决方案--------------------------------------------------------
强!!
----------------解决方案--------------------------------------------------------