动态链表的创建部分貌似哪里的指针有问题,请大家帮忙看看
程序代码:/*
*.编程实现一个单向项链表,能实现链表的创建、插入、删除、显示功能。
*/
#include<stdio.h>
#include<stdlib.h>
#include<iostream.h>
struct stu
{
char name[20];
long num;
float score;
struct stu *next;
};
int n;
stu *create()
{
stu *head,*pNext,*p;
short i,N;
head=(stu *)malloc(sizeof(stu));
p=head;
printf("input the number of student:\n");
scanf("%d",&N);
printf("input name and number for nodes:\n");
scanf("%c,%ld,%f",head->name,&head->num,&head->score);//为头结点赋值
for(i=0;i<N;i++)
{
pNext=(stu *)malloc(sizeof(stu));//连续开辟结构体空间
scanf("%c,%ld,%f",pNext->name,&pNext->num,&pNext->score);
p->next=pNext;
p=pNext;
}
//printf("please input the students' information:\n");
/*for(i=1;i<N;i++)
{
p=p->next;
}*/
cout<<head<<endl;
pNext->next=NULL;
return head;
}
void print(stu *head)//查询链表
{
stu *p;
printf("Output the infomation in the chain list:\n");
p=head;
if(head!=NULL)
do
{
printf("%c,%ld,%f",p->name,p->num,p->score);
p=p->next;
}while(p!=NULL);
}
stu *insert(stu * head,stu *pInsert)
{
stu *temp,*p,*pIns;
p=head;
pIns=pInsert;//指向要插入的结点
if(head==NULL)
{
head=pIns;
pIns->next=NULL;
}
else
{
while((pIns->num>p->num)&&(p->next!=NULL))
{
temp=p;//temp指向刚才p所指的结点
p=p->next;//p后移一个结点
}
if(pIns->num<=p->num)
{
if(head==p)
head=pIns;//插到原来第一个结点之前
else
{
temp->next=pIns;
pIns->next=p;
}
}
else//一定是为结点
{
p->next=pIns;
pIns->next=NULL;
}
}
n=n+1;
printf("insert success!");
return head;
}
stu *dele(stu *head,long num)
{
stu *p1,*p2;
if(head==NULL)
printf("empty list!\n");
p1=head;
while(p1->num!=num&&p1->next!=NULL)
{
p2=p1;
p1=p1->next;
}
if(p1->num==num)
{
if(p1==head)
head=p1->next;
else
p2->next=p1->next;
n=n-1;
free(p1);
}
else
printf("the node not be found!\n");
return head;
}
int main()
{
stu *cl,*stud;
int i;
long num;
printf("*************************************************\n");
printf( "select the item \n");
printf( "1:set the students' information \n");
printf( "2:query the students' information \n");
printf( "3:insert the students'information \n");
printf( "4:delete the students'information \n");
printf( "0:ESC the program! \n");
printf("*************************************************\n");
do
{
printf("select the number to next");
scanf("%d",&i);
switch(i)
{
case 1:
{
//printf("Please input the students' information:\n");
cl=create();
cout<<cl<<endl;
}break;
case 2:
{
print(cl);
}break;
case 3:
{
printf("Please input the students' information:\n");
scanf("%c,%ld,%f",stud->name,&stud->num,&stud->score);
cl=insert(cl,stud);
}break;
case 4:
{
printf("Please input the delete number:\n");
scanf("%ld",&stud->num);
cl=dele(cl,num);
}break;
case 5:
exit(0);
default:break;
}
}while(i!=0);
return 0;
}






