线性链表的应用。。大虾看看啊。。不知哪错了。
#include <stdio.h>#include<stdlib.h>
struct Grade_Info
{
int score;
struct Grade_Info *next;
};
typedef struct Grade_Info NODE;
NODE *Create_LinkList();
void Insert_LinkList(NODE *head,NODE *pnew,int i);
void Delete_LinkList(NODE *head,int i);
void Display_LinkList(NODE *head);
void Free_LinkList(NODE *head);
void main()
{
NODE *head,*pnew;
head=Create_LinkList(); //create linklist
if(head==NULL) //if failed
return;
printf("after create:");
Display_LinkList(head);
pnew=(NODE*)malloc(sizeof(NODE));
if(pnew==NULL)
{
printf("no enough memory!\n");
return;
}
pnew->score=88;
Insert_LinkList(head,pnew,3);
printf("after insert");
Display_LinkList(head);
Delete_LinkList(head,3);
printf("after delete:");
Display_LinkList(head);
Free_LinkList(head);
}
NODE *Create_LinkList()
{
NODE *head,*pnew,*tail;
int score;
head=(NODE *)malloc(sizeof(NODE)); //Create head point1;
if(head==NULL) //验证内存 ,防止溢出
{
printf("no enough memory! ");
return(NULL);
}
head->next=NULL;
tail=head;
printf("input the score of student: \n");
while(1)
{
scanf("%d",&score);
if(score<0)
break;
pnew=(NODE *)malloc(sizeof(NODE)); //Create first point
if(pnew==NULL) //验证内存 ,防止溢出
{
printf("no enough memory! ");
return(NULL);
}
pnew->score=score; //存入分数
pnew->next=NULL; //第一个节点的指针域置为NULL
tail->next=pnew; //头指针域指向第一个节点
tail=pnew; //为指针指向第一个节点
}
return(head);
}
void Insert_LinkList(NODE *head,NODE *pnew,int i)
{
NODE *p;
int j;
p=head;
for(j=0;j<i&&p->next!=NULL;j++)
p=p->next;
if(p==NULL)
{
printf("the %d node not foundt!\n");
return;
}
pnew->next=p->next;
p->next=pnew;
}
void Delete_LinkList(NODE *head,int i)
{
NODE *p,*q;
int j;
if(i==0)
{
return;
}
p=head;
for(j=1;j<i && p->next!=NULL;j++)
p=p->next;
if(p->next==NULL)
{
printf("the %d node not found!\n",i);
return;
}
q=p->next; //q指向待删节点i
p->next=q->next; //删除i节点
free(q);
}
void Display_LinkList(NODE *head)
{
NODE *p;
for(p=head->next;p!=NULL;p->next)
printf("%d",p->score);
printf("\n");
}
void Free_LinkList(NODE *head)
{
NODE *p,*q;
p=head;
while(p->next!=NULL)
{
q=p->next;
p->next=q->next;
free(q);
}
free(head);
}