2013-10-25 2 views
1

이것은 제가 작업 해본 프로그램입니다. 이 프로그램을 사용하면 문자열을 링크 된 목록에 넣은 다음 목록을 조작 할 수 있습니다. "ins"는 문자열을 목록에 삽입하고, "del"은 문자열을 삭제하며, "prl"은 목록에있는 내용을 검토하고, 'pst'는 인쇄 통계를 볼 수있게합니다. 괜찮은 금액을 벌었다고 생각하지만 여기에 내 문제가 있습니다.링크 된 목록에 중복 노드 추가

대신 5 "이름 1": 나는 1 개 이상의 노드가 PRL에 인쇄 된 노드 옆에 #,

당신이 5 번 목록에 이름을 입력하고 싶었 말할 수있다 싶어 한 번만 "name 5"라고 말하고 싶습니다. ins 함수, 내 기본 메소드 및 if 문에서 수백만 가지 옵션을 시도했습니다. 아무것도 출력을 변경하려면 보이지 않습니다. 조언이나 조언이 있다면 크게 감사하겠습니다. 정말 고맙습니다.

#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 

#define MIN_LENGTH 4 
#define MAX_LENGTH 11 //define command length 

struct node{ 
    char list[MAX_LENGTH]; 
    int count; 
     struct node *next; 
}; //creates node to be manipulated 

typedef struct node Node; 
typedef Node *ListNode; 

void ins(ListNode *ptr, char *value); 
char* del(ListNode *ptr, char *value); 
void prl(ListNode currPtr); 
void pst(ListNode currPtr); 
//function prototype 

int count_of_nodes; 

int main(void){ 

    ListNode startPtr = NULL; //sets List to Null value 

    char com[MIN_LENGTH]; 
    char cho[MAX_LENGTH]; //declares com and cho to be within the set boundaries 

    while(strcmp(com, "end") != 0){ 
    printf("Command? "); 
    scanf("%s", &com); 
    //entering 'end' as a command will cause this program to stop, 

    if(strcmp(com, "ins") == 0){ 
     scanf("%s", &cho); 
     ins(&startPtr, cho); 
     count_of_nodes ++; 
      printf("%s ", cho); 
     printf("%d\n", count_of_nodes); 
     //if statement for the insert command, 'ins', corresponds to ins function 
     // Adds a string of your choice to the list 
    } 

     else if(strcmp(com, "del") == 0){ 
    scanf("%s", &cho); 
    if(del(&startPtr, cho)){ 
     count_of_nodes --; 
     printf("%d\n", count_of_nodes); 
    } 
    else{ 
     printf("%s not found.\n", cho); 
    } 
    // if statement for the delete command, 'del' 
    //deletes a string of your choice from the list 
     } 

     else if(strcmp(com, "fde") == 0){ 
    // fde(); 
     scanf("%s", &cho); 
     printf("%s\n", cho); 

    //if statement for the force delete command, 'fde' 
    // work in progress, should delete node, regargless of of it's count 
     } 

     else if(strcmp(com, "pst") == 0){ 
    pst(startPtr); 

    //if statement for the print stats command, 'pst' 
    // allows you to see # of nodes, node with max count 
    // node with min count, and average count of nodes 
     } 

     else if(strcmp(com, "prl") == 0){ 
    prl(startPtr); 

    //if statement for printing the list, 'prl' 
    // prints out the list as it is, shows the count of strings 
    } 

     else if(strcmp(com, "pcr") == 0){ 
    // pcr(); 
     scanf("%s", &cho); 
     printf("%s\n", cho); 

     //if statement for print count range, 'pcr' 
     //work in progress, should print nodes with a count between an interval 
     // user chooses interval 

    } 
     else if(strcmp(com, "ppr") == 0){ 
    // ppr(); 
     scanf("%s", &cho); 
     printf("%s\n", cho); 

     //if statement for print prefix, 'ppr' 
     //work in progress, should add count to node by entering node prefix 
    } 
     else if(strcmp(com, "psu") == 0){ 
    // psu(); 
     scanf("%s", &cho); 
     printf("%s\n", cho); 

     //if statement for print suffix, 'psu' 
     //work in progress, should add count to node by entering node suffix 
    } 

    else if(strlen(com) >= 4 || strlen(com) < 3){ 
    printf("You have entered an incorrect command.\n"); 
    //bug checks 
    } 
    } 
} 


