2016-03-31 21 views
1

Obwohl nicht Voraussetzung für meinen Kurs, ich versuche, Einzel- und Doppel-verknüpfte Liste zu implementieren, um sicherzustellen, dass ich die Konzepte verstehe. Ich habe die meisten Probleme ausgearbeitet, die ich denke, außer für eine, die ich versucht habe zu debuggen und printf meinen Weg aus. Obwohl ich verschiedene Beiträge gelesen habe, kann ich nicht herausfinden, warum sie nicht ähnlich arbeiten.Verknüpfte Liste in C

Eine meiner Insert-Funktion, wenn die while(h != NULL) kickt Ich muss das letzte Element noch einmal überprüfen, weil es es überspringen würde. Ich dachte über eine Do/While-Schleife nach, aber das würde nicht funktionieren.

Ich habe die exakt gleiche Schleife für die Suchfunktion und es findet das letzte Element in der Liste mit while(h != NULL) statt mich vor der Verarbeitung zu treten.

ist die Schleife, die von der Suchfunktion arbeitet,

while(h != NULL) //check list 
     if(h->val == s) 
      return true; 
     else 
      h = h->next; 

Hier wird die Schleife ist, die nicht aus der Insert-Funktion hat, wird diesen nicht den Endwert in der verknüpften Liste unter Verwendung von am Ende vor dem Beenden die Schleife. Wenn Sie den vollständigen Code einliest, musste ich zuerst überprüfen, ob if (t->next == NULL && t->val > n), um es den letzten Artikel zu suchen.

 while(t->next != NULL) //assigns it in the middle 
     { 
      if(t->val < n) 
       t = t->next; 
      else 
      { 
       node* tt = t->prev; 
       tt->next = a; 
       a->next = t; 
       a->prev = t->prev; 
       t->prev = a; 

       return h; //returns original 
      } 
     } 

Vielen Dank im Voraus und hoffentlich erkläre ich es. Schwierig wie ich versuche zu lernen, was ich nicht weiß.

#include <stdio.h> 
#include <stdlib.h> //for rand(); 
#include <stdbool.h> //bool function 

typedef struct _node 
{ 
    int val; 
    struct _node* next; 
    struct _node* prev; 
} node; 

node* create(int n, node* h); 

node* insert(int n, node* h); 

bool search(int s, node* h); 

void print(node* h); 

void del(node* h); 

int main() 
{ 
    node* h = NULL; 

    for(int i = 0; i < 15; i++) 
    { 
     int n = rand() % 15; 
     h = create(n, h); 
    } 

    print(h); 

    int s = 10; //number to search for 

    if(search(s, h)) 
     printf("Found Item\n"); 
    else 
     printf("Item not found\n"); 

    del(h); 
} 

bool search(int s, node* h) 
{ 
    if(h == NULL) 
     printf("No nodes created to search.\n"); 

    while(h != NULL) //check list 
     if(h->val == s) 
      return true; 
     else 
      h = h->next; 

    return false; 
} 


void del(node* h) 
{ 
    node* t = h; 

    while (h->next != NULL) 
    { 
     t = h->next; 
     h->next = NULL; 
     free(h); 
     h = t; 
    } 
     free(h); 
} 

node* insert(int n, node* h) 
{ 
    node* a = malloc(sizeof(node)); 
    node* t = h; 

    if(a == NULL) 
    { 
     printf("Failed to create new node\n"); 
     return NULL; 
    } 

    a->val = n; //sets new node to have value of n 

    if(h->val >= n) //assigns the integer to the beginning 
    { 
     a->next = t; 
     t->prev = a; 
     a->prev = NULL; 
     h = a; //assigns a new head since it appended the item to the beginning 
     return h; 
    } 

    while(t->next != NULL) //assigns it in the middle 
    { 
     if(t->val < n) 
      t = t->next; 
     else 
     { 
      node* tt = t->prev; 
      tt->next = a; 
      a->next = t; 
      a->prev = t->prev; 
      t->prev = a; 

      return h; //returns original 
     } 
    } 

    if (t->next == NULL && t->val > n) //check to see if second to last 
    {  
     node* tt = t->prev; 
     tt->next = a; 
     a->next = t; 
     a->prev = t->prev; 
     t->prev = a; 
    } 
    else 
    { 
     a->next = NULL; 
     a->prev = t; 
     t->next = a; 
    } 

    return h; 
} 

node* create(int n, node* h) 
{ 
    if(h == NULL) //create first node in the list 
    { 
     h = malloc(sizeof(node)); 
     if(h == NULL) 
     { 
      printf("Failed to create first node\n"); 
      return NULL; 
     } 
     h->val = n; 
     h->next = NULL; 
     h->prev = NULL; 
    } 
    else //create additional nodes in the list 
     h = insert(n, h); 

    return h; 
} 

void print(node* h) 
{ 
    if(h == NULL) 
     printf("List is empty\n"); 
    else 
     while(h != NULL) 
     { 
      printf("%i\n", h->val); 
      h = h->next; 
     } 
} 
+0

Was ist der CS50? –

+0

Es tut mir leid, nicht klar zu sein, es ist Harvard Online-Klasse durch edx.org können Sie kostenlos tun. Sie decken verknüpfte Listen und Doubles ab, müssen aber nicht als Auftrag eingereicht werden. – JDL

+0

"meine Einfügefunktion wenn die while (h! = NULL) startet". In Ihrer Funktion "Einfügen" gibt es keine solche Bedingung. Können Sie bitte klarstellen? – kaylum

Antwort

0

Wenn ich Ihre Frage richtig verstanden hat, fragen Sie diese Zeilen:

while(t->next != NULL) //assigns it in the middle 
{ 
    if(t->val < n) 
     t = t->next; 
    else 
    { 
     node* tt = t->prev; 
     tt->next = a; 
     a->next = t; 
     a->prev = t->prev; 
     t->prev = a; 

     return h; //returns original 
    } 
} 

if (t->next == NULL && t->val > n) //check to see if second to last 
{  
    node* tt = t->prev; 
    tt->next = a; 
    a->next = t; 
    a->prev = t->prev; 
    t->prev = a; 
} 
else 
{ 
    a->next = NULL; 
    a->prev = t; 
    t->next = a; 
} 

und die Frage lautet: „Wie der Codeblock Wiederholung aus dem Innern der Zeit nach der Weile zu vermeiden“

Vielleicht so etwas wie:

while(1) 
{ 
    if(t->val < n) 
    { 
     if (t->next == NULL) break; 
     t = t->next; 
    } 
    else 
    { 
     //assigns it in the middle 
     node* tt = t->prev; 
     tt->next = a; 
     a->next = t; 
     a->prev = t->prev; 
     t->prev = a; 

     return h; //returns original 
    } 
} 

// Add new node at the end 
a->next = NULL; 
a->prev = t; 
t->next = a;