| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1532 人关注过本帖
标题:一个链表用插入多个结点,怎么从键盘输入啊,有点混了
只看楼主 加入收藏
wll19901105
Rank: 1
等 级:新手上路
帖 子:27
专家分:0
注 册:2012-12-4
结帖率:100%
收藏
已结贴  问题点数:8 回复次数:4 
一个链表用插入多个结点,怎么从键盘输入啊,有点混了
#include <stdio.h>
#include <stdlib.h>
 
struct student
{
        int age;
        char name[10];
        struct student *next;
};
struct student *add(struct student *head)
{
        struct student *p = malloc(sizeof(struct student));
        p->next = head;
        head = p;
        return head;
}
搜索更多相关主题的帖子: head include return 
2012-12-14 13:01
农民也疯狂
Rank: 2
来 自:伯纳乌
等 级:论坛游民
帖 子:48
专家分:56
注 册:2012-12-4
收藏
得分:3 
struct student *add(struct student *head, int age, char *name)
{
        struct student *p = (struct student *)malloc(sizeof(struct student));
        struct student *temp;
        temp = head;
        while(temp->next != NULL)
        {
            temp++;
        }
        p->age = age;
        p->name = name;
        p->next = NULL;
        temp->next = p;
        return head;
}

屌丝的世界不容懈怠
2012-12-14 16:25
crystall
Rank: 8Rank: 8
等 级:蝙蝠侠
威 望:7
帖 子:184
专家分:809
注 册:2012-12-1
收藏
得分:3 
回复 2楼 农民也疯狂
程序代码:
struct student *add(struct student *head, int age, char *name)
{
        struct student *p = (struct student *)malloc(sizeof(struct student));
        struct student *temp;
        temp = head;
        while(temp->next != NULL)
        {
            temp++;
        }

        p->age = age;
        //p->name = name; 不可以这样赋值
        strcpy(p->name,name);
        p->next = NULL;
        temp->next = p;

        return head;
}
2012-12-14 17:50
冰冻零点
Rank: 3Rank: 3
来 自:西安电子科技大学
等 级:论坛游侠
帖 子:81
专家分:136
注 册:2012-9-18
收藏
得分:3 
while(temp->next != NULL)

        {

            //temp++;
temp=temp->next

        }

好好学习,天天向上
2012-12-14 18:19
crystall
Rank: 8Rank: 8
等 级:蝙蝠侠
威 望:7
帖 子:184
专家分:809
注 册:2012-12-1
收藏
得分:0 
程序代码:
#include"stdafx.h"
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#include<string.h>

struct CNode
{
    CNode()
    {
        nAge = 0;
        pNext = NULL;
        memset(szName, 0, sizeof(szName));
    }
   
    CNode(int nage, char szname[], CNode* pnext)
    {
        nAge = nage;
        strcpy(szName, szname);
        pNext = pnext;
    }
   
    int  nAge;
    char szName[32];
    CNode *pNext;
};

struct student
{
    CNode* pHead;    //
    CNode* pTail;    //
    int   nCount;    //学生个数
};

//初始化
void Init(student& theStud)
{
    theStud.nCount = 0;
    theStud.pHead = theStud.pTail = NULL;
}

//添加学生
void MyAdd(student& theStud)
{
    CNode theNode;
    printf("请输入学生年龄: ");
    fflush(stdin);
    scanf("%d", &theNode.nAge);

    printf("请输入学生姓名: ");
    fflush(stdin);
    scanf("%31s", &theNode.szName[0]);

    printf("\r\n");

    CNode *pNewNode = (CNode *)malloc(sizeof(theNode));
    if(pNewNode == NULL)
    {
        return;
    }

    pNewNode->nAge = theNode.nAge;
    strcpy(pNewNode->szName, theNode.szName);

    //头结点为NULL,表示还没有添加数据
    if(theStud.pHead == NULL)
    {
        theStud.pHead = pNewNode;
        theStud.pHead->pNext = NULL;
    }
    else if(theStud.pTail != NULL)
    {
        pNewNode->pNext = NULL;
        theStud.pTail->pNext = pNewNode;
    }

    theStud.pTail = pNewNode;

    theStud.nCount++;
}

//显示刚刚输入的数据
void MyDisplay(student& theStud)
{
    CNode *pCurNode = NULL;
    pCurNode = theStud.pHead;

    while(pCurNode)
    {
        printf("学生年龄: %d\r\n", pCurNode->nAge);
        printf("学生姓名: %s\r\n", pCurNode->szName);
        printf("\r\n");

        pCurNode = pCurNode->pNext;
    }
}

//释放申请的空间
void MyRelease(student& theStud)
{
    CNode *pCurNode = theStud.pHead;
    CNode *pTempNode = NULL;
    while(pCurNode)
    {
        pTempNode = pCurNode->pNext;
        if(pCurNode)
        {
            delete pCurNode;
            pCurNode = pTempNode;
        }
    }
}


void main()
{
    student theStudent;
    CNode   theNode;

    //初始化
    Init(theStudent);

    //以下增加3个学生信息
    int i = 0;
    while(i < 2)
    {
        printf("增加第%d个学生信息: \r\n", i+1);

        MyAdd(theStudent);

        i++;
    }

    //显示刚刚输入的数据
    MyDisplay(theStudent);

    //释放申请的空间
    MyRelease(theStudent);
}
2012-12-14 18:22
快速回复:一个链表用插入多个结点,怎么从键盘输入啊,有点混了
数据加载中...
 
   



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

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