데이터 구조 실습 2 Debug

2275 단어
#include<stdafx.h>
#include<stdlib.h>
typedef struct student
 {  int  num ; 
    int  score ;
    struct student *next ;
 }ST;
#define   LEN   sizeof(ST)
int  n;
ST *creat(void)
{
	ST *p1,*p2,*head=NULL;
	int n=0;
	p1=(ST *) malloc (LEN); //       
	if(p1==NULL)
	{
		printf("
No enough memory!
");exit(0); } scanf("%d %d",&p1->num,&p1->score); while ( p1->num!=0 ) { n=n+1;// 1 if(n==1)head=p1;// n 1, p1 else p2->next=p1; p2=p1;// p2 p1 p1=(ST *) malloc (LEN);//p1 if(p1==NULL) { printf("
No enough memory!
");exit(0); } scanf("%d %d",&p1->num, &p1->score); } p2->next =NULL; // free(p1);// p1 return(head); } void list(ST *head) { ST *p; p=head; while (p!=NULL) { printf("%d,%d
",p->num,p->score); p=p->next ; } } ST *del(ST *head, int num) { ST *p,*q=NULL; p=head; while((num!=p->num)&&(p->next!=NULL)) { q=p; p=p->next; } if(num==p->num) { if(p==head)head=p->next;// else q->next=p->next;// free(p); // n=n-1; // 1 printf("deleted !
"); } else printf("can not delete!
"); return(head);// } ST *insert(ST *head) { ST *p0,*p1,*p2; p1=head; p0=(ST*)malloc(LEN); scanf("%d%d",&p0->num,&p0->score); if(head==NULL)// , { head=p0; p0->next=NULL; } else { while((p0->num>p1->num)&&(p1->next!=NULL)) { p2=p1; p1=p1->next; }// if(p0->num<=p1->num) { if(head==p1)head=p0;// else p2->next=p0;// p0->next=p1; } else { p1->next=p0;p0->next=NULL; }// } n++;// 1 return( head ) ; } void main() { ST *h=NULL; h=creat(); list(h); h=del(h,2); list(h); h=insert(h); list(h); }

좋은 웹페이지 즐겨찾기