void ins(ListNode *ptr, char *value){ 
    //insert function 

    ListNode newPtr; 
    ListNode prevPtr; 
    ListNode currPtr; 
    //variables used in insert 

    newPtr = (ListNode) malloc(sizeof(Node)); 
    //make space in the list for new node 

    if(newPtr != NULL){ 
     if (strcmp(value, newPtr->list) == 0){ 
    newPtr->count++; 
     } //trouble area, trying to figure out how to add to count 
     else{ 
     memset(newPtr, 0, sizeof(Node)); 
     memcpy(newPtr-> list, value,strlen(value)); 
     //puts value into node 

     newPtr->count++; 
     } 
     // newPtr->list = value; 
     newPtr->next = NULL; 

    prevPtr = NULL; 
    currPtr = *ptr; 


    while(currPtr != NULL && value > currPtr-> list){ 
     prevPtr = currPtr; 
     currPtr = currPtr->next; 
    } 
    if(prevPtr == NULL){ 
     newPtr->next = *ptr; 
     *ptr = newPtr; 
    } 
    else{ 
     prevPtr->next = newPtr; 
     newPtr->next = currPtr; 
    } 
    } 

    else{ 
     printf("No memory available\n"); 
    }//bug checks 
} 
char* del(ListNode *ptr, char *value){ 
    //delete function 

    ListNode prevPtr; 
    ListNode currPtr; 
    ListNode tempPtr; 
    //variables used in delete 

    // if(value == (*ptr)->list){ 
    if(0 == strcmp(value, (*ptr)->list)){ 
    tempPtr = *ptr; 
    *ptr = (*ptr)->next; 
    free(tempPtr); //fress tempPtr 
    return value; 
    } 
    else{ 
    prevPtr = *ptr; 
    currPtr = (*ptr)->next; 

    while(currPtr != NULL && 0 != strcmp(value, currPtr->list)){ 
     prevPtr = currPtr; 
     currPtr = currPtr->next; 
    } 

    if(currPtr != NULL){ 
     tempPtr = currPtr; 
     prevPtr->next = currPtr->next; 
     free(tempPtr); 
     return value; 
    } 
    } 
    return '\0'; 
} 

void fde(){ 
} //work in progress, disregard 

void pst(ListNode currPtr){ 
    //print stats function 

    int total; 
    float avg; 
    ListNode maxP; 
    ListNode minP; 
    ListNode temp; 
    //variables used in print stats 

    if (currPtr == NULL){ 
    printf("The list is empty.\n"); //bug checks 
    } 
    else{ 
    temp = maxP = minP = currPtr; 


    while (temp != NULL) 
    { 
     if (temp->count > maxP->count) 
    { 
     maxP = temp; //finds max value of count 
    } 
     else if (temp->count < minP->count) 
    { 
     minP = temp; //finds min value of count 
    } 
     total = total + temp->count; 
     temp = temp->next; 
    } 

    } 
    avg = total/(float)count_of_nodes; //finds average for node counts 

    printf("Total of nodes: %d\n", count_of_nodes); 
    printf("Max: %d\n", maxP->count); 
    printf("Min: %d\n", minP->count); 
     printf("Average: %f\n", avg); 
     //prints for function 
} 

void prl(ListNode currPtr){ 

    if(currPtr == NULL){ 
    printf("The List is Empty.\n"); //bug checks 

    }else{ 
    while(currPtr != NULL){ //loops through list and prints all nodes inside 
     printf("%s " , currPtr->list); 
     printf("%d\n", currPtr->count); 
     currPtr = currPtr->next;  
    } 
    } 
} 

void pcr(){ 
} //work in progress, disregard 
void ppr(){ 
} //work in progress, disregard 
void psu(){ 
} //work in progress, disregard 
+0

'ins'에서는 새로운 노드에 대한 메모리를 즉시 할당합니다. 기존 목록을 반복하여 이름이 일치하는 노드를 찾아야합니다. 이미 존재하는 경우 카운트를 증가시킵니다. 그렇지 않은 경우 새 노드를 만드십시오. –

+0

새 노드를 할당하기 전에 루프를 추가 할 수 있습니까?이 루프는 목록을 검색하고 일치하는 노드를 찾습니다. – user2917840

답변

0

난 그냥, 새로운 노드를 할당하기 전에 루프 일치하는 노드의 목록을 검색하고 보이는 을 루프를 추가 할 수 있을까요?

예, 가능합니다. del에서 사용할 수있는 함수에이 루프를 넣는 것이 좋습니다.

// search list '**ptr' in ascending order for string 'value' 
// return 0 if string found in list, '*ptr' points to pointer to string's node 
//  >0 if not found and '*ptr' points to node pointer where to insert 
//  -1 if not found and '*ptr' points to null pointer where to append 
int search(ListNode **ptr, char *value) 
{ 
    ListNode currPtr; 
    for (; currPtr = **ptr; *ptr = &currPtr->next) 
    { 
     int cmp = strcmp(currPtr->list, value); 
     if (0 <= cmp) return cmp; 
    } 
    return -1; 
} 

void ins(ListNode *ptr, char *value) 
{ //insert function 
    if (search(&ptr, value) == 0) 
     (*ptr)->count++; 
    else 
    { //make space in the list for new node 
     ListNode newPtr = malloc(sizeof(Node)); 
     if (newPtr == NULL) 
     { 
      printf("No memory available\n"); 
      return; 
     } 

     strncpy(newPtr->list, value, sizeof newPtr->list); 
     newPtr->count = 1; 
     newPtr->next = *ptr; 
     *ptr = newPtr; 
    } 
}