코드는 이중 연결된 목록을 만들어야합니다. 고유 한 IP가 충족되는 횟수로 IP 주소 목록을이 목록에 추가해야합니다. 그런 다음 목록을 정렬해야합니다. 죄송합니다. 코드는 녹음 될 때 어딘가에 순환됩니다. 굵게 표시된 부분을 강조 표시했습니다. 오후 8시 30 분 P.S. 정렬 방법 선택을 도와 주시면 기쁘게 생각합니다. 나는 이미 그것을 만들었지 만, 퀵 소트 또는 다른 것을 사용하는 것이 더 좋을까요?무한 반복 녹음 문자열
#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");
}
C와 C++의 혼합을 눈으로 보는 것은 항상 고통 스럽습니다. – LihO
클래스 생성자 ('new'보다는'calloc()'을 사용했기 때문에 결코 해고되지 않음)와는 별도로 C++ 프로그래밍 언어를 전혀 사용하지 않습니다 **. 정렬 된 문자열리스트를 원한다면,'std :: list'와'std :: list :: sort'를 사용하면된다. 이것이 C++ 프로그래밍 클래스를위한 것이라면, 당신은 존경할만한 등급에 가까운 것을 얻지 못할 것입니다. 정렬 알고리즘은 걱정거리 중 가장 적은 것입니다. 목록, 로더 및 관리를 * 처음 * 가져옵니다. –
WhozCraig