2013-10-05 42 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++)