C语言-指针链表

//pointer and list    list:creat,insert,delete function progamer

#include
#include

#define PT "Student Number:%ld name:%-10s score:%.1f\n",p->num,p->name,p->score
#define N sizeof(struct stud) 
struct stud{
	long num;
	char name[11];
	float score;
	struct stud *next;
};
 print(struct stud *p){
p=p->next;
while(p!=NULL)
{
	printf(PT);
	p=p->next;
}
 }
 
 //Creat linked list 
 struct stud *creat(void){
	 struct stud *p1,*p2,*head;
	 head=p2=(struct stud *)malloc(N);
	 /*head ,p2 point to head node*/
	printf("Please input student number ,name,score:(std_num input 0 end)\n");
	p1=(struct stud *)malloc(N);
	/*p1 point to the first node*/
	scanf("%d %s %f",&p1->num,&p1->name,&p1->score);
	while(p1->num!=0)
	{
		p2->next=p1;
		//new node cat the end of linked list
		p2=p1;
		//p2 point to the new end list 
		p1=(struct stud *)malloc(N);
		//p1 points to new apply for node
		scanf("%d %s %f",&p1->num,&p1->name,&p1->score);
	}
	 p2->next=NULL;
	 //footer node  next domain get empty
 free(p1);
 //release void node
 return head;
 
 }
 /****************insert node *******************/
 
 int insert(struct stud *p0){
	 struct stud *p;
	 p=(struct stud *)malloc(N);
	 //p points to new application of node
	 printf("Please input insert student number name score\n");
	 scanf("%d %s %f",&p->num,&p->name,&p->score);
	 while(p0->next!=NULL&&p0->next->numnum)
		 // find with duplicate
		 {
			free(p);
			//The release of the new node storage space
				return 0;
		}
	p->next=p0->next;
	//after subsequent node link the after new node 
	p0->next=p;
	//The new node link to the precursor
	return 1;
 }
 /****************Linked list node deletion********************/
 int Delete(struct stud *p0)
 {
	 long num;
	 struct stud *p;
	 p=p0->next;
	 if(p==NULL)
		 return 0;
	 //Only the first node is empty table,can't delete returns 0;
	 printf("Please enter the student id to delete\n DNO:");
	 scanf("%d",&num);
	 while(p!=NULL)
	 {
		 if(p->num==num)
			 //find  the node to delete[]
			 {
				 p0->next=p->next;
				 //After subsequent node link to the precursor
				 free(p);
				 return 1;
				 
			 }
			 p0=p;
			 p=p->next;
			 //Continue to find passage pointer
			 
	 }
	 
	return 0;//Want to delete the node returns 0 was not found 
	 
	 
	 
	 
	 
 }
 int main()
 {
	 
	 struct stud *head,*p;
	 head=creat();
	 print(head);
	 p=Delete(head);
	 if(p)
		 printf(PT);
	 else printf("Insert failed!\n");
	 
	 
	 
	 
	 
	 
	 return 0;
 }

 

你可能感兴趣的:(C)