0

やあ皆さん、二重リンクリストを作成するのはこれが初めてなので、ここで何をしているのかよくわかりません。コードを確認するのに助けが必要です。ありがとうございます。コメントを含めて行ったことです。ここで行った機能は、印刷、逆印刷、リンクされたリスト内の要素のカウント、およびこのノードが存在するかどうかを判断するための検索機能です。

 void printListfow() //print the list in forward manner
{
    CLR;
    struct node *tmpval; //declare a temp storage
    if(start==NULL) //if 1st node = null,then nth is inside,nth to print
    {
         printf("List is empty\n");
         return; 
    }
     tmpval=start; //assign the head/start to temp storage to retrieve data in 1st node
     printf("List of customer details: \n");
     while(tmpval!=NULL) //keep doing till it is NULL/the end
     {
         printf("%d ", tmpval->detail); //print the 'detail' which is in the node temp is pointing at
         tmpval=tmpval->next; //assign next node to the temp storage so that it can be printed again
     }

}


void printListrev() //print in reverse manner
{
      CLR;
      struct node *tmpval; //temp storage
      if(start==NULL) //
      {
          printf("List is empty\n");
          return;
      }
      tmpval=start; //assign start to tmpval to retrieve value
      printf("List of customer details: \n");
      tmpval=tmpval->prev //move backward and assign the data to tmpval
      printf("%d",tmpval->detail) //print it

}

void count() //count total number of records
{   struct node *x;
    x=start; //assign value of start to temp storage
    int ctr=0; //initialize counter
    while(x!=NULL) 
  {
    x=x->next; //keep going to next node and then increase the counter
    ctr++;
  }
  printf("Number of customer records are %d\n",ctr);
}



int getNode(node *tmp ,int cust) //when user wants to delete a customer ID and its details, this will search through the list,then if found,pass the value to another function for deletion
{
    tmp=tmp->cust; 
    while(tmp!=NULL)
    {
        if(tmp->detail == cust) //check if detail[ID stored] is same as requested[cust]
        {
            return 1;
        }tmp=tmp->next; //if not same,then move to next one
    }return 0;

}

ありがとう!

4

2 に答える 2

0

コンテキストでprintListrev():

これが循環二重リンク リストでない限り、最後の要素の前に最初の要素がある場合、start前の要素は NULL になります。したがって、次のように の前のフィールドにアクセスしても意味がありませstartん。

tmpval=start;
...
tmpval=tmpval->prev;

この目的のために、リストの末尾への別のポインターを保持できます。

その他の代替手段は次のとおりです。

再帰関数:

void printrev(struct node *s)
{
    if (s == NULL)
    return;
    printrev(s->next);
    printf("%d ", s->detail);
}

反復関数:

void printrev()
{
    struct node *end;
    for (end = start; end->next != NULL; end = end->next)
    ;
    for (; end != NULL; end = end->prev)
    printf("%d ", end->detail);
}

あなたgetNodeの用途は限られています。要素を削除したい場合getnode、要素が存在するかどうかのみを返すとします。存在するとしてdeleteNodeも、関数はリスト内の適切な要素を削除する前に反復する必要があります。

getNodeこれは、ノードへのポインターを返すことで解決できます。

node *getNode(int x)
{
    node *t;
    for (t = start; t != NULL; t = t->next)
        if (t->detail == x)
                return t;
    return t;
}

これで、次のようにコードを削除できます。

void delNode(node *n)
{
    n->prev->next = n->next;
    n->next->prev = n->prev;
    free(n);
}

そして、次のように呼び出します。

node *n;

if ((n = getNode(x)) != NULL)
    delNode(n);

私はあなたstructが次のようであると仮定しました:

struct node {
    int detail;
    struct node *next;
    struct node *right;
};

typedef struct node * node;
于 2013-07-26T12:35:58.943 に答える
0
  • printListrev() では、反転しないノードを 1 つだけ出力しています。
  • あなたがやっている大きな問題の1つは getNode() にあります。ローカルポインターのアドレスを変更していますが、元のポインターは以前の場所を指しています。
  • もしそうなら、この関数が戻った後にノードアドレスを知ることができないので、そのノードを削除する方法。
  • すべてのノードに対して getNode() を呼び出すつもりですか? その場合、多くのノードがある場合は適切ではありません。
于 2013-07-26T12:36:56.400 に答える