树形链表

#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;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
/* * 基于链表实现树结构 */ package dsa; public class TreeLinkedList implements Tree { private Object element;//树根节点 private TreeLinkedList parent, firstChild, nextSibling;//父亲、长子及最大的弟弟 //(单节点树)构造方法 public TreeLinkedList() { this(null, null, null, null); } //构造方法 public TreeLinkedList(Object e, TreeLinkedList p, TreeLinkedList c, TreeLinkedList s) { element = e; parent = p; firstChild = c; nextSibling = s; } /*---------- Tree接口中各方法的实现 ----------*/ //返回当前节点中存放的对象 public Object getElem() { return element; } //将对象obj存入当前节点,并返回此前的内容 public Object setElem(Object obj) { Object bak = element; element = obj; return bak; } //返回当前节点的父节点;对于根节点,返回null public TreeLinkedList getParent() { return parent; } //返回当前节点的长子;若没有孩子,则返回null public TreeLinkedList getFirstChild() { return firstChild; } //返回当前节点的最大弟弟;若没有弟弟,则返回null public TreeLinkedList getNextSibling() { return nextSibling; } //返回当前节点后代元素的数目,即以当前节点为根的子树的规模 public int getSize() { int size = 1;//当前节点也是自己的后代 TreeLinkedList subtree = firstChild;//从长子开始 while (null != subtree) {//依次 size += subtree.getSize();//累加 subtree = subtree.getNextSibling();//所有孩子的后代数目 } return size;//即可得到当前节点的后代总数 } //返回当前节点的高度 public int getHeight() { int height = -1; TreeLinkedList subtree = firstChild;//从长子开始 while (null != subtree) {//依次 height = Math.max(height, subtree.getHeight());//在所有孩子中取最大高度 subtree = subtree.getNextSibling(); } return height+1;//即可得到当前节点的高度 } //返回当前节点的深度 public int getDepth() { int depth = 0; TreeLinkedList p = parent;//从父亲开始 while (null != p) {//依次 depth++; p = p.getParent();//访问各个真祖先 } return depth;//真祖先的数目,即为当前节点的深度 } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值