该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
#include
#include
#include
typedef struct list
{
char item;
struct list* nextPtr;
}List;
typedef List* ListPtr;
void InitializeList(ListPtr* pList);
int ListIsEmpty(ListPtr hptr);
int AddItem(ListPtr* Plist);
int ListIsFull(void);
void printfList(ListPtr hPtr);
void freeList(ListPtr* pList);
int main()
{
ListPtr headPtr;
InitializeList(&headPtr);
printf("Enter 1 to Add item\nEnter -1 exit!\n");
int choice=1;
scanf("%d",&choice);
while(choice!=-1 && choice==1)
{
AddItem(&headPtr);
if(ListIsFull())
{
printf("The list is full!");
break;
}
printf("Enter your choice:");
scanf("%d",&choice);
}
if(ListIsEmpty(headPtr))
{
printf("Nothing in it !\n");
}
printfList(headPtr);
freeList(&headPtr);
return 0;
}
void InitializeList(ListPtr* pList )
{
*pList=NULL;
}
int ListIsEmpty(ListPtr hPtr)
{
if(hPtr==NULL)
return 1;
else
return 0;
}
int AddItem(ListPtr* pList)
{
printf("Enter a char:\n");
char ch;
getchar();
scanf("%c",&ch);
ListPtr newPtr=malloc(sizeof(List));
newPtr->item=ch;
ListPtr previousPtr,currentPtr;
if(*pList==NULL)
{
*pList=newPtr;
newPtr->nextPtr=NULL;
return 1;
}
else
{
previousPtr=NULL;
currentPtr=*pList;
while(currentPtr!=NULL && ch>(currentPtr->item))
{
previousPtr=currentPtr;
currentPtr=currentPtr->nextPtr;
}
if(ch==(currentPtr->item))
{
printf("you enter a same value!\n");
return 0;
}
if(previousPtr==NULL)
{
newPtr->nextPtr=currentPtr;
// currentPtr=newPtr;
*pList=newPtr;
return 1;
}
if(currentPtr==NULL)
{
currentPtr=newPtr;
newPtr->nextPtr=NULL;
return 1;
}
previousPtr->nextPtr=newPtr;
newPtr->nextPtr=currentPtr;
return 1;
}
}
int ListIsFull()
{
ListPtr pt;
pt=malloc(sizeof(List));
if(pt==NULL)
{
return 1;
}
else
{
free(pt);
return 0;
}
}
void freeList(ListPtr* pList)
{
ListPtr pt;
while(*pList!=NULL)
{
pt=*pList;
*pList=(*pList)->nextPtr;
free(pt);
}
}
void printfList(ListPtr hPtr)
{
while(hPtr!=NULL)
{
printf("%c->",hPtr->item);
hPtr=hPtr->nextPtr;
}
printf("NULL\n");
}
编译没有问题没有警告 我想知道是我的哪些细节没有实现好或者是处理字符输入那些地方需要更正
在这首先谢谢各位了!