鱼C论坛

 找回密码
 立即注册
查看: 2763|回复: 3

这是C链表,是建立和删除链表的程序,输入num(学号)和score,问一下当删除某节点,怎么使

[复制链接]
发表于 2012-7-27 22:34:55 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
//这是C链表,是建立和删除链表的程序,输入num(学号)和score,问一下当删除某节点,怎么使得学号按顺序输出?

/******************************************
**************这是一个C链表程序***********/
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>

struct student
{
        int num;
        int score;
        struct student *next;
};

void main ()
{
        struct student *creat(struct student *head);//链表函数!
        void print(struct student *head);//打印函数!
        struct student *del(struct student *head,int num);//删除函数!
        int n;
        struct student *head;
        head=NULL;
       
        head=creat(head);
        print(head);
        printf("Please input the num you will delete: ");
        scanf("%d",&n);
        print(del(head,n));
        printf("\n");
}
int n=0;
struct student *creat(struct student *head)
{
        struct student *p1,*p2;
        p1=p2=(struct student *)malloc(sizeof(struct student));
        printf("Please input the num: ");
        scanf("%d",&p1->num);
        printf("Please input the score: ");
        scanf("%d",&p1->score);
       
        p1->next=NULL;
       
        while(p1->num!=0)
        {        n++;
        if(head==NULL)
        {
                head=p1;
        }
        else
        {
                p2->next=p1;
        }
        p2=p1;
       
        p1=(struct student *)malloc(sizeof(struct student));
       
        printf("Please input the num: ");
        scanf("%d",&p1->num);
        printf("Please input the score: ");
        scanf("%d",&p1->score);
        }
        p2->next=NULL;
        return head;
}
void print(struct student *head)
{
        struct student *temp;
        temp=head;
        printf("the all record is %d\n",n);
        while(temp!=NULL)
        {       
                printf("%d %d\n",temp->num,temp->score);
               
                temp=temp->next;
        }
}

struct student *del(struct student *head,int num)
{
        struct student *p1,*p2;
        if(head==NULL)
        {
                printf("The list is a null!");
                goto END;
        }
        p1=head;
        while(p1->num!=num&&p1->next!=NULL)
        {                                                                        
                p2=p1;
                p1=p1->next;
        }
        if(p1->num==num)
        {
                if(head->num==num)
                {
                        /*struct student *temp;
                        temp=head;*/                               
                        head=head->next;        //类似于这里一样,但是下面想不出
                        /*head->num=temp->num;*/
                }
                else
                {
                        p2->next=p1->next;        //这里该怎么定义,试着学号是按顺序排列的呢?
                }
                printf("The num you enter has been deleted!success!\n");
                n--;
        }
        else
        {
                printf("The num you enter is not in!\n");
        }
END:
        return head;
}










想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
发表于 2012-7-27 23:18:41 | 显示全部楼层
本帖最后由 akon 于 2012-7-27 23:29 编辑

问下,你要输入的学号比如是1.2.3.4.5;之后删除的是3,输出的要1.2.3.4,还是1.2.4.5,要是降序的输出还要有一个排序的函数!你的意思是要排序的函数吗?下面是一个排序的函数
void px(List *h)// 把链表的头传过来
{
     List *p,*q;
     p=h;
     while(p!=NULL)
     {
          if(p->num>p->next->num)//作比较
         {    q=p;               完成替换
             p=p->next;
             p->next=q;
         }
         p=p->next;
     }
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
发表于 2012-7-27 23:54:19 | 显示全部楼层
不太明白LZ 的意思 可以说清楚点嘛  
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
 楼主| 发表于 2012-7-28 09:08:49 | 显示全部楼层

就是如果输入 01 99  02 98  03 97  04 96  删除02   会输出01 99  03 97  04 96 怎么使得此输入为01 99  02 97 03 96
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-25 08:42

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表