| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1077 人关注过本帖
标题:简单链表问题
只看楼主 加入收藏
ClearningC
Rank: 2
等 级:论坛游民
帖 子:98
专家分:43
注 册:2016-10-26
结帖率:89.47%
收藏
已结贴  问题点数:1 回复次数:6 
简单链表问题
Description
链表是使用指针把一系列结点连接起来的数据结构。它由一系列结点(链表中每一个元素称为结点)组成,结点可以在运行时动态生成。每个结点包括两个部分:一个是存储数据元素的数据域,另一个是存储下一个结点地址的指针域。每个结点里的指针指向下一个结点,而链表的入口(head指针)指向第一个结点
现在请你实现一个简单的链表,具体请看头文件
Input
第一行是整数 n
接下来有n行,每行两个整数,代表插入的位置和插入的数据
Output
输入每次插入后的结果
Sample Input
3
0 1
1 2
-1 3
Sample Output
1
1 2
position is not valid
搜索更多相关主题的帖子: 动态 元素 
2016-12-18 22:26
九转星河
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:长长久久
等 级:贵宾
威 望:52
帖 子:5023
专家分:14003
注 册:2016-10-22
收藏
得分:1 
链表里面的源数据没有给出来~看头文件理解不了~怎么做

[code]/*~个性签名:bug是什么意思?bug是看上去没有可能的东西实际上是有可能做到的 就是这样~2018-08-08更~*/[/code]
2016-12-18 22:46
ClearningC
Rank: 2
等 级:论坛游民
帖 子:98
专家分:43
注 册:2016-10-26
收藏
得分:0 
回复 2楼 九转星河
哦哦,对不起啊,忘记给了。
2016-12-18 23:44
ClearningC
Rank: 2
等 级:论坛游民
帖 子:98
专家分:43
注 册:2016-10-26
收藏
得分:0 
main 函数
程序代码:
#include <stdio.h>
#include "linkedList.h"

void print() {
    int i;
    for (i = 0; i < size; ++i) {
        printf("%d ", get(i));
    }
    printf("\n");
}


int main() {
    head = NULL;
    size = 0;
    int n, i, position, value;
    scanf("%d", &n);
    for (i = 0; i < n; i++) {
        scanf("%d%d", &position, &value);
        if (insert(position, value)) {
            print();
        } else {
            printf("position is not valid\n");
        }
    }
    clear();
    return 0;
}

头文件
程序代码:
#ifndef LINKEDLIST_H
#define LINKEDLIST_H
#include <stdbool.h>

typedef struct node {
    int value;
    struct node* next;
} node ;

int size; // the size of linked list
node* head; // the head of linkedlist

//insert the value to the right position
//if the position is not valid, return false
//if insert successfully, return true
bool insert(int position, int value);

// return the value in the given position
int get(int position);

//clear the linkedlist, remember to free the memory you allocated
void clear();

#endif
2016-12-18 23:45
九转星河
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:长长久久
等 级:贵宾
威 望:52
帖 子:5023
专家分:14003
注 册:2016-10-22
收藏
得分:0 
回复 4楼 ClearningC
这应该是考试题吧,我暂时还没理解要求,看上去不好理解我先放放,有时间再来处理~叫其他人来帮忙看一下

[code]/*~个性签名:bug是什么意思?bug是看上去没有可能的东西实际上是有可能做到的 就是这样~2018-08-08更~*/[/code]
2016-12-19 01:16
ClearningC
Rank: 2
等 级:论坛游民
帖 子:98
专家分:43
注 册:2016-10-26
收藏
得分:0 
这是那几个函数的代码
程序代码:
#include "linkedList.h"
#include<stdlib.h> 
node *prev,*current;
bool insert(int position, int value)
{
    

    if(position<0||position>size)
        return false;
    else
    {
        current=( struct node*)malloc(sizeof( struct node));
        current->value=value;
        if(position==0)
        {
            current->next=head;
            head=current;
            size++;
            return true;
        }
        if(position<=size)
        {
            prev=head;
            for(int i=1;i<position;i++)
            {
                prev=prev->next;       
            }
            current->next=prev->next;
            prev->next=current;
            size++;
            return true;
        }
    }
}
// return the value in the given position
int get(int position)
{
    node *prev=head;
    for(int j=0;j<position;j++)
    {
        prev=prev->next;
    }
    return prev->value;
}
//clear the linkedlist, remember to free the memory you allocated
void clear()
{
    current=head;
    while(head!=NULL)
    {
        current=head;
        head=current->next;
        free(current);
    }
}
2016-12-19 23:58
marlow
Rank: 6Rank: 6
等 级:侠之大者
威 望:2
帖 子:125
专家分:419
注 册:2016-7-18
收藏
得分:1 
你要别人解决什么问题呢?手机上看代码,知道问题有助于很快解决

一切都在学习、尝试、摸索中
2016-12-20 07:47
快速回复:简单链表问题
数据加载中...
 
   



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

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