第九次作业

1.将学生绩点组成链表

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define null 0
typedef struct student
{
    char name[50];
    char num[50];
    float score;
    struct student *next;
}student;

typedef struct head
{
    int n;
    float ave;
    student *next;
}head;


head *Creat()
{
    float sum = 0;
    head *h;
    student *p,*t;
      p = (student*)malloc(sizeof(student));
      h = (head*)malloc(sizeof(head));

    h->n = 0;
    h->next = null;    
    t = p;
    while (1)
    {
          scanf("%s",p->name);
        if (p->name[0] == '*') 
        {break;}
          scanf("%s",p->num);
          scanf("%f",&p->score);
        sum += p->score;
        p->next = null;

        if (h->n == 0)
        {
            h->next = p;
        }
        else t->next=p;        
        t = p;
        p = (student*)malloc(sizeof(student));
        h->n++;

    }
    h->ave = sum/h->n;
    return h;
}

int main(void)
{
    head *H,*H1;
    student *P,*N,*T;
    int a = H->n;
    P = (student*)malloc(sizeof(student));
    H1 = (head*)malloc(sizeof(head));
    H1->next = null;
    H1->n = 0;
    T = P;

    H = Creat();
    P = H->next;
    for (int i = 0; i < H->n; i++)
    {
        if (P->score>H->ave)
        {
            if (H1->n == 0)
            {
                H1->next = P;
                T = H1->next;
            }
            else T->next=P;
            T = P;
            H1->n++;
        }
        P = P->next;
    }
    for (int i = 0; i < H1->n; i++)
    {
        if (i==0)
        {
              printf("%s\n",H1->next->name);
            P = H1->next;            
        }
        else
          printf("%s\n",P->name);
        P = P->next;
    }
    
}

2.删除链表后为空输出null

#include<stdio.h>

#include<stdlib.h>

typedef struct head
{
    int n;
    struct numbers *next;
}head;

typedef struct numbers
{
    int data;
    struct numbers *next;
}numbers;

head *Create()
{
    head *h;
    numbers *p;
    numbers *t;

    p=(numbers*)malloc(sizeof(numbers*));
    h=(head*)malloc(sizeof(head*));

    while (1)
    {
          scanf("%d",&p->data);
          
        if (p->data == -1) {break;}
        
        p->next=NULL;

        if (h->n == 0)
        {
            h->next=p;        
        }
        else 
        t->next=p;
        t=p;
        
        p=(numbers*)malloc(sizeof(numbers*));
        h->n++;
    }
    
        return h;
}

head *Delect(head* h)
{
    head *h1;
    numbers *p,*t;
    t=(numbers*)malloc(sizeof(numbers*));
    h1=(head*)malloc(sizeof(head*));
    p=(numbers*)malloc(sizeof(numbers*));
    h1->n=0;
    t = h->next;

    for (int i = 0; i < h->n; i++)
    {
        if (t->data%2==0)
        {
            if (h1->n == 0)
            {
                h1->next = t;
            }
            else p->next = t;
            p = t;
            h1->n++;
        }
        t = t->next;
    }
    
      return h1;
}

int main(void)
{
    int n;
      scanf("%d",&n);

    for (int i=0;i<n;i++)
    {
        head *h;
        
        numbers *num;

        h = Delect(Create());
        
        if (h->n == 0)
        {
              printf("NULL\n");
            continue;
        }
        for (int i=0;i < h->n ;i++)
        {
            if (i == 0)
            {
                  printf("%d ",h->next->data);
                num = h->next->next;
            }
            else
            {
                  printf("%d ",num->data);
                  num = num->next;
            }
            
        }
                  printf("\n");
    }
    
}

3.设计函数构造出s1与s2的交集新链表s3

#include<stdio.h>
#include<stdlib.h>

 typedef struct head
{
    int n;
    struct numbers *next;
}head;

typedef struct numbers
{
    int data;
    struct numbers *next;
}numbers;

head *Create()
{
    head *h;
    numbers *p;
    numbers *t;
    numbers *end;

    p=(numbers*)malloc(sizeof(numbers*));
    end=(numbers*)malloc(sizeof(numbers*));
    h=(head*)malloc(sizeof(head*));

    end->data = -1;
    end->next = NULL;
    while (1)
    {
          scanf("%d",&p->data);
        if (p->data == -1) {t->next = end;break;}
        p->next=NULL;

        if (h->n == 0)
        {
            h->next=p;        
        }
        else t->next=p;
        
        t=p;
        
        p=(numbers*)malloc(sizeof(numbers*));
        
        h->n++;
    }
    
      return h;
}

