只有在C链表追加唯一值 [英] Appending unique values only in a linked list in C

查看:80
本文介绍了只有在C链表追加唯一值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

typedef struct child {int count; char word[100]; inner_list*next;} child;
typedef struct parent
{ char data [100];
child * head;
int count;
parent * next; } parent;

void append(child **q,char num[100],int size)
{   child *temp,*r,*temp2,*temp3;
parent *out=NULL;
  temp = *q;
  temp2 = *q;
  temp3 = *q;
  char *str;
  if(*q==NULL)
  {   temp = (child *)malloc(sizeof(child));
    strcpy(temp->word,num);
    temp->count =size;
    temp->next=NULL;
    *q=temp;
  }
  else
  {  temp = *q;
   while(temp->next !=NULL)
   {  temp=temp->next;
   }
   r = (child *)malloc(sizeof(child));
   strcpy(r->word,num);
   r->count = size;
   r->next=NULL;
   temp->next=r;
  }

}

这是我使用的添加元素为我的孩子我的名单追加功能。但我的问题是,它仅应追加其后面跟着一个字符串唯一值。这意味着:

This is my append function which I use for adding an element to my child list. But my problem is it only should append unique values which are followed by a string. Which means :

Inputs :  aaa bbb aaa ccc aaa bbb ccc aaa

附加应采取行动:

Append should act :

For aaa string there  should be a list like bbb->ccc(Not bbb->ccc->bbb since bbb is already there if bbb is coming more than one time it should be increase count only.)
For bbb string there should be list like aaa->ccc only
For ccc string there should be list like aaa only

我希望我能讲得清楚。有没有什么想法?请询问进一步的信息。

I hope i could make myself clear. Is there any ideas? Please ask for further info.

我已经试过正在检查与新元素进入了previous元素。我有点失败了。

What i've tried is checking the previous elements entered with the new element. I kinda failed it.

int search(child *p)
{
    child *temp= (child *)malloc(sizeof(child));
    int var =0;
char num[100];
temp = p;
strcpy(num,p->word);
while(temp->next!=NULL)
    {

    if(strcmp(temp->word,num)==0)
    var =1;
    temp=temp->next;
    }
return var;
}

这是我到目前为止已经试过。有了这个搜索功能,如果该元素在不在,我会控制。但它失败了。

This is what i've tried so far. With this search function i would control if the element is here or not. But it failed.

推荐答案

如果我理解正确的话,考虑到输入

If I understand correctly, given the inputs

aaa bbb aaa ccc aaa bbb ccc aaa

您希望父列表中有3个要素 - 对 AAA 子列表,其中 BBB 和一个为 CCC

You want the parent list to have 3 elements - a child list for aaa, one for bbb and one for ccc.

AAA 列表应该包括随后所有的字符串 AAA 在原来的输入,这里只是 BBB CCC 。它应该只包含它们各一次,用计数中增加相应的节点变量,以便 BBB 的计数2 CCC的数为1。

The list for aaa should contain all strings that followed aaa in the original input, which here is just bbb and ccc. It should only contain them once each, with the count variable in the corresponding nodes incremented so that bbb's count is 2 and ccc's count is 1.

这是正确的?如果是,请继续阅读。

Is this correct? If it is, read on.

for every string S in your input
{
   if S is not associated with a child list in the parent
   {
      create a new child list associated with S at the end of the parent list
   }

   // now we have C, the child list we either found above or created
   if there is a string S' after S
   {
      find the element S' in the child list C by iterating through it
      if you don't find the element S', create/append it with count = 1
      else when you find the element, increment its count
   }
}

我想,你想走这应该得到你。

I think this should get you where you want to go.

这篇关于只有在C链表追加唯一值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