|
10鱼币
#include "stdafx.h"
#include <stdlib.h>
struct film
{
char name[40];
int mark;
};
typedef struct film Item;
typedef struct node
{
Item item;
struct node *next;
}Node;
typedef Node *List;
void Initialinelist(Node* plist);//初始化空链表指针
bool ListisEmpty(Node *plist);//确定指针是否为空
bool ListisFull(Node *plist);//确定指针是否满
unsigned int ListItemCount(Node *plist);//确定列表的个数
bool AddItem(Item item,Node *plist);//在尾部增加项目
void Traverse(Node *plist,void(*pfun)(Item item));//吧一个函数用于每个列表
void FreeList(Node *plist);//释放内存
void CopyToNode(Item item,Node *plist);
void showmovies(Item item);
int _tmain(int argc, _TCHAR* argv[])
{
Node movies;
Item temp;
Initialinelist(&movies);
if(ListisFull(&movies))
{
fprintf(stderr,"No memory create it!\n");
exit(0);
}
puts("Enter you like film name\n");
while(gets(temp.name)!=NULL&&temp.name[0]!='\0')
{
puts("film rating\n");
scanf("%d",&temp.mark);
while(getchar()!='\n')
continue;
if(AddItem(temp,&movies)==false)
{
fprintf(stderr,"No memory create it again!\n");
break;
}
if(ListisFull(&movies))
{
puts("The List is Full\n");
break;
}
puts("Enter the next film \n");
}
if(ListisEmpty(&movies))
{
fprintf(stderr,"No film create it!\n");
}
else
{
fprintf(stderr,"The List create it!\n");
Traverse(&movies,showmovies);
}
printf("you entered %d movies.\n",ListItemCount(&movies));
printf("Bye\n");
return 0;
}
void Initialinelist(Node* plist)
{
plist=NULL;
}
bool ListisEmpty(Node *plist)
{
if(plist==NULL)
{
return true;
}
else
return false;
}
bool ListisFull(Node *plist)
{
Node *pt;
bool full;
pt=(Node *)malloc(sizeof(Node));
if(pt==NULL)
{
full=true;
}
else
full=false;
free(pt);
return full;
}
unsigned int ListItemCount(Node *plist)
{
int count=0;
while(plist!=NULL)
{
++count;
plist=plist->next;
}
return count;
}
bool AddItem(Item item,Node *plist)
{
Node *pnew;
Node *scan=plist;
pnew=(Node *)malloc(sizeof(Node));
if(pnew==NULL)
{
return false;
}
CopyToNode(item,pnew);
pnew->next=NULL;
if(scan==NULL)
{
plist=pnew;
}
else
{
while(scan->next!=NULL)
scan=scan->next;
scan->next=pnew;
}
return true;
}
void CopyToNode(Item item,Node *plist)
{
plist->item=item;
}
void Traverse(Node *plist,void(*pfun)(Item item))
{
Node *pnew;
pnew=plist;
while (pnew!=NULL)
{
(*pfun)(pnew->item);
pnew=pnew->next;
}
}
void FreeList(Node *plist)
{
Node *pnew;
while(plist!=NULL)
{
pnew=plist;
free(plist);
plist=pnew->next;
}
}
void showmovies(Item item)
{
printf("you like film %s and socorl %d",item.name,item.mark);
}
错误调试了下 好像是指
while(scan->next!=NULL)
scan=scan->next;
scan->next=pnew 访问统一快内存空间 请大侠指教下 怎么修改
|
最佳答案
查看完整内容
改动:
struct film
{
char* name[40];
int mark;
};
谢谢!希望可以帮你解决!
|