簡體   English   中英

tail->下一個打印地址而不是數據循環鏈表c ++

[英]tail->next printing address instead of data circular linked list c++

我試圖在我的循環單鏈表中將兩端綁在一起。 在文件名file.txt中,包含ABCDEFGHIJKLMNOPQRSTUVWXYZ作為文本,我能夠分別打印出頭部和尾部,A和Z. 但是,我希望Z指向A但是我的輸出是A的地址(見下文)。 我在addNode()中做錯了什么?

#include <string.h>
#include <cstdio>
#include <cstdlib>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <fstream>    // For file input
#include <cassert>    // for assertions
using namespace std;

    FILE *filename = fopen("file.txt", "r"); // Try to open file


struct Node
  {
  char data;
  Node* next;

  } ;

    void addNode( Node * &head, Node * &tail, char input)
      {
      Node *pTemp = new Node;
      pTemp->data = input;

       if( head == NULL)
          {
            head=pTemp;
           tail=pTemp;
               tail->next = head;
          }

      else
          {
          tail->next = pTemp;
                pTemp->next=head;
          }

    tail=pTemp;

      }//end addNode()


    int main()
      {
      assert(filename);
      Node *head=NULL;
       Node *tail=NULL;
      char c =' ';
      int i=0;

      while( fscanf(filename,"%c", &c) != EOF)
        addNode( head,tail, c);



    cout<<endl;
    cout<<"\nHead element is "<<head->data<<endl;
    cout<<"Tail element is "<<tail->data<<endl;
    cout<<"After tail '"<<tail->data<<"' is : "<< tail->next<<endl;

      }

目前的輸出是:

Head element is A
Tail element is Z
After tail 'Z' is : 0x371168

期望的輸出是:

Head element is A
Tail element is Z
After tail 'Z' is : A
#include <string.h>
#include <cstdio>
#include <cstdlib>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <fstream>    // For file input
#include <cassert>    // for assertions
using namespace std;

    FILE *filename = fopen("file.txt", "r"); // Try to open file


struct Node
  {
  char data;
  Node* next;

  } ;

    void addNode( Node * &head, Node * &tail, char input)
      {
      Node *pTemp = new Node;
      pTemp->data = input;

       if( head == NULL)
          {
            head=tail =pTemp;
           tail->next=pTemp;

          }

      else
          {
          pTemp->next = tail->next;
          tail->next=pTemp;
          tail = pTemp;

          }


      }//end addNode()


    int main()
      {
      assert(filename);
      Node *head=NULL;
       Node *tail=NULL;
      char c =' ';
      int i=0;

      while( fscanf(filename,"%c", &c) != EOF)
        addNode( head,tail, c);



    cout<<endl;
    cout<<"\nHead element is "<<head->data<<endl;
    cout<<"Tail element is "<<tail->data<<endl;
    cout<<"After tail '"<<tail->data<<"' is : "<< tail->next->data<<endl;

      }

嘗試這個。

我進一步改進並刪除了addNode中所需的代碼。

你想設置“tail-> next = pTemp;”的原因 是因為pTemp是一個新的內存,因為你正在創建一個鏈接列表,你希望前一個節點(下一個)指針指向新的內存。 因此,它創建了一個鏈接列表。

這樣,您不僅可以遍歷第一個和最后一個元素。 您可以遍歷整個鏈接列表。

如果你錯過了這個陳述,那么你將無法遍歷。 前一節點的下一個變量將不與下一個節點連接。

例:

Node A
[A  | pointer to self] <- head and tail 
Node B
[A  | pointer to B ] <-head
[B  | pointer to A ] <-tail
Node C
[A  | pointer to B ] <-head
[B  | pointer to C ]  
[C  | pointer to A ]   <-tail

Node D
[A  | pointer to B ] <-head
[B  | pointer to C ]  
[C  | pointer to D ]    
[C  | pointer to A ]   <-tail

tail->next只是指向下一個元素的指針。 你仍然應該稱它為data成員:

cout<<"After tail '"<<tail->data<<"' is : "<< tail->next->data <<endl;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM