2013-07-17 40 views
0

這是我在鏈表中​​追加整數的代碼。將數組的元素存儲在鏈表中

#include "iostream" 
using namespace std; 

class LinkList 
{ 
private: 
     struct Node 
     { 
     int data; 
     Node* link; 
     }*p; 

public: 
     LinkList(); 
     ~LinkList(); 

     void Print();   // Prints the contents of linkedlist 
     void Append(int num); // Adds a new node at the end of the linkedlist 
     void Delete(int num); // Deletes the specified node from the linkedlist 

     void AddatBeg(int num);// Adds a new node at the beginning of the linkedlist 
     void AddAfter(int c, int num); // Adds a new node after specified number of nodes 
     int Count();   // Counts number of nodes present in the linkedlist 

}; 

LinkList::LinkList() 
{ 
    p = NULL; 
} 

LinkList::~LinkList() 
{ 
    if (p == NULL) 
     return; 

    Node* tmp; 
    while(p != NULL) 
    { 
     tmp = p->link ; 
    delete p; 
     p = tmp; 
    } 
} 

// Prints the contents of linkedlist 
void LinkList::Print() 
{ 
    if (p == NULL) 
    { 
     cout<< "EMPTY"; 
     return; 
    } 

    //Traverse 
    Node* tmp = p; 
    while(tmp != NULL) 
    { 
     cout<<tmp->data<<endl; 
     tmp = tmp->link ; 
    } 
} 

// Adds a new node at the end of the linkedlist 
void LinkList::Append(int num) 
{ 
     Node *newNode; 

     newNode = new Node; 
     newNode->data = num; 
     newNode->link = NULL; 

     if(p == NULL) 
     { 
     //create first node 
     p = newNode; 
     } 
     else 
     { 
      //Traverse 
      Node *tmp = p; 
      while(tmp->link != NULL) 
      { 
        tmp = tmp->link; 
      } 

      //add node to the end 
     tmp->link = newNode; 
     } 
} 

// Deletes the specified node from the linkedlist 
void LinkList::Delete(int num) 
{ 
    Node *tmp; 

    tmp = p; 
    //If node to be delete is first node 
    if(tmp->data == num) 
    { 
     p = tmp->link; 
     delete tmp; 
     return; 
    } 

    // traverse list till the last but one node is reached 
    Node *tmp2 = tmp; 
    while(tmp!=NULL) 
    { 
     if(tmp->data == num) 
     { 
     tmp2->link = tmp->link; 
     delete tmp; 
     return; 
     } 

     tmp2 = tmp; 
     tmp = tmp->link; 
    } 
    cout<< "\nElement "<<num<<" not Found." ; 
} 

int LinkList::Count() 
{ 
     Node *tmp; 
     int c = 0; 

     //Traverse the entire Linked List 
     for (tmp = p; tmp != NULL; tmp = tmp->link) 
      c++; 

     return (c); 
} 

int main() 
{ 
     LinkList* pobj = new LinkList(); 
     pobj->Append(11); 
     pobj->Print(); 

     delete pobj; 
     return 0; 
} 

我在找的是一個代碼,我可以在鏈表中插入數組元素。例如,如果有兩個包含元素(1,2,3)和(4,5,6)的數組,則Code應該創建兩個鏈接列表,並且每個鏈接列表的第一個節點的地址應該存儲在一個數組中,以便運行for循環將按順序打印所有鏈接列表。 實施例:

Linked List 1 = (1,2,3) 
Linked List 2 = (4,5,6) 

陣列和陣列中的元素數目的數量將是動態的。

+1

爲什麼你想要鏈接列表? (有證據表明他們很少*有用)。如果你真的*必須*使用鏈表,有沒有理由不能使用'std ::: list'? –

+0

這個問題似乎是脫離主題,屬於http://codereview.stackexchange.com/ – TemplateRex

+0

@JerryCoffin:整個想法是將電話號碼列表存儲在內存中,並以最快的方式搜索任何號碼。 – sajal

回答

2

然後你應該使用這個2D矢量std::vector< std::vector<int> > vect;

+0

不要忘記在> as之後放置空格>>是一個運算符,並且會產生編譯時錯誤 –

+0

@ sajal chk的答案,既然你想列表的大小包含列表以及增長這是解決方案。 –

+1

非常感謝。這正是我尋找的解決方案。 – sajal

2

注意:我想你的問題是爲了學習的目的。如果沒有,請不要執行您自己的鏈接列表,請使用std::vector。因爲緩存未命中,因此優先考慮向量爲std::list,鏈接列表的性能低於向量。檢查this question about performance comparisons between std::list and syd::vector

鏈表作爲雙鏈表普遍實現,使push_backpop_back操作O(1)(也就是說,沒有遍歷需要的push_back)。

來實現這一點的方式,在節點存儲不僅僅是一個鏈接到下一個節點,也是一個鏈接到一個節點:

struct node 
{ 
    node* previous; 
    node* next; 
    int data; 
}; 

而且鏈表類存儲,表示結束節點名單中,除了起始節點:

node* begin; 
node* end; 

請注意,我說的,不「表示列表的最後一個節點」 「列表的最後一個節點」。也就是說,end->previous指向列表的最後一個節點。 end->next指向任何內容(nullptr)。 Check this picture。所以

通過這種方式,push_back實施只穿end->preious指向新節點(和new_node->next指着end),並new_node->previous指着end->previous插入後的值。

+0

即使我使用std :: vector,我正在尋找的是一組向量,它將逐個顯示所有向量。 – sajal

+0

然後更改節點的數據成員並使用std :: vector。但最簡單的方法是使用矢量矢量('std :: vector >') – Manu343726

相關問題