-1

我在 c 中创建一个双向链表。我想打印出我的上一个和下一个值来检查我的双向链表是否正确。

#include <stdio.h>
#include "dubblelist.h"
#include <string.h>
#include <stdlib.h>

void add(int number);
void print();
void add_to(int number);
void append(int number);
void main(){

int i = 1;

  add(1);
  add(2);
  add(3);
  print();


}
void add_to(int number){
    struct node *head;
    head=LINK;
    head = (struct node *)malloc(sizeof(struct node));
    head->d = number;
    head->prev = NULL;
    head->next = NULL;

    if(LINK == NULL)
    {
        LINK = head;
        head -> next = NULL;
        head -> prev = NULL;
    }else{
        //head -> next = LINK;
        //head -> prev = LINK;
        //LINK = head;
    }


}
//add other values
void append(int number){
    struct node *kop, *right;

    printf("add_new_value\n");
    kop = (struct node *)malloc(sizeof(struct node));
    kop -> d = number;
    right = LINK; 

    while(right->next != NULL)
        right=right->next;

        right->next =kop;
        right=kop;
        right->next=NULL;
        right->prev=NULL;


}
//add first value 
void add(int number){
    struct node *head;

    head=LINK;
    if(head==NULL){
    printf("List is leeg!\n");
    add_to(number);
    }else{
    printf("List bevat item\n");
    append(number);
    }
}

void print(){
    printf("----------------------PRINT--------------------------\n");
    struct node *n;
    int *p;

    while(n!=NULL){
    //next_value = n->next;
    printf("%d",n->d);


    //printf("%d",n->next);
    //printf("%d",n->next);
    //printf("||\n ");
    n = n->next;
    }
}

.h 文件

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

typedef int DATA;
struct node{
    DATA d;
    struct node *prev;
    struct node *next;
} *LINK;
4

1 回答 1

1
void printNode(struct *node){
    if (node) {
       int pp = node->prev ? node->prev->d : -1;
       int nn = node->next ? node->next->d : -1;
       printf("[p:%d] [c:%d] [n:%d]", pp, node->d, nn);
    } else {
       printf("(null)");
    }
}

void print(struct node *head){
    printf("----------------------PRINT--------------------------\n");
    struct node *n = head;
    while(n!=NULL){
      printNode(n);
    }
}
于 2013-03-17T21:50:53.337 回答