在链表中添加冗余节点

时间:2013-10-25 22:00:27

标签: c linked-list

这是我一直在努力的计划。此程序允许您将字符串放入链接列表,然后让您操作列表。 “ins”允许您将字符串插入列表,“del”允许您删除这些字符串,“prl”允许您查看列表中的内容,“pst”可让您查看打印统计信息。我想我已经完成了相当多的工作,但这是我的问题。我希望在节点旁边有一个#,在prl中打印多于1个节点,

说你想在名单中输入5次名字:

Command? ins name
Command? ins name
Command? ins name
Command? ins name
Command? ins name
Command? prl
name 1 
name 1
name 1
name 1 
name 1

而不是5“名字1”我想说“名字5”一次, 我在我的ins函数,我的main方法和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

1 个答案:

答案 0 :(得分: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;
    }
}