| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 930 人关注过本帖, 1 人收藏
标题:自己写了一个链表的小程序,好多错,帮我改改
只看楼主 加入收藏
jjg
Rank: 2
等 级:论坛游民
帖 子:67
专家分:42
注 册:2009-8-19
结帖率:46.15%
收藏(1)
已结贴  问题点数:20 回复次数:7 
自己写了一个链表的小程序,好多错,帮我改改
#include <iostream>
using namespace std;
struct node
{
    int data;
    node *next;
};

int init()
{
    node * head;
    head=NULL;
}
int insert(node a)
{

    while(p->data!=0)
    {
        p=new node;
        cout>>"please enter some node:";cin>>a;
        head=p;
        p->data=a.data;
        p=p->next;
    }
    return head;
}
void find(node b)
{
    cout<<"please enter b:"<<endl;cin>>b;
    while(p!=NULL)
    {
        if(b.data==p->data)
        {
            cout<<"have find node b"<<endl;
            break;
        }
        else
            p=p->next;
    }
}
void main()
{
    node *p;

    init();
    insert(a);
    find(b);
}








搜索更多相关主题的帖子: 链表 改改 
2009-10-16 17:21
jjg
Rank: 2
等 级:论坛游民
帖 子:67
专家分:42
注 册:2009-8-19
收藏
得分:0 
难道没有人知道
2009-10-16 21:05
xxfeng11
Rank: 1
等 级:新手上路
帖 子:43
专家分:5
注 册:2009-5-4
收藏
得分:2 
类型要用typedef  
int init()
{
    node * head;
    head=NULL;
}
int insert(node a)
这里的a要用 *&a 或 *a
  下面的没看  自己注意细节
2009-10-16 22:40
xufan
Rank: 8Rank: 8
等 级:蝙蝠侠
威 望:6
帖 子:232
专家分:804
注 册:2008-10-20
收藏
得分:2 
这个程序请LZ告诉我它的意图,即它的目的。
对于你们的语法错误:
除主函数外,p没有被定义就被引用。(我不知道结构体p是用来干什么的)

~~~~~~我的明天我知道~~~。
2009-10-17 11:58
xiehaishui
Rank: 1
等 级:新手上路
威 望:1
帖 子:9
专家分:3
注 册:2009-7-2
收藏
得分:2 
从你写的程序来看,你的C++基本功好差,就连基本的函数传递参数都错了,在main函数中,你的a,b参数的类型都没声明,还有就是你的c++版的数据结一点都不规范,既然是C++,就应该把函数放在类中,你应该设计类,而不是单纯的建立一个struct,你应该多体会面向对象的思想,而不是凑些c语言的东西就组成了c++.把#include<stdio.h>改为#include<iostream.h>,那样跟c有区别么。说的严厉了一点,别生气,但是学习就是应该严谨,要规范些,这样你的编程能力才能提高。
2009-10-17 17:05
jjg
Rank: 2
等 级:论坛游民
帖 子:67
专家分:42
注 册:2009-8-19
收藏
得分:0 
回复 5楼 xiehaishui
说的好,这就是我平时不写代码的后果
2009-10-17 19:13
mfh
Rank: 6Rank: 6
等 级:侠之大者
帖 子:179
专家分:411
注 册:2009-5-31
收藏
得分:2 
#include <stdio.h>
#include<malloc.h>
#include<stdlib.h>
#include<string.h>
#define N 100
 
typedef struct node
{
    char data;
    node *next;
}Node;
 
int Insert(Node *Head)
{
    char a[N];
    unsigned int i;
    int length;
    Node *temp;
 
     printf("输入一串元素:");
     gets(a);
    for(i=0;i<(length=strlen(a));i++)
    {
        if((temp=(Node *)malloc(sizeof(Node)))==NULL)
            exit(0);
        temp->data=a[i];
        temp->next=NULL;
        Head->next=temp;
        Head=temp;
    }
     
    return 1;
}
  
int Find(Node *Head)
{
 
    char a;
    Node *temp;
 
    temp=Head->next;
    printf("请输入你想要查找的元素:");
    scanf("%c",&a);
    while(temp!=NULL)
    {
        if(temp->data==a)
        {
            printf("找到你所查找的元素:%c\n",temp->data);
            return 1;
        }
        temp=temp->next;
 
    }
    printf("没有你想查找的元素!!\n");
    return 0;
}
void main()
{
    Node *Head;
    if((Head=(struct node *)malloc(sizeof(struct node)))==NULL)
        exit(0);
    printf("初始化成功\n");
    Insert(Head);                        //插入元素
    Find(Head);
}


同志下回书代码时将要求说清楚一点,还有你写的代码根本就不是代码,下次加油!!!!!!!!!
2009-10-17 22:55
y2k_connect
Rank: 2
等 级:论坛游民
威 望:1
帖 子:15
专家分:61
注 册:2009-10-4
收藏
得分:2 
回复 楼主 jjg
linux c语言版:

#include <stdio.h>
#include <stdlib.h>

#define MaxLen        128

typedef struct node {
    int    data;
    node*    next;
} *Node;

int init(Node *head) {
// 初始化.
    *head = malloc(sizeof(struct node));
    if (!head)
        return -1;
    // 链表元素总量.
    (*head)->date = 0;
    // 链表的第1个元素地址.
    (*head)->next = 0;

    return 0;
}

int insert(Node head, int x) {
// 在链表头插入元素.
    Node p;
   
    p = malloc(sizeof(struct node));
    if (!p)
        return -1;
    p->date = x;
    p->next = head->next;
    head->next = p;
    head->date ++;

    return 0;
}

Node find(Node head, int x) {
// 在链表中查找x.
    int i, flag;

    for (i=0, flag=0, p=head->next; i < head->date; i++, p=p->next)
        if (p->date == x) {
            flag = 1;
            break;
        }
    if (flag)
        // 找到, 返回链表元素的地址.
        return p;

    // 未找到, 返回0.
    return 0;
}

void deinit(Node *head) {
// 关闭链表.
    int i, n;
    Node p, q;

    n = (*head)->date;
    for (i=0, p=(*head)->next; i < n; i++) {
        q = p;
        p = q->next;
        free(q);
    }
    p = *head;
    free(p);
    *head = 0;
}

int input_key(void) {
    int i, x;
    char arr[128];
   
    for (i=0; i < MaxLen; i++) [
        arr[i] = getchar();
        if (arr[i] == '\n')
            break;
    }
    x = atoi(arr);

    return x;
}

int main() {
    Node list, p;
    int x;

    // 建立链表.
    x = init(&list);
    if (!x)
        return -1;
    // 插入元素.
    x = insert(list, input_key());
    if (!x) {
        deinit(list);
        return -1;
    }
    x = insert(list, input_key());
    if (!x) {
        deinit(list);
        return -1;
    }
    x = insert(list, input_key());
    if (!x) {
        deinit(list);
        return -1;
    }
    x = insert(list, 234);
    if (!x) {
        deinit(list);
        return -1;
    }
    x = insert(list, 435);
    if (!x) {
        deinit(list);
        return -1;
    }
    x = insert(list, input_key());
    if (!x) {
        deinit(list);
        return -1;
    }
    // 查找.
    x = 1;
    p = find(x);
    if (!x)
        printf("没有找到%d\n", x);
    else
        printf("p=%x, date=%d\n", p, p->date);
    // 关闭链表.
    deinit(list);
   
    return 0;
}



[ 本帖最后由 y2k_connect 于 2009-10-19 16:46 编辑 ]
2009-10-19 16:31
快速回复:自己写了一个链表的小程序,好多错,帮我改改
数据加载中...
 
   



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

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