head *Select(head *h1,head *h2)
{
    head *h;
    h=(head*)malloc(sizeof(head*));
    h->n = 0;

    numbers *p1,*p2,*t;
    p1 = h1->next;
    p2 = h2->next;
    t = p1;
    for (;;)
    {
        if (p1->data == -1||p2->data == -1)
        {
            break;
        }
        
        if (p1->data==p2->data)
        {

            if (h->n == 0)
            {
                h->next = p1;
            }
            else t->next = p1;
        
            t = p1;
            h->n++;
            p1 = p1->next;
        }
        else if (p1->data>p2->data)
        {
            p2 = p2->next;
        }
        else if (p1->data<p2->data)
        {
            p1 = p1->next;
        }
        
        
    }

      return h;
}

int main(void)
{
    head *h1,*h2,*h3;
    numbers *p;

    h1 = Create();
    h2 = Create();
    h3 = Select(h1,h2);

    if (h3->n == 0)
    {
          printf("NULL");
          return 0;
    }
    else
    {
        p = h3->next;
        for (int i=0; i<h3->n;i++)
        {
              printf("%d ",p->data);
            p = p->next;
        }
        
    }
}

4.给你·一些整数,频繁地插入和删除某些元素,查找某个元素或输出链表中所有元素

#include<stdlib.h>
#include<stdio.h>
typedef struct head
{
    int n;
    struct numbers *next;
}head;

typedef struct numbers
{
    int data;
    struct numbers *next;
}numbers;

int n;

head *Create()
{

    head *h;
    numbers *p;
    numbers *t;

    p=(numbers*)malloc(sizeof(numbers*));
    h=(head*)malloc(sizeof(head*));
      scanf("%d",&n);
    int ints[30];
    for (int i = 0; i < n; i++)
    {
          scanf("%d",&ints[n-i-1]);
    }
    
    for(int i = 0;i<n;i++)
    {
        p->data = ints[i];
        p->next=NULL;

        if (h->n == 0)
        {
            h->next=p;        
        }
        else t->next=p;
        t=p;
        p=(numbers*)malloc(sizeof(numbers*));
        h->n++;
    }
    
       return h;
}

int Show(head*h)
{   
    if (n ==0)
    {
          printf("Link list is empty\n");
        return 0;
    }
    
    numbers *p;
    p = h->next;
    for (int i = 0; i < n; i++)
    {
          printf("%d ",p->data);
        p = p->next;
    }
      printf("\n");
}

head *Delect(head*h)
{
    int m;
    numbers*p;

    p = h->next;
      scanf("%d",&m);
    if (m>n)
    {
          printf("delete fail\n");
          return h;
    }
    if (m==1)
    {
        h->next =h->next->next;
    }
    else
    {
    for (int i = 0; i < n; i++)
    {
        if (i+2 == m)
        {
            p->next = p->next->next;
            break;
        }
        p = p->next;
    }
    }
    n--;
       printf("delete OK\n");
    return h;
}

head *Insect(head *h)
{
    int m;
    
    numbers *t,*p;
    
    t=(numbers*)malloc(sizeof(numbers*));
    
    p = h->next;
    
    scanf("%d %d",&m,&t->data);

    if (m>n+1)
    {
          printf("insert fail\n");
          return h;
    }
    
    if (m == 1)
    {
        t->next=h->next;
        h->next=t;
    }
    else
    {
        for (int i=0;i<n;i++)
        {
            if (i+2==m)
            {
                t->next=p->next;
                p->next=t;
                break;          
            }
            p=p->next;       
        }
        
    }
      printf("insert OK\n");  
    n++;
    return h;
}

int Get(head *h)
{
    int m;
      scanf("%d",&m);
    numbers *p;
    p = h->next;
    if (m>n)
    {
         printf("get fail\n");
    }
    
    for (int i=1;i<n+1;i++)
    {
        if (i==m)
        {
             printf("%d\n",p->data);
             return 0;
        }
        p = p->next;        
    }
    
}

int main(void)
{
    int count;
    char choice[6];
    head *h;
    h = Create();
      scanf("%d",&count);
    for (int i = 0; i < count; i++)
    {
          scanf("%s",choice);
        
        if (choice[0] == 's') {Show(h);}
        else 
        if (choice[0] == 'g') {Get(h);}
        else 
        if (choice[0] == 'i') {h=Insect(h);}
        else 
        if (choice[0] == 'd') {h=Delect(h);}
        
    }
    
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值