回 帖 发 新 帖 刷新版面

主题:[讨论]学生信息表的建立.删除

学生信息表的建立.删除
可动态运算俩个一元多项式相加

回复列表 (共3个回复)

沙发

#include<stdio.h>
#include<malloc.h>
#define LEN sizeof(struct student)
//#define NULL 0

struct student
{
       long int number;
       float score;
       struct student *next;
};

int length=0;

struct student* Creat(void);

void Print(struct student* head);

struct student* Insert(struct student* head,struct student* stu);

struct student* Delete(struct student* head,long num);

int main()
{
    struct student* head;
    head=Creat();//创建学生信息 
    Print(head);//显示学生信息 
    struct student* stu;
    stu=(struct student*)malloc(LEN);
    printf("Enter the inserted student's number and score: ");
    scanf("%ld%f",&stu->number,&stu->score);
    head=Insert(head,stu);//插入学生信息 
    Print(head);
    long int num;
    printf("Enter the student,s number: ");
    scanf("%ld",&num);
    head=Delete(head,num); //删除学生信息 
    Print(head);
    system("pause");
    return 0;
}

struct student* Creat(void)
{
       struct student* head;
       struct student* p1,*p2;
       p1=(struct student*)malloc(LEN);
       p2=(struct student*)malloc(LEN);
       head=NULL;
       printf("Enter the %d student's number and score: ",length+1);
       scanf("%ld%f",&p1->number,&p1->score);
       while(p1->number!=0)
       {
           ++length;
           if(length==1)
              head=p1;
           else 
              p2->next=p1;
           p2=p1;
           p1=(struct student*)malloc(LEN);
           printf("Enter the %d student's number and score: ",length+1);
           scanf("%ld%f",&p1->number,&p1->score);
       }
       p2->next=NULL;
       return (head);
}

void Print(struct student* head)
{
     struct student* p;
     p=head;
     if(head==NULL)     
       printf("No  student's information!\n");
     else
     {
         printf("The student's information are:\n");
         while(p!=NULL)
         {
            printf("Number:%ld\tScore:%f\n",p->number,p->score);
            p=p->next;
         }
     }
}       

struct student* Insert(struct student* head,struct student* stu)
{
       
       struct student*p0,*p1,*p2;  
       p1=head;
       p0=stu;
       if(head==NULL)
       {
           head=p0;
           p0->next=NULL;
       }
       else
       {
           while((p0->number>p1->number)&&(p1->next!=NULL))
           {
               p2=p1;
               p1=p1->next;
           }
           if(p0->number<=p1->number)
           {
              if(head==p1)//在首部添加 
                head=p0;
              else        //在中间添加 
                 p2->next=p0;
              p0->next=p1;
           }
           else  //在尾部添加 
           {
               p1->next=p0;
               p0->next=NULL;
           }
       }
           ++length;
           return (head);
}

struct student* Delete(struct student* head,long num)
{
       struct student *p1,*p2;
       if(head==NULL)
       {
          printf("List NULL!\n");
          exit(0);
       }
       p1=head;
       while(num!=p1->number&&p1->next!=NULL)
       {
           p2=p1;
           p1=p1->next;
       }
       if(num==p1->number)
       {
          if(p1==head)
            head=p1->next;
          else
             p2->next=p1->next;
          printf("Delete: %ld\n",num);
          --length;
       }
       else 
          printf("%ld not been found!\n",num);
       return (head);
}
       

板凳


上面楼主好像只写了学生信息管理的程序,这是我写的多项式相加,刚学数据结构。
看可不可以。。。。。。
#include<stdio.h>
#include<stdlib.h>
typedef struct linknode
{
    int data;
    struct linknode  *next;
}node1;
 typedef  struct linknode  *node;
/*给带头结点的链表放入数值*/
node creat_linknode(void)
{
    int x;
    node s,p,head;
    p=head=(node)malloc(sizeof(node1));
    printf("please input some intengers:\n");
    scanf("%d",&x);
    while(x!=-1)
    {
         s=(node)malloc(sizeof(node1));
         s->data=x;
         p->next=s;
         p=s;
         scanf("%d",&x);

     }
    p->next=NULL;
    return head;
}
void print_linknode(node head,int x)
{
    node p;
    int i=1;
    p=head->next;
    if(!p)
     {   printf("it is empty");}
    else
    while(p&&i)
    {
        printf("%4d",p->data);
        p=p->next;
        if(i%2)
            printf("(%d)",x);
        i++;

    }
    printf("\n");

}
/*对两个多项式进行相加并保证原来链表不变*/
node sum(node head1,node head2)
{
    node p1,q1,head3,p2,q2,r,s,t=NULL;

    head3=(node)malloc(sizeof(node1)); /*申请头结点*/
    head3->next=NULL;
    r=head3;
    p1=head1->next;
    while(p1)
    {
         s=(node)malloc(sizeof(node1));
         s->data=p1->data;               /*拷贝链表head1给链表head3*/
         r->next=s;
         r=s;
         p1=p1->next;
    }
    r->next=NULL;
    q1=head2->next;                      /*q1指向head2的第一个值*/
    while(q1) /* 外循环是让每一个head2中的值跟head3比较*/
    {
        q2=q1;q1=q2->next;  /*q2指的是系数,而q1指的是指数*/

        p2=head3->next; p1=p2->next;
        while(p2&&q1->data!=p1->data)
        {
            t=p1;p2=p1->next;p1=p2->next;
        }

        if(p2)
        {
            s=(node)malloc(sizeof(node1));
            s->data=p2->data+q2->data;

            if(t==NULL)
            {
                s->next=p1;
                head3->next=s;
            }
            else
            {
                s->next=p1;
                t->next=s;
            }
            free(p2);
        }
        else
        {
            s=(node)malloc(sizeof(node1));
            s->data=q2->data;
            r->next=s;
            r=s;

            s=(node)malloc(sizeof(node1));
            s->data=q1->data;
            r->next=s;
            r=s;

        }
        q1=q1->next;

    }
    r->next=NULL;
    return head3;
}
/*计算出x的 n次方值*/
long mi(int x,int n)
{
    long result;
    if(n==0)
     result=1;
    else
    result=x*mi(x,n-1);
    return result;
}
/*计算出多项表达式的值*/
long jisuan(node head,int x)
{
    node p,pre;
    long sum=0;;
    p=head->next;
    while(p)
    {  pre=p;
       p=p->next;
       sum=sum+pre->data*mi(x,p->data);
       p=p->next;

    }
    return sum;
}
main()
{

    node A,B,C;
    int x;

    printf("please in put an intenger x=");/*其中x是指指数*/
    scanf("%d",&x);

    A=creat_linknode();
    B=creat_linknode();

    print_linknode(A,x);
    print_linknode(B,x);

    C=sum(A,B);
    printf("A(%d)+B(%d)=",x,x); /*打印出相加后的多项式*/
    print_linknode(C,x);

    printf("A(%d)+B(%d)=%ld",x,x,jisuan(C,x)); /*打印和的值*/
    getch();

}
用的是win-TC编的,如果用Vc++只要把getch();去掉就可以了

3 楼

不错啊!!
顶!!

我来回复

您尚未登录,请登录后再回复。点此登录或注册