| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 611 人关注过本帖
标题:关于链表的游标实现
只看楼主 加入收藏
Maps
Rank: 2
等 级:论坛游民
帖 子:16
专家分:10
注 册:2015-3-8
结帖率:66.67%
收藏
已结贴  问题点数:30 回复次数:3 
关于链表的游标实现
#include"stdio.h"

#define CursorSpace 100
typedef int pertonode;
typedef pertonode Position;
typedef pertonode List;

struct Node{
  int  X;
  Position Next;
};

struct Node CursorList[CursorSpace];//定义数组

void InitializeCursorList(void){//初始化数组
  int i = 0;
  for (i = 0; i < CursorSpace; i++) {
    CursorList[i].Next = i + 1;
  }
  CursorList[CursorSpace - 1].Next = 0;
}

static Position CursorAlloc(){  
  Position P;
  P = CursorList[0].Next;//这个每次调用的时候不都是把p=1了吗?
  printf("%d\n",P); //当插入调用此函数的时候为什么 输出的p是 1 2 3 4 5?
  CursorList[0].Next = CursorList[P].Next;
  CursorList[P].Next = 0;
  return P;
}
void Insert(List L, Position P, int  X){   //插入
  Position tmp;
  tmp = CursorAlloc();
  CursorList[tmp].X = X;
  CursorList[tmp].Next = CursorList[P].Next;
  CursorList[P].Next = tmp;
}
void Print(List L){    输出数组
  Position P = CursorList[L].Next;
  while (P != 0) {
    printf("%d ",CursorList[P].X);
    P = CursorList[P].Next;
  }

  printf("\n");
}


int main()
{

  printf("start ...\n");
  InitializeCursorList();
  List L = CursorAlloc();
  Insert(L, L, 1);
  Insert(L, L, 3);
  Insert(L, L, 5);
  Insert(L, L, 4);
  Print(L);
  return 0;
}
  输出的结果为什么是  1 2 3 4 5
                     4 5 3 1 ??
新手求解答啊。



[ 本帖最后由 Maps 于 2015-3-22 22:43 编辑 ]
搜索更多相关主题的帖子: include 
2015-03-22 19:50
longwu9t
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:小飞侠
威 望:6
帖 子:732
专家分:2468
注 册:2014-10-9
收藏
得分:20 
这样造轮子 要到几时……………………

程序代码:
#include <glib.h>

int main(int argc, char** argv) {
    int i;
    GSList *list = g_slist_append(NULL, "ABC"), *p;
    list = g_slist_append(list, "124");
    list = g_slist_append(list, "xyz");
    g_print("Here's the list:\n");

    for(i = 0, p = list; p != NULL; i++) {
        g_print("The current item is '%s'\n",
                (char*)g_slist_nth(list, i)->data);
        p = p->next;
    }

    g_slist_free(list);
    return 0;
}

Only the Code Tells the Truth             K.I.S.S
2015-03-22 21:29
Maps
Rank: 2
等 级:论坛游民
帖 子:16
专家分:10
注 册:2015-3-8
收藏
得分:0 
回复 2楼 longwu9t
呃。。这样确实麻烦了点。。主要是想明白为什么那样可以输出。。

看到了编程大海的一角,我还在努力的寻找方向,
2015-03-22 22:41
longwu9t
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:小飞侠
威 望:6
帖 子:732
专家分:2468
注 册:2014-10-9
收藏
得分:10 
程序代码:
#include <stdio.h>
#include <glib.h>

int cmp(const gpointer *a, const gpointer *b) {
    return GPOINTER_TO_INT(a) - GPOINTER_TO_INT(b);
}


int main(int argc, char** argv) {
    int i, tmp, sum = 0, idx = -1;
    GSList *list = NULL, *p = NULL;

    for(;;) {
        if(scanf("%d", &tmp) != 1) break; //非法输入退出初始化

        list = g_slist_append(list, GINT_TO_POINTER(tmp)); //初始化链表
    }

    for(i = 0, p = list; p != NULL;) {
        tmp = GPOINTER_TO_INT(g_slist_nth_data(list, i++)); //按位置返回数据
        g_print("The NO.%d item is %d\n", i, tmp);
        sum += tmp;
        p = p->next;
    }

    g_print("SUM = %d\n", sum);
    g_print("COUNT = %d\n\n", g_slist_length(list)); //计算链表长度

    list = g_slist_sort(list, (GCompareFunc)cmp); //排序(升序)

    for(i = 0, p = list; p != NULL;) {
        tmp = GPOINTER_TO_INT(g_slist_nth_data(list, i++));
        g_print("The NO.%d item is %d\n", i, tmp);
        p = p->next;
    }

    puts("");

    list = g_slist_reverse(list); //反转

    for(i = 0, p = list; p != NULL;) {
        tmp = GPOINTER_TO_INT(g_slist_nth_data(list, i++));
        g_print("The NO.%d item is %d\n", i, tmp);
        p = p->next;
    }

    puts("Enter a number to find...");

    while(scanf("%d", &tmp) != 1) while(getchar() != '\n');
    
    idx = g_slist_index(list,  GINT_TO_POINTER(tmp)); //查找数据返回位置

    if(-1 == idx) puts("find nothing...");

    else g_print("index = %d\n", idx);


    g_slist_free(list); //释放
    return 0;
}

Only the Code Tells the Truth             K.I.S.S
2015-03-23 14:51
快速回复:关于链表的游标实现
数据加载中...
 
   



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

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