| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1032 人关注过本帖
标题:单链表问题中插入,删除问题
只看楼主 加入收藏
yaorongfu
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2013-3-7
结帖率:0
收藏
已结贴  问题点数:20 回复次数:4 
单链表问题中插入,删除问题
#include<stdio.h>
typedef struct student{
    int data;
    long number;
    char name;
    char sex;
    int age;
    struct student*next;
}LIST;
LIST *CreatLList(int n)
{        LIST *head,*p,*q;
          int i;
if(n>0)
{
    head=(LIST *)malloc(sizeof(LIST));
    printf("请输入第一个学生信息:\n");
        scanf("%ld%s%s%d",&head->number,&head->name,&head->sex,&head->age);
    p=head;
    for(i=2;i<=n;i++)
    {
        q=(LIST *)malloc(sizeof(LIST));
        printf("请输入第一个学生信息:\n");
            scanf("%ld%s%s%d",&q->number,&q->name,&q->sex,&q->age);
        p->next=q;p=q;}
    q->next=NULL;}

else
head=NULL;
return head;
}

void OutputLList(LIST *p)
{
        
    printf("\n输出信息:\n");
    while(p){
        printf("学号:%d,姓名:%s,性别:%s,年龄:%d\n",
            p->number,p->name,p->sex,p->age);
        p=p->next;
    }
}

LIST *InsertLList(LIST *p, LIST *newnode,int iPos)
{

   
   
}
LIST *DeleteLList(LIST *p,int iPos)
{
   
   
   
   
}

int FindLList(LIST *p,int age)
{
   
}
(3) 向链表指定位置插入新结点 函数原型:LIST *InsertLList(LIST *p, LIST *newnode,int iPos); 函数参数:p为单链表头指针 newnode指向要插入的新节点 iPos为插入位置,将新节点插在第iPos个位置,若表中无第iPos个结点,则给出相应提示信息。头结点位置为0。 返回值: 单链表头指针
(4) 删除链表指定位置的结点 函数原型:LIST *DeleteLList(LIST *p,int iPos); 函数参数:p为单链表头指针 iPos 为指定位置,若表中无第iPos结点,则给出相应提示信息。头结点位置为0。 返回值: 单链表头指针
(5) 查找与给定年龄age相同的记录 函数原型:int FindLList(LIST *p,int age); 函数参数:p为单链表头指针 age 为查找条件 返回值: 查找成功,返回该结点所在位置信息,头结点位置为0。 查找失败,返回-1。后三个实在编不出来
搜索更多相关主题的帖子: long next include number 
2013-03-31 18:16
pauljames
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
等 级:千里冰封
威 望:9
帖 子:1555
专家分:10000
注 册:2011-5-8
收藏
得分:5 
链表的基本操作,数据结构的书上都有,搜寻下

经常不在线不能及时回复短消息,如有c/单片机/运动控制/数据采集等方面的项目难题可加qq1921826084。
2013-03-31 20:17
liuxiangtao
Rank: 2
等 级:论坛游民
帖 子:11
专家分:57
注 册:2013-3-18
收藏
得分:5 
数据结构书上都有讲
2013-03-31 21:16
邓士林
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:淮河河畔
等 级:贵宾
威 望:61
帖 子:2392
专家分:13384
注 册:2013-3-3
收藏
得分:5 
书上都有原算法,你看看

Maybe
2013-03-31 21:24
yctchxf
Rank: 6Rank: 6
来 自:盐城
等 级:侠之大者
威 望:2
帖 子:176
专家分:454
注 册:2012-4-10
收藏
得分:5 
#include<stdio.h>
#include<stdlib.h>
typedef struct student{
     int data;
     long number;
     char name;
     char sex;
     int age;
     struct student *next;
 }LIST;


 LIST *CreatLList(int n)
 {      
     LIST *head,*p,*q;
     int i;
     if(n>0)
     {
         head=(LIST *)malloc(sizeof(LIST));
         printf("请输入第一个学生信息:\n");
         scanf("%ld%d%s%s%d",&head->data,&head->number,&head->name,&head->sex,&head->age);
         printf("学号:%d,姓名:%s,性别:%s,年龄:%d\n", head->number,head->name,head->sex,head->age);
         p=head;
         for(i=2;i<=n;i++)
         {
             q=(LIST *)malloc(sizeof(LIST));
             q->next=NULL;//初始状态 q->next 应该为空。
             printf("请输入第%d个学生信息:\n",i);
             scanf("%ld%s%s%d",&q->number,&q->name,&q->sex,&q->age);
             printf("学号:%d,姓名:%s,性别:%s,年龄:%d\n", q->number,q->name,q->sex,q->age);
             p->next=q;p=q;
         }
        
     }
 
     else
     head=NULL;
     return head;
 }
 
void OutputLList(LIST *p)
 {
         
     printf("\n输出信息:\n");
     while(p){
         printf("学号:%d,姓名:%s,性别:%s,年龄:%d\n",
             p->number,p->name,p->sex,p->age);
         p=p->next;
     }
 }
 
LIST *InsertLList(LIST *p, LIST *newnode,int iPos)
{  //函数参数:p为单链表头指针 newnode指向要插入的新节点 iPos为插入位置,将新节点插在第iPos个位置,
    //若表中无第iPos个结点,则给出相应提示信息。头结点位置为0。 返回值: 单链表头指针
    LIST *q=p,*m=NULL;
    int i=0;
    for(i=0;i<iPos-1&&q!=NULL;i++)
    { //查找第iPos前面一个节点。
        q=q->next;
    }
    if(q!=NULL)
    {// 表中有 iPos 个节点。
        m=(LIST *)malloc(sizeof(LIST));
        *m=*newnode;
        m->next=q->next;
        q->next=m;
        return p;
    }
    else
    {// 表中没有第 iPos 个节点。
        printf("表中没有第 iPos 个节点。");
        return NULL;
    }
   
   
   
}
 LIST *DeleteLList(LIST *p,int iPos)
 {//函数参数:p为单链表头指针 iPos 为指定位置,若表中无第iPos结点,
     //则给出相应提示信息。头结点位置为0。 返回值: 单链表头指针
     LIST *q=p;
    int i=0;
    for(i=0;i<iPos-1&&q!=NULL;i++)
    { //查找第iPos前面一个节点。
        p=p->next;
    }
    if(q!=NULL)
    {// 表中有 iPos 个节点。
        q->next=q->next->next;//删除点 iPos 个节点。
        return p;
    }
    else
    {// 表中没有第 iPos 个节点。
        printf("表中没有第 iPos 个节点。");
        return NULL;
    }
}
 
int FindLList(LIST *p,int age)
{//函数参数:p为单链表头指针 age 为查找条件 返回值:
    //查找成功,返回该结点所在位置信息,头结点位置为0。 查找失败,返回-1。
     LIST *q;
     int local;
     for(q=p;q->next!=NULL; q=q->next)
     {
        if(q->age==age) local=q->data;//data 是否为位子信息?     
     }
     if(q->next==NULL) return -1;
     else return local;
}

void main()
{
 LIST *List;
 int n=0;
 printf("输入初始状态学生人数:\n");
 scanf("%d",&n);
 List=CreatLList(n);



}
来不及调试了,反正你的代码就有问题。还有我不明白,为啥你非得在 LIST *CreatLList(int n) 中同是输入很多学生的信息啊?
我建议你把 构造链表和插入分开来考虑。不要把这些搀和在一起。会很凌乱的……改天上课我把自己写的东西再发给你。
2013-03-31 22:40
快速回复:单链表问题中插入,删除问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.015248 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved