如何建立双向链表?急
我的程序如下,很明显运行不起来:请大家帮帮忙
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 }
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);}
[解决办法]
#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;
}
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
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}
[解决办法]
这个有什么问题?
/* 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的运行结果:
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文件。里面有双向链表的实现。