2013-10-25 43 views
1

這是我一直在研究的程序。該程序可讓您將字符串放入鏈接列表中,然後讓您操作列表。 「ins」可讓您將字符串插入列表中,「del」可讓您刪除這些字符串,「prl」可讓您查看列表中的內容,'pst'可讓您查看打印統計信息。我想我已經完成了一個體面的金額,但這是我的問題。我希望這是旁邊的節點超過1個節點,印在PRL一個#,在鏈表中添加冗餘節點

說你想進入名5次進入名單:

而不是5「名稱1」我想它說一次「名稱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; 
    } 
}