#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#define NODEADDSAMEOK 0
#define NODEADDSAMENG 1
typedef struct lk
{
int id;
struct lk *pnext;
int data;
}Node;
typedef struct lklst
{
Node *list;
int count;
int data;
struct lklst *lstnext;
}lists;
void DisplayList(Node * head);
Node *CreateNode()
{
Node *node = NULL;
node = (Node *)malloc(sizeof(Node));
node->pnext = NULL;
node->data = (rand()%100);
return node;
}
lists * createlists()
{
lists *node = NULL;
node = (lists *)malloc(sizeof(lists));
node->lstnext = NULL;
node->list = NULL;
node->list = (Node *)malloc(sizeof(Node));
return node;
}
void AddNodetail(Node *head,Node *node)
{
Node *temp = head;
Node *temptmp = NULL;
temptmp = (Node *)malloc(sizeof(*temptmp));
memcpy(temptmp,node,sizeof(Node));
if(temp == NULL)
{
printf("NULL head\n");
return;
}
else
{
while(temp->pnext != NULL)
{
temp = temp->pnext;
}
if(temptmp == NULL)
{
printf("add tail fail : temptmp == NULL");
}
else
{
temp->pnext = temptmp;
temp->pnext->pnext = NULL;
}
}
}
void InitList(Node **head ,int count)
{
Node *temp = *head;
int i = 0;
if(temp == NULL)
{
printf("list init error !\n");
}
else
{
for(i = 0; i < count; i++)
{
temp->pnext = CreateNode();
temp->pnext->id = i;
temp = temp->pnext;
}
}
printf("\n");
}
lists *SameData(Node *head)
{
Node *temp = head;
lists *lststemp= NULL;
lststemp = (lists *)malloc(sizeof(lists));
lists *lsts= NULL;
lsts = (lists *)malloc(sizeof(lists));
lsts = lststemp;
int flag = NODEADDSAMENG;
if(temp == NULL)
{
printf("NULL LIST \n");
return NULL;
}
else
{
while(temp->pnext != NULL)
{
temp = temp->pnext;
lststemp = lsts;
flag = NODEADDSAMENG;
if(lsts == NULL)
{
printf("lsts malloc error :: NULL\n");
return NULL;
}
else if(lststemp == NULL)
{
printf("lststemp malloc error : NULL\n");
return NULL;
}
else
{
while(lststemp != NULL || flag == NODEADDSAMENG)
{
if(lststemp->data == temp->data)
{
lststemp->count++;
AddNodetail(lststemp->list,temp);
flag = NODEADDSAMEOK;
}
else
{
}
if(lststemp->lstnext == NULL && flag == NODEADDSAMENG)
{
lststemp->lstnext = createlists();
lststemp->lstnext->count++;
printf("11111\n");
AddNodetail(lststemp->lstnext->list,temp);
lststemp->lstnext->data = temp->data;
lststemp->lstnext->lstnext == NULL;
break;
}
lststemp = lststemp->lstnext;
}
}
}
}
return lsts;
}
void FreeList(Node * head)
{
while(head != NULL)
{
free(head);
head = head->pnext;
//printf("free\n");
}
}
void FreeLists(lists *head)
{
while(head != NULL)
{
while(head->list)
{
free(head->list);
head->list = head->list->pnext;
//printf("head list free\n");
}
free(head);
head = head->lstnext;
//printf("lstnext free\n");
}
}
#if 0
void FreeList(Node *head)
{
if(head->pnext == NULL)
{
if(head != NULL)
{
printf("head free \n");
free(head);
}
printf("end free\n");
return;
}
else
{
printf("one free\n");
FreeList(head->pnext);
}
}
void FreeLists(lists *lst)
{
if(lst->lstnext == NULL)
{
if(lst != NULL)
{
printf("lst free \n");
free(lst);
}
printf("end lst free\n");
return;
}
else
{
printf("one lst free\n");
free(lst->list);
FreeLists(lst->lstnext);
}
}
#endif
void DisplayList(Node * head)
{
Node *temp = head;
if(temp == NULL)
{
printf("display error \n");
}
else
{
temp = temp->pnext;
while(temp != NULL)
{
printf(" id : %d ,data : %d \n",temp->id,temp->data);
temp = temp->pnext;
}
}
}
void DisplaySameLists(lists * listhead)
{
lists *temp = listhead;
if(listhead == NULL)
{
printf("display listhead error \n");
}
else
{
temp = temp->lstnext;
while(temp != NULL)
{
printf("___________________\n");
printf(" same list count : %d,data : %d \n",temp->count,temp->data);
DisplayList(temp->list);
printf("__________________\n");
temp = temp->lstnext;
}
}
}
int main()
{
srand((unsigned)time(NULL));
Node *list= NULL;
list = (Node *)malloc(sizeof(Node));
lists *lst= NULL;
lst = (lists *)malloc(sizeof(lists));
int count = 0;
printf("input create nodes:");
scanf("%d",&count);
InitList(&list,count);
DisplayList(list);
lst = SameData(list);
DisplaySameLists(lst);
FreeList(list);
FreeLists(lst);
return 0;
}