如何建立双向链表?急
我的程序如下,很明显运行不起来:请大家帮帮忙
- C/C++ code
1 #include "apue.h" 2 #include "malloc.h" typedef struct DuLNode{ 6 int data; 7 struct DuLNode *prior; 8 struct DuLNode *next; 9 }DuLNode,*DuLink; 10 11 DuLNode *create(DuLNode *h) 12 { 13 DuLNode *c,*r; 14 r=h; 15 int i,n; 16 printf("input the number of node:"); 17 scanf("%d",&n); 18 for(i=0;i<n;i++) 19 { 20 c=(DuLNode *)malloc(sizeof(DuLNode)); 21 printf("input the value of node:"); 22 scanf("%d",&c->data); 23 r->next = c; 24 c->prior = r; 25 r = c; 26 } 27 r->next = NULL; 28 /*h->prior = r; 29 h = r;*/ 30 return h; 31 } 51 void 52 output(DuLNode *h) 53 { 54 DuLNode *l; 55 l = h->next; 56 while(!l) 57 { 58 printf("%d",l->data); 59 l = l->next; 60 } 61 } 62 63 void 64 main(void) 65 { 66 DuLNode *p; 67 create(p); 68 output(p); 69 70 }
[解决办法]
我感觉你的方法没啥大的问题
只是头结点处理的好像不怎么妥当
修改了下,可以运行
- C/C++ code
typedef struct DuLNode{ int data; struct DuLNode *prior; struct DuLNode *next;}DuLNode,*DuLink;DuLNode *create(DuLNode *h){ DuLNode *c,*r; r=h; int i,n; printf("input the number of node:"); scanf("%d",&n); for(i=0;i<n;i++) { c=(DuLNode *)malloc(sizeof(DuLNode)); printf("input the value of node:"); scanf("%d",&c->data); r->next = c; c->prior = r; r = c; } r->next = NULL; /*h->prior = r; h = r;*/ return h;}voidoutput(DuLNode *h){ DuLNode *l; l = h->next; while(l) //判断条件错误! { printf("%d",l->data); l = l->next; }}voidmain(void){ DuLNode *p = new DuLNode; create(p); output(p);}
[解决办法]
- C/C++ code
#include <stdio.h>
#include <stdlib.h>
typedef struct DuLNode{
int data;
struct DuLNode *prior;
struct DuLNode *next;
}DuLNode,*DuLink;
DuLNode *create(DuLNode *h)
{
DuLNode *c,*r;
int i, n;
r = h;
printf("input the number of node:");
scanf("%d", &n);
r->data = n; /*头结点保存链表元素个数*/
for(i=0; i <n; i++)
{
c=(DuLNode *)malloc(sizeof(DuLNode));
printf("input the value of node:");
scanf("%d", &(c->data));
/*先勾走新建节点的前后链接指针*/
c->prior = r;
c->next = NULL;
/*将新建节点添加到链表尾*/
r->next = c;
/*链表尾指针后移*/
r = c;
}
return h;
}
void output(DuLNode *h)
{
DuLNode *l;
l = h->next;
while(l != NULL)
{
printf("%d",l->data);
l = l->next;
}
}
int main(void)
{
DuLNode *p = (DuLNode *)malloc(sizeof(DuLNode)); /*为链表头分配空间*/
create(p);
output(p);
free(p);
return 0;
}
[解决办法]
[解决办法]
你用头指针的话。要用二级指针才行
因为你要修改的是指针本身,而不只指向的内容
[解决办法]
有个错误,改下
- C/C++ code
for (i = 0; i < iNum; i++) { printf("\nInput the value of node:"); scanf("%d", &(pTmp[i]->iData)); pHead->next = pTmp[i]; pTmp[i]->prev = pHead; pHead = pTmp[i]; }
[解决办法]
建议用头节点。这样处理起来比较方便
[解决办法]
晕,如楼上所言,再改下,改回CreateNode
- C/C++ code
pNODE CreateNode(const pNODE * prev, const int iNum){ int i; int iTmp; pNODE pTmp; if (!prev || iNum <= 0) return NULL; pTmp = new NODE [iNum];// allocate all memory in one time if (!pTmp) return NULL; memset(pTmp, NULL, sizeof(NODE) * iNum); for (i = 0; i < iNum; i++) { printf("\nInput the value of node:"); scanf("%d", &(pTmp[i]->iData)); if (i >= 1) { pTmp[i - 1]->next = pTmp[i]; pTmp[i]->prev = pTmp[i - 1]; } } *prev = pTmp; return *prev;// return list head}
[解决办法]
这个有什么问题?
[解决办法]
VC调试时按Alt+8,TC或BC用TD调试,打开汇编窗口看每句C对应的汇编不就啥都明白了吗。
想要从本质上理解C指针,必须学习汇编以及C和汇编的对应关系。
从汇编的角度理解和学习C语言的指针,原本看似复杂的东西就会变得非常简单!
[解决办法]
楼主运行出来了没有?那个删除的程序好像不对,那个if可不是循环语句啊,只是一个判断语句
[解决办法]
产生段错误就是访问了错误的内存段,一般是你没有权限,或者根本就不存在对应的物理内存,尤其常见的是访问0地址.
原因是一般是错误地使用指针引起的
1)访问系统数据区,尤其是往 系统保护的内存地址写数据
最常见就是给一个指针以0地址
2)内存越界(数组越界,变量类型不一致等) 访问到不属于你的内存区域
加上“-g -rdynamic"的参数进行编译,然后用gdb调试运行这个新编译的程序,应该可以找到出现段错误的位置
[解决办法]
- C/C++ code
/* t02.c: 测试双向链表。*/#include <glib.h>// my_Node: 定义链表的元素。typedef struct { int data;} my_Node;void output(gpointer data, gpointer user_data) {// output(): 输出链表的元素。 my_Node* a; a = (my_Node*)data; g_print("%d ", a->data);}int main(int argc, char** argv) { GList *l, *l_p; my_Node* p; int i, n=10; // 初始化。 l = NULL; g_random_set_seed(time(NULL)); // 增加数据到链表中。 for (i=0; i < n; i++) { p = g_malloc(sizeof(my_Node)); p->data = g_random_int(); l = g_list_prepend(l, p); } // 遍历双向链表,输出链表的元素。 g_print("1. Doubly-Linked Lists:\n\t"); g_list_foreach(l, output, NULL); g_print("\n"); // 删除第2个元素。 g_print("2. Remove an element from a GList.\n"); l_p = l; for (i=0; i < 2; i++) l_p = l_p->next; l = g_list_remove(l, l_p->data); // 遍历双向链表,输出链表的元素。 g_print("3. Doubly-Linked Lists:\n\t"); g_list_foreach(l, output, NULL); g_print("\n"); // 释放链表。 g_list_free(l); l = NULL; return 0;}
[解决办法]
t02的运行结果:
- Perl code
wy@debian:~/src$ ./t021. Doubly-Linked Lists: -1731640906 -1714261248 -1526875896 1937269189 -1384980456 1191456902 -613615506 -743665714 -2027555027 -1965918582 2. Remove an element from a GList.3. Doubly-Linked Lists: -1731640906 -1714261248 1937269189 -1384980456 1191456902 -613615506 -743665714 -2027555027 -1965918582 wy@debian:~/src$
[解决办法]
看GLib库的glib.c文件。里面有双向链表的实现。