2013-10-05 40 views
0

代碼應創建一個雙向鏈接列表。這個IP地址列表應該添加到這個列表中,並且符合唯一IP的次數。然後列表應該排序。對不起,代碼在記錄時會在某個地方循環。以粗體突出顯示的地方(試圖做到:))。 P.S.如果你能幫我選擇排序方法,我會很高興。我已經做了一個,但使用quicksort或其他什麼會更好?無盡的循環錄製字符串

#include <stdlib.h> 
#include <iostream> 
#include <stdio.h> 
using namespace std; 
struct IP 
{ 
    char b[20]; 
    int count; 
}; 
struct Node 
{ 
    IP a; 

    Node *Next,*Prev; 
}; 
struct List 
{ 
    Node *Head,*Tail; 
    int length; 
    List():Head(NULL),Tail(NULL){}; 


}; 
List* list_new() 
{ 
    return (List *)calloc(1, sizeof(List)); 
} 
void list_delete(List* l) 
{ 
    while (l->Head) 
    { 
     l->Tail=l->Head->Next; 
     free (l->Head); 
     l->Head=l->Tail; 
    } 
    l->length=0; 
} 
bool push(List* l, IP a) 
{ 
    Node *temp=(Node*) calloc (1, sizeof(Node)); 
    temp->Next=NULL; 
    temp->a=a; 
    if (l->Head!=NULL) 
    { 
     temp->Prev=l->Tail; 
     l->Tail->Next=temp; 
     l->Tail=temp; 
    } 
    else 
    { 
     temp->Prev=NULL; 
     l->Head=l->Tail=temp; 
    } 
    return 1; 
} 
bool pop(List*l, IP* x) 
{ 
    (*x)=l->Tail->a; 
    l->Tail->Prev->Next=NULL; 
    l->Tail=l->Tail->Prev; 
    l->length++; 
    return 1; 
} 
bool unshift(List*l, IP a) 
{ 
    Node *temp=(Node*) calloc (1, sizeof(Node)); 
    temp->Next=NULL; 
    temp->a=a; 
    if (l->Head!=NULL) 
    { 
     temp->Next=l->Head; 
     l->Head->Prev=temp; 
     l->Head=temp; 


    } 
    else 
    { 
     temp->Prev=NULL; 
     l->Head=l->Tail=temp; 
    } 
    return 1; 
} 
bool shift(List* l, IP* x) 
{ 
    (*x)=l->Head->a; 
    l->Head->Next->Prev=NULL; 
    l->Head=l->Head->Next; 
    return 1; 
} 
bool reverse (List* l) 
{ 
    Node* temp=l->Head; 
    Node* swaps=NULL; 
    l->Tail=l->Head; 
    while (temp!=NULL) 
    { 
     swaps=temp->Prev; 
     temp->Prev=temp->Next; 
     temp->Next=swaps; 
     temp=temp->Prev; 
    } 
    if (swaps != NULL) l->Head = swaps->Prev; 
    return 1; 
} 
void sort (List* l) 
{ 
    int i; 
    for (i=0; i<l->length; ++i) { 
     Node* compared = l->Head; 
     while (compared->Next != NULL) { 
      if (compared->Next->a.count > compared->a.count) { 
       IP t = compared->Next->a; 
       compared->Next->a = compared->a; 
       compared->a = t; 
      } 
      compared = compared->Next; 
     } 
    } 
} 
void Show(List* l) 
{ 
    int i; 

    Node* temp=l->Head; 
    while (temp!=NULL) 
    { 

     cout<<temp->a.b<<" "<<temp->a.count<<"\n"; 
     temp=temp->Next; 
    } 
    cout<<"\n"; 
} 

int main() 
{ 
    int i; 
    char strbuf[1000],chTemp; 
    IP ipTemp; 
    bool met; 
    system("CLS"); 

    List* l = list_new(); 

    FILE* foo; 
    errno_t err; 
    err=fopen_s(&foo,"input.txt","r"); 
    if(err == 0) 
    { 
     printf("The file 'input.txt' was opened\n"); 
    } 
    else 
    { 
     printf("The file 'input.txt' was not opened\n"); 
    } 
    while (!feof(foo)) 
    { 

     fgets(strbuf,1000,foo); 
     fclose(foo); 
     for (i=0;i++;i<20) 
      if (strbuf[i]==' ') {strncpy_s(ipTemp.b,strbuf, i);break;} 

     Node* cur = l->Head; 
     met=0; 
     while (cur!=NULL) 
     { 
      if (cur->a.b == ipTemp.b) 
      { 
       met=1; 
       cur->a.count++; 
       break; 
      } 
      cur=cur->Next; 
     } 
     if (met==0) 
     { 
      push(l,ipTemp); 
      l->Tail->a.count++; 
     } 
    } 

    sort(l); 
    Show(l); 

    system("PAUSE"); 
} 
+3

對於眼睛來說,看到C和C++的混合總是很痛苦 – LihO

+1

除了類構造函數(因爲你的'calloc()'而不是'''''''''''''',所以在這個**中並沒有明顯使用C++編程語言**。如果你想要一個有序的鏈接列表,可以使用'std :: list '和'std :: list :: sort'並完成它。如果這是針對C++編程類的,那麼您可能無法獲得任何接近尊敬級別的東西。排序算法是您最擔心的問題。首先獲取列表,加載器和管理器*。 – WhozCraig

回答

3

如果代碼有一個更清潔的壓痕,你也許意識到邏輯是錯的:

while (!feof(foo)) 
{ 
    fgets(strbuf,1000,foo);  // <-- what if fgets hits EOF or error occurs? 
    fclose(foo);    // <-- why? 
    for (i = 0; i++; i < 20) // <-- i++ is always true ~> infinite loop 
     .... 
    ... 
} 

應(假設你想編寫代碼C):

while (fgets(strbuf, 1000, foo)) 
{ 
    for (i = 0; i < 20; i++) 
     .... 
    ... 
} 
+0

+1,您可以輕鬆地複製'while'表達式的'// < - why?'註釋以及其餘代碼的約80%。不知道輸入格式顯然是不方便的,但顯然前20個字符中沒有空格的任何行(不管是多少個被讀取或不讀取)都被忽略。這可能是故意的,但很難說。如果我能夠努力破譯含有少量的意義的話,我會再次高調地回答這個問題。 – WhozCraig

1

for (i=0;i++;i<20)應該for (i=0;i<20;i++)