鱼C论坛

 找回密码
 立即注册
查看: 2860|回复: 10

C 语言结构指针,请大牛帮忙看看链表的创建和增删功能的代码。

[复制链接]
发表于 2013-1-30 17:38:35 | 显示全部楼层 |阅读模式
5鱼币
C 语言结构指针,请大牛帮忙看看链表的创建和增删功能的代码。
帮忙看看问题出在什么问题,老卡死。
另外,帮忙看看,是不是还可以再优化?
在此深表感激!
 #include <stdio.h>
#include <malloc.h>
#include <stdlib.h>#define LEN sizeof(struct stu)int n=0;//*****************************************************************
//定义一个学生结构stu,参数只包含了学号和成绩,num,score
//还有一个指向下一个结点的next指针
//*****************************************************************struct stu
{
 int num;
 int score;
 struct stu *next;
};//*****************************************************************
//声明主要用到的函数,创建链表,及对链表的增删改操作的函数
//*****************************************************************
struct stu *create(); //创建
void print(struct stu *head);//输出
struct stu *del(struct stu *head,int num); //删除节点
struct stu *inser(struct stu *head,struct stu *stu_2);//插入节点
//*****************************************************************
//主函数MAIN()
//*****************************************************************
void main()
{
 //定义一个结构指针,用来接收创建的链表
 struct stu *T;
 int num;
 T = create(); //打印输出链表
 print(T); //删除节点
 printf("Please input the num that You want to deleted:");
 scanf("%d",&num);
 T = del(T,num);
 print(T); //插入节点
 
 printf("Please input the data of insert!\n"); struct stu *stu_2; stu_2 = (struct stu *)malloc(LEN); printf("Please input the data of num:");
 scanf("%d",&stu_2->num);
 printf("Please input the data of score:");
 scanf("%d",&stu_2->score);
 T = inser(T,stu_2);
 print(T);
 
 
 system("pause");
}
//*****************************************************************
//创建表
//*****************************************************************
struct stu *create()
{
 struct stu *head;//头指针
 struct stu *p1,*p2;//动态指针,p2跟随p1,保存p1的足迹,以备所需 //用户输入数据前,要手动为结构开辟一个结构空间,并将p1,p2指向此结构地址
 p1 = p2 = (struct stu *)malloc(LEN);
 //提示用户输入数据
 printf("Please input the data of num:");
 scanf("%d",&p1->num);
 printf("Please input the data of score:");
 scanf("%d",&p1->score); head = NULL;
 while(0 != p1->num)
 {
  n++;
  if(1 == n)
  {
   head = p1;
  }
  else
  {
   p2->next = p1;
   
  }
  p2 = p1;//此处注意:必须先将p1指向下一个结点,再把p2跟过来,最后开辟新的内存空间
   
  //开避下一个节点内存空间
  p1 = (struct stu *)malloc(LEN);  printf("Please input the data of num:");
  scanf("%d",&p1->num);
  printf("Please input the data of score:");
  scanf("%d",&p1->score);
 }
 p2->next = NULL;
 return head;
}//*****************************************************************
//打印表
//*****************************************************************
void print(struct stu *head)
{
 //定义指针结构变量来接收链表
 struct stu *p;
 p = head; printf("共有%d条记录被统计\n",n); if(NULL != head)
 {
  while (NULL != p)
  {
   printf("\n该学生学号是:%d,成绩是:%d \n",p->num,p->score);
   p = p->next;
  };
 }
}//*****************************************************************
//删除节点
//*****************************************************************
struct stu *del(struct stu *head,int num)
{
 struct stu *p1,*p2; if(NULL != head)
 {
  p1 = head;  while(num != p1->num && NULL != p1->next)
  {
   
   p2 = p1;
   p2->next = p1;
  }  if(num == p1->num)
  {
   if(head = p1)
   {
    head = p1->next;
   }
   else
   {
    p2->next = p1->next;
   }
   printf("Delete No.%d SUCCEDD!\n",num);
  }
  else 
  {
   printf("No.%d is not found!",num);
  }
  n = n - 1;
 }
 else
 {
  printf("This is a Null table!");
  head = NULL;
 }
 
