注册 登录
编程论坛 C++教室

c++数据结构链表 这我自己写了两个函数 哪里错啦?

於晓琪123 发布于 2015-04-06 12:23, 692 次点击
#include "stdafx.h"
#include <iostream>
using namespace std;
struct Student
{
    int data;
    Student *next;
};
void CreateList(Student *&head)
{
    char c;
    Student *p=new Student;
    p = head;
    cout << "输入字符" << endl;
    while ((c=getchar())!='s')
    {
        p->data = c;
        p = p->next;
    }
    p = NULL;
}
void Show(Student *&head)
{
    Student *p = new Student;
    p = head;
    while (p)
    {
        cout << p->data << " ";
        p = p->next;
    }
}


int _tmain(int argc, _TCHAR* argv[])
{
    Student *head;
    CreateList(head);
    Show(head);
    return 0;
}
9 回复
#2
於晓琪1232015-04-06 12:24
求解答啊  运行不出来
#3
npu_wenbing2015-04-06 13:08
#include "stdafx.h"
#include <iostream>
using namespace std;
struct Student
{
    char data;
    Student *next;
};
void CreateList(Student *head)
{
    char c;
    Student *p2;
  Student *p;
    p = p2=head;
    cout << "输入字符"<<endl;
   
    while ((c=getchar())!='s')
    {   
    p2->next=p;
   
       p2=p;
        p=new Student;
         p->data = c;
   
    }
    p->next=NULL;
   
}
void Show(Student *head)
{
   Student *p;
    p = head;
    while (p)
    {
        cout << p->data << " ";
        p = p->next;
    }
}


int main(int argc, char* argv)
{
    Student *head=new Student;
    CreateList(head);
    Show(head);
    return 0;
}
#4
npu_wenbing2015-04-06 13:08
已编译通过
#5
npu_wenbing2015-04-06 13:11
楼主是新手吧。。。。结构体指针用的是眼花缭乱
还有数据类型。。。
再是main函数参数。。。
不太用C++,不知道改的可符合楼主的意?
#6
於晓琪1232015-04-06 16:17
回复 5楼 npu_wenbing
因为是用VS写的 main
#7
於晓琪1232015-04-06 16:19
回复 3楼 npu_wenbing
好的 谢谢
#8
yangfrancis2015-04-07 13:07
while ((c=getchar())!='s')
    {
        p->data = c;
        p = p->next;
    }
问题出在这里。不能先找到下一节点,再为它输入数据。只能先开一个内存空间,为其输入数据之后,再将该地址往链表上续。
#9
於晓琪1232015-04-15 08:53
回复 8楼 yangfrancis
正解
1