 return head;
}//*****************************************************************
//插入节点
//*****************************************************************
struct stu *inser(struct stu *head,struct stu *stu_2)
{
 struct stu *p0;
 struct stu *p1,*p2; p0 = stu_2;
 p1 = head; if(NULL == head)
 {
  head = p0;//如果是空的链表,就将头指针指向此将要插入的节点
  p0->next = NULL;
 }
 else
 {
  while((p0->num > p1->num) && (NULL != p1->next)) //如果学号小于或等于下一个节点的学号且,下一个节点非空,就继续遍历此表
  {
   
   p2 = p1;
   p2->next = p1;
  }  if(p0->num <= p1->num) //如果将要插入的节点的学号等于下个节点的学号,且此节点为头节点,则将head指向p0。即表头插入。
  {
   if(head == p1)
   {
    head = p0;
   }
   else //中间插入
   {
    
    p2->next = p0;
    
   }
   p0->next = p1;
  }
  else //表尾插入
  {
   p1->next = p0;
   p0->next = NULL;
  }
 }
 n = n + 1;
 return head;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
发表于 2013-1-30 17:38:36 | 显示全部楼层
本帖最后由 消失在黑暗中 于 2013-1-31 20:26 编辑
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
#define LEN sizeof(struct stu)
int n=0;
//*****************************************************************
//定义一个学生结构stu,参数只包含了学号和成绩,num,score
//还有一个指向下一个结点的next指针
//*****************************************************************
struct stu
{
        int num;
        int score;
        struct stu *next;
};//*****************************************************************
//声明主要用到的函数,创建链表,及对链表的增删改操作的函数
//*****************************************************************
struct stu *create(); //创建
void print(struct stu *head);//输出
struct stu *del(struct stu *head,int num); //删除节点
struct stu *inser(struct stu *head,struct stu *stu_2);//插入节点
//*****************************************************************
//主函数MAIN()
//*****************************************************************
void main()
{
        //定义一个结构指针,用来接收创建的链表
        struct stu *T;
        int num;
        T = create(); //创建链表
        print(T); //打印链表
        printf("Please input the num that You want to deleted:");
        scanf("%d",&num);
        T = del(T,num);//删除节点
        print(T); //打印链表
        printf("Please input the data of insert!\n"); 
        struct stu *stu_2; 
        stu_2 = (struct stu *)malloc(LEN);
        printf("Please input the data of num:");
        scanf("%d",&stu_2->num);
        printf("Please input the data of score:");
        scanf("%d",&stu_2->score);
        T = inser(T,stu_2);
        print(T);
        system("pause");
}
//*****************************************************************
//创建表
//*****************************************************************
struct stu *create()//修改了这个函数,按照楼主的意思,如果输入的num等于0,结束链表的添加,此函数至少返回
//一个头结点
{
        struct stu *head = NULL;//头指针
        struct stu *p1,*p2;//动态指针,p2跟随p1,保存p1的足迹,以备所需 //用户输入数据前,要手动为结构开辟一个结构空间,并将p1,p2指向此结构地址
        while(1)
        {
                p1 = (struct stu *)malloc(LEN); 
                printf("Please input the data of num:");
                        scanf("%d",&p1->num);
                if(head == NULL)
                {
                     p2  =   head = p1;
                }
                else
                {
                        p2->next = p1;
                }
                p1->next = NULL;
                if (p1->num == 0)//假如输入的学号为0,结束链表的添加 
                {
                                 p2->next = NULL;
                                break;
              }
              n++;
                printf("Please input the data of score:");
                scanf("%d",&p1->score);
                p2 = p1;//此处注意:必须先将p1指向下一个结点,再把p2跟过来,最后开辟新的内存空间
        }
        return head;
}//*****************************************************************
//打印表
//*****************************************************************
void print(struct stu *head)
{
        //定义指针结构变量来接收链表
        struct stu *p;
        p = head; 
        printf("共有%d条记录被统计\n",n); 
        if(NULL != head)
        {
                while (NULL != p)
                {
                        printf("\n该学生学号是:%d,成绩是:%d \n",p->num,p->score);
                        p = p->next;
                };
        }
}//*****************************************************************
//删除节点
//*****************************************************************
struct stu *del(struct stu *head,int m_num)//修改了删除函数
{
        struct stu *p1,*p2;
        p1 = head;
        if (head != NULL)
        {
                while (m_num != p1->num)
                {
                        p2 =p1;//保存节点指针
                        p1 = p1->next;//获取下一个节点地址
                        if (p1 == NULL)
                        {
                                printf("No.%d is not found!",m_num);
                                return head;
                        }//找不到节点,返回head
                }
                if (p1 == head)
                        head = p1->next;
                else 
                        p2->next = p1->next;
                free(p1);//释放删除的节点
                n--;
        }
        return head;
}//*****************************************************************
//插入节点
//*****************************************************************
struct stu *inser(struct stu *head,struct stu *stu_2)//我试了下插入的功能,能正常插入,所以没改
{
        struct stu *p0;
        struct stu *p1,*p2; p0 = stu_2;
        p1 = head; 
        if(NULL == head)
        {
                head = p0;//如果是空的链表,就将头指针指向此将要插入的节点
                p0->next = NULL;
        }
        else
        {
                while((p0->num > p1->num) && (NULL != p1->next)) //如果学号小于或等于下一个节点的学号且,下一个节点非空,就继续遍历此表
                {
                        
                        p2 = p1;
                        p2->next = p1;
                }  if(p0->num <= p1->num) //如果将要插入的节点的学号等于下个节点的学号,且此节点为头节点,则将head指向p0。即表头插入。
                {
                        if(head == p1)
                        {
                                head = p0;
                        }
                        else //中间插入
                        {
                                
                                p2->next = p0;
                                
                        }
                        p0->next = p1;
                }
                else //表尾插入
                {
                        p1->next = p0;
                        p0->next = NULL;
                }
        }
        n = n + 1;
        return head;
}

评分

参与人数 1鱼币 +2 贡献 +1 收起 理由
lyoal + 2 + 1

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2013-1-30 17:46:17 | 显示全部楼层
ee。。。看不懂
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2013-1-30 18:32:42 | 显示全部楼层
有够乱的,卡住是程序有问题哈。
del的时候,修改代码:
//*****************************************************************
//删除节点
//*****************************************************************
struct stu *del(struct stu *head,int num)
{        
        struct stu *p1,*p2; 
        if(NULL != head)
        {
                p1 = head;  
                while(num != p1->num && NULL != p1->next)
                {   
                        p2 = p1;
                        p1 = p1->next;                        
                        //p2->next = p1;
                }  
                if(num == p1->num)
                {
                        p2->next = p1->next;
                        /*if(head = p1)
                        {
                                head = p1->next;
                        }else
                        {
                                p2->next = p1->next;
                        }*/                        
                        printf("Delete No.%d SUCCEDD!\n",num);
                        n = n - 1;
                }else 
                {
                        printf("No.%d is not found!",num);
                }
                
        }else
        {
                printf("This is a Null table!");
                head = NULL;
        } 
        return head;
}
增加元数的时候,修改代码:
//*****************************************************************
//插入节点
//*****************************************************************
struct stu *inser(struct stu *head,struct stu *stu_2)
{
        struct stu *p0;
        struct stu *p1,*p2; 
        p0 = stu_2;
        p1 = head; 
        if(NULL == head)
        {
                head = p0;//如果是空的链表,就将头指针指向此将要插入的节点
                p0->next = NULL;
        }else
        {
                //寻找最末端
                while(NULL!=p1->next)
                {
                        p1 = p1->next;
                }
                //添加元数
                p1->next = p0;
                p0->next = NULL;

                /*
                while((p0->num > p1->num) && (NULL != p1->next)) //如果学号小于或等于下一个节点的学号且,下一个节点非空,就继续遍历此表
                {   
                        p2 = p1;
                        p2->next = p1;                        
                }  
                if(p0->num <= p1->num) //如果将要插入的节点的学号等于下个节点的学号,且此节点为头节点,则将head指向p0。即表头插入。
                {
                        if(head == p1)
                        {
                                head = p0;
                        }else //中间插入
                        {    
                                p2->next = p0;    
                        }
                        p0->next = p1;
                }else //表尾插入
                {
                        p1->next = p0;
                        p0->next = NULL;
                }
                */
        }
        n = n + 1;
        return head;
}

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2013-1-30 22:47:00 | 显示全部楼层

非常感谢,可能我说的不太清楚,这个对链表的创建后,对链表的节点的删除,插入操作中,插入分头部,中间,尾部三种插入情况,但经这样一改只剩追加节点的操作了。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2013-1-31 16:58:29 | 显示全部楼层
我是来刷墙的

                               
登录/注册后可看大图





















                               
登录/注册后可看大图

防辐射服鉴别
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2013-2-2 14:56:26 | 显示全部楼层
牛人啊,太牛逼了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2013-2-2 19:24:53 | 显示全部楼层
赚取鱼币了喽
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2013-2-2 20:49:05 | 显示全部楼层
赚取鱼币了喽
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2013-2-3 12:40:34 | 显示全部楼层
没有  钱了  找钱
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2013-6-4 23:40:39 | 显示全部楼层
没有鱼比了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2024-6-15 10:05

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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