int test_removeNode() {
	int valid = 0;
	int count = 0;
	struct doublyLinkedListNode *root = NULL;

	addNode(&root, createNode(9));
	addNode(&root, createNode(6));
	addNode(&root, createNode(4));
	addNode(&root, createNode(2));

	removeNode(&root, 0);
	count = countNode(root);
	valid += assertTrue("test_removeNode: The list has 4 nodes", count == 4);

	removeNode(&root, 4);

	count = countNode(root);
	valid += assertTrue("test_removeNode: The list has 3 nodes", count == 3);

	removeNode(&root, 9);

	count = countNode(root);
	valid += assertTrue("test_removeNode: The list has 2 nodes", count == 2);

	deleteList(&root);

	return valid;
}
int countNode(struct tree *root)
{
	if(root==NULL)
		return 0;
	else
	{
		return countNode(root->left)+countNode(root->right)+1;
	}
}
Exemple #3
0
int kthSmallest(TreeNode* root, int k) {
	int num = countNode(root);
	int n = countNode(root->right);
	if (k == num - n)
		return (root -> val);
	if (k < num - n)
		return kthSmallest(root -> left, k);
	else return kthSmallest(root -> right, k - num + n);
}
Exemple #4
0
void countNode(Node* node)
{
	if(node->left == 0 && node->right == 0)
	{
		if(node->free == 1)
			count[node->order]++;
		return;
	}

	if(node->left != 0)
		countNode(node->left);

	if(node->right != 0)
		countNode(node->right);
}
Exemple #5
0
int main()
{
  
  char fileName[50];
  Node *root = NULL;

  printf("Enter data file name: ");
  scanf("%[^\n]", fileName);
  myfflush();

  root = createTree(fileName);
  printf("Done\n");

  //traversal
  printf("PreOrder\n");
  preOrder(root);
  printf("\n");
  printf("inOrder\n");
  inOrder(root);
  printf("\n");
  printf("PostOrder\n");
  postOrder(root);
  printf("\n");

  //count Nodes
  printf("The number of nodes is %d\n", countNode(root));

  Node *h = find(root, 2);
  printf("The height of node h is %d\n", height(h));
  printf("The depth of node 11 is %d\n", depth(root, 11));

  return 0;
}//end main
Exemple #6
0
struct node *swapFirstAndLastKthNode(struct node *start,int k)
{
	int n=countNode(start),i;
	
	if(k>n)
	return start;
	
	if(k==(n-k+1))
	return start;
	
	struct node *first=start;
	struct node *first_prev=NULL;
	for(i=1;i<k;i++)
	{
		first_prev=first;
		first=first->link;
	}
	
	struct node *last=start;
	struct node *last_prev=NULL;
	for(i=1;i<(n-k+1);i++)
	{
		last_prev=last;
		last=last->link;
	}
	
	first_prev->link=last;
	last_prev->link=first;
	
	struct node *tmp=first->link;
	first->link=last->link;
	last->link=tmp;
	
	return start;
}
Exemple #7
0
 void countEdge(Node*, Edge edge)
 {
     // Don't count edges that are already counted for their type checks.
     if (edge.willHaveCheck())
         return;
     countNode(edge.node());
 }
void writeFile(struct month *cur)//파일에 데이터쓰기(출력)
{
	int n;
	int month, day;
	char place[50], name[50], content[50];
	FILE *data;
	//파일에 순서대로 출력한다.
	data = fopen("data.txt", "w");
	n = countNode(cur);
	if(n == 0)
	{//입력할 스케쥴이 없을 때
		fprintf(data,"");
	}else
	{
		while(cur != NULL)
		{
			month = cur->month;
			day = cur->day;
			strcpy(name, cur->name);
			strcpy(place, cur->place);
			strcpy(content, cur->content);
			fprintf(data, "%d %d %s %s %s\n", month, day, name, place, content);
			cur = cur->next;
		}
	}
	fclose(data);
}
Exemple #9
0
 int countNode(TreeNode* root, int a, int b) {
     if (!root) {
         return 0;
     }
     if (root -> val <= a || root -> val >= b) {
         return -1;
     }
     int left = countNode(root -> left, a, root -> val);
     if (left == -1) {
         return -1;
     }
     int right = countNode(root -> right, root -> val, b);
     if (right == -1) {
         return -1;
     }
     return left + right + 1;
 }
Exemple #10
0
void fillCount()
{
	int i = 0;
	for(i = 0; i <= MAX_ORDER; i++)
	{
		count[i] = 0;
	}
	countNode(root);
}
void writeNew(struct month *cur)//스케쥴의 개수가 0개일 때 데이터 새로 입력
{
	int n; //node의 개수
	input(cur);
	n = countNode(cur);
	if(n>1)//스케쥴의 개수가 2개이상이면 크기순으로 정렬
		sort(cur);
	//파일에 순서대로 출력한다.
	writeFile(cur); 
}
int test_countNode() {
	int valid = 0;
	int count = 0;
	struct doublyLinkedListNode *root = NULL;

	addNode(&root, createNode(1));
	addNode(&root, createNode(10));
	addNode(&root, createNode(100));
	addNode(&root, createNode(1000));

	count = countNode(root);
	valid += assertTrue("test_countNode: The list has 4 nodes", count == 4);

	deleteList(&root);

	count = countNode(root);
	valid += assertTrue("test_countNode: The list has 0 nodes", count == 0);

	return valid;
}
Exemple #13
0
 void helper(TreeNode* root, int &res) {
     if (!root) {
         return;
     }
     int tmp = countNode(root, INT_MIN, INT_MAX);
     if (tmp != -1) {
         res = max(res, tmp);
         return;
     }
     helper(root -> left, res);
     helper(root -> right, res);
 }
Exemple #14
0
void del_btw(int pos){	
	if (pos>0 && pos<=countNode())
	{
		struct node* temp = head;
		int count=0;
		while(count!=(pos-1)){
			temp = temp->next;
		}			
		temp->next=temp->next->next;
	}
	else printf("INVALID POSITIONS\n");
}
void addSchedule(struct month *cur)//스케쥴 추가
{
	int node;//스케쥴(node)의 개수
	node = countNode(cur);//스케쥴(node)의 개수
	if(node == 0)//스케쥴의 개수가 없을 때 데이터 새로 쓰기
	{
		writeNew(cur);
	}
	else
	{  
		insertion(cur);//스케쥴이 있는 경우 스케쥴 삽입
	}
}
Exemple #16
0
struct node *sort012(struct node *start)
{
	if(start==NULL||start->link==NULL)
	return start;
	
	int noOfNodes=countNode(start);
	
	struct node *tail=start;
	struct node *ptr=start;
	struct node *prev=start;
	
	while(tail->link!=NULL) tail=tail->link;
	int i;
	
	for(i=0;i<noOfNodes;i++)
	{
		struct node *tmp=ptr;
		ptr=ptr->link;
		
		if(tmp->data==0)
		{
			if(prev!=tmp)
			{
				tmp->link=start;
				start=tmp;
				prev->link=ptr;
			}
		}
		
		else if(tmp->data==2)
		{
			tail->link=tmp;
			tmp->link=NULL;
			tail=tmp;
			
			if(prev==tmp) start=prev=ptr;
			else prev->link=ptr;
		}
		
		else
		{
			if(prev!=tmp)
			prev=prev->link;
		}
	}
	return start;
}
void menu(struct month *cur)
{ 
	while(1)
	{
		int select;
		int node;//스케쥴 개수
		printf("\n       ┌── 목  록 ──┐\n       │                │\n");
		printf("       │ 1. 스케쥴 추가 │\n       │                │\n");
		printf("       │ 2. 스케쥴 제거 │\n       │                │\n");
		printf("       │ 3. 스케쥴 출력 │\n       │                │\n");
		printf("       │ 4. 스케쥴 검색 │\n       │                │\n");
		printf("       │ 5. 종료        │\n       │                │\n");
		printf("       └────────┘\n");
		printf("         → ");
		scanf("%d", &select);
		getchar();
		printf("\n");
		node = countNode(cur);//node(스케쥴)의 개수
		if(select == 1)
		{  
			addSchedule(cur);//스케쥴 추가  
		}
		else if(select == 2)
		{
			removeSchedule(cur);//스케쥴 제거
			writeFile(cur);//스케쥴 제거후 갱신된 현재 데이터를 텍스트파일에 출력(쓰기)   
		}
		else if(select == 3)
		{
			printSchedule(cur);//스케쥴 출력
		}else if(select ==4)
		{
			searchSchedule(cur);//스케쥴 검색
		}
		else if(select ==5)
		{
			clearMemory(cur);//프로그램종료전 할당된 메모리를 제거시킨다.
			exit(1);
		}
		else{
			printf(" ERROR !!\n");
			continue;
		}
	}
}
Exemple #18
0
int countNode(Node *r)
{

  int numNodes = 1;

  if(r == NULL) return 0;

  Node *p = r->leftMostChild;

  while(p != NULL){
    
    numNodes += countNode(p);
    p = p->rightSibling;

  }//end while

  return numNodes;

}//end countNode
void printSchedule(struct month *cur)//스케쥴을 출력
{
	int node;//스케쥴의 개수
	node = countNode(cur);
	if(node != 0)
	{
		printf(" ▩   스케쥴 출력   ▩\n");
		while (cur != NULL) 
		{
			if(cur->next !=NULL){    
				printf("     날짜 : %d월 %d일\n     대상 : %s\n     장소 : %s\n     내용 : %s\n", cur->month, cur->day, cur->name, cur->place, cur->content);
				printf(" ▩                 ▩\n\n");
			}
			else{    
				printf("     날짜 : %d월 %d일\n     대상 : %s\n     장소 : %s\n     내용 : %s\n", cur->month, cur->day, cur->name, cur->place, cur->content);
				printf(" ▩                 ▩\n\n");
			}
			cur = cur->next;     
		}
	}else
	{
		printf(" 스케쥴이 없습니다.\n");
	}
}
Exemple #20
0
 bool run()
 {
     ASSERT(m_graph.m_form == ThreadedCPS || m_graph.m_form == SSA);
     
     // First reset the counts to 0 for all nodes.
     for (BlockIndex blockIndex = 0; blockIndex < m_graph.numBlocks(); ++blockIndex) {
         BasicBlock* block = m_graph.block(blockIndex);
         if (!block)
             continue;
         for (unsigned indexInBlock = block->size(); indexInBlock--;)
             block->at(indexInBlock)->setRefCount(0);
         for (unsigned phiIndex = block->phis.size(); phiIndex--;)
             block->phis[phiIndex]->setRefCount(0);
     }
 
     // Now find the roots:
     // - Nodes that are must-generate.
     // - Nodes that are reachable from type checks.
     // Set their ref counts to 1 and put them on the worklist.
     for (BlockIndex blockIndex = 0; blockIndex < m_graph.numBlocks(); ++blockIndex) {
         BasicBlock* block = m_graph.block(blockIndex);
         if (!block)
             continue;
         for (unsigned indexInBlock = block->size(); indexInBlock--;) {
             Node* node = block->at(indexInBlock);
             DFG_NODE_DO_TO_CHILDREN(m_graph, node, findTypeCheckRoot);
             if (!(node->flags() & NodeMustGenerate))
                 continue;
             if (!node->postfixRef())
                 m_worklist.append(node);
         }
     }
     
     while (!m_worklist.isEmpty()) {
         while (!m_worklist.isEmpty()) {
             Node* node = m_worklist.last();
             m_worklist.removeLast();
             ASSERT(node->shouldGenerate()); // It should not be on the worklist unless it's ref'ed.
             DFG_NODE_DO_TO_CHILDREN(m_graph, node, countEdge);
         }
         
         if (m_graph.m_form == SSA) {
             // Find Phi->Upsilon edges, which are represented as meta-data in the
             // Upsilon.
             for (BlockIndex blockIndex = m_graph.numBlocks(); blockIndex--;) {
                 BasicBlock* block = m_graph.block(blockIndex);
                 if (!block)
                     continue;
                 for (unsigned nodeIndex = block->size(); nodeIndex--;) {
                     Node* node = block->at(nodeIndex);
                     if (node->op() != Upsilon)
                         continue;
                     if (node->shouldGenerate())
                         continue;
                     if (node->phi()->shouldGenerate())
                         countNode(node);
                 }
             }
         }
     }
     
     if (m_graph.m_form == SSA) {
         // Need to process the graph in reverse DFS order, so that we get to the uses
         // of a node before we get to the node itself.
         Vector<BasicBlock*> depthFirst;
         m_graph.getBlocksInDepthFirstOrder(depthFirst);
         for (unsigned i = depthFirst.size(); i--;)
             fixupBlock(depthFirst[i]);
     } else {
         RELEASE_ASSERT(m_graph.m_form == ThreadedCPS);
         
         for (BlockIndex blockIndex = 0; blockIndex < m_graph.numBlocks(); ++blockIndex)
             fixupBlock(m_graph.block(blockIndex));
         
         cleanVariables(m_graph.m_arguments);
     }
     
     m_graph.m_refCountState = ExactRefCount;
     
     return true;
 }
 int kthSmallest(TreeNode* root, int k) {
     int count = countNode(root->left);
     if (k <= count) return kthSmallest(root->left, k);
     else if (k > count + 1) return kthSmallest(root->right, k - count - 1);
     return root->val;
 }
void removeSchedule(struct month *cur)//스케쥴 제거
{
	struct month *head = NULL;
	struct month *temp = NULL;
	int node;
	int month;
	int day;
	head = cur;
	node = countNode(cur);//스케쥴 개수
 
	if(node == 0)
	{
		printf(" 제거할 스케쥴이 없습니다.\n");
	}else
	{
		int prevent = 0;//제거의 한번 실행을 위한 변수
		printf(" 월 → ");
		scanf("%d",&month);
		getchar();
		printf(" 일 → ");
		scanf("%d",&day);
		getchar();
 
		node = countNode(cur);
		if(prevent == 0 && node == 1)//스케쥴이 1개일 때
		{
			if(cur->month == month && cur->day == day)
			{
				cur->month = 0;
				cur->day = 0;
				strcpy(cur->name, "");
				strcpy(cur->place, "");
				strcpy(cur->content, "");
				cur->next = NULL;    
			}
			prevent++;
		}
		//제거될 달이 스케쥴 node의 첫번째일 때
		if((prevent == 0) && node == 2 && (cur->month == month) && (cur->day == day))//스케쥴이 2개이고 첫번째 노드값과 같을 때
		{
			temp = cur->next;
			cur->month = temp->month;
			cur->day = temp->day;
			strcpy(cur->name, temp->name);
			strcpy(cur->place, temp->place);
			strcpy(cur->content, temp->content);
			cur->next = NULL;
			free(temp);
			prevent++;
		}
		if((prevent == 0) && node >2 && (cur->month == month) && (cur->day == day))//스케쥴 세 개이상이고 첫번째 노드랑 같을 때
		{
			temp = cur;//cur의 전 node를 temp라고 하자.   
			while(cur->next != NULL)
			{
				int month,day;
				char place[50], name[50], content[50];
				month = cur->month;
				day = cur->day;
				strcpy(place, cur->place);
				strcpy(name, cur->name);
				strcpy(content, cur->content);
 
				cur->month = cur->next->month;
				cur->day = cur->next->day;
				strcpy(cur->name, cur->next->name);
				strcpy(cur->place, cur->next->place);
				strcpy(cur->content, cur->next->content);
 
				cur->next->month = month;
				cur->next->day = day;
				strcpy(cur->next->name, name);
				strcpy(cur->next->place, place);
				strcpy(cur->next->content, content);
				temp = cur;
				cur = cur->next;
			}
			temp->next = NULL;
			free(cur);
			prevent++;
		}
		if((node > 1) && (prevent == 0))//마지막 스케쥴 node의 값이 지워질 때
		{
			temp = cur->next;
			while(temp->next != NULL)
			{
				temp = temp->next;
				cur = cur->next;
			}
 
			if(temp->month == month && temp->day == day)
			{
				prevent++;
				free(temp);
				cur->next = NULL;
			}
			cur = head;
		}  
		//중간의 스케쥴 노드를 제거시킬때, cur의 다음노드를 가르키는 포인터노드가 필요하다.
		if((node > 1) && (prevent == 0))
		{
			cur = head;
			temp = cur->next;
			while(temp->next != NULL)
			{
				if(temp->month == month && temp->day == day)
				{
					cur->next = temp->next;
					free(temp);
					break;
				}
				cur = cur->next;
				temp = temp->next;
			}
			cur = head;
		}
	}
}
void searchSchedule(struct month *cur)
{
	char search[50];//찾을 문자열
	struct month *head = NULL;
	int n;//스케쥴의 개수
	int button=0;
	int time=0;
	head = cur;
	n = countNode(cur);
	if(n != 0)
	{
		printf(" 검색\n → 1. 날짜\n → 2. 문자열\n → ");
		scanf("%d",&button);
		getchar();
		if(button==1){
			printf(" 찾을 날짜 → ");
			scanf("%d", &time);
			getchar();
			while(cur != NULL)
			{
				if(cur->month == time)
				{
					printf("\n 월이 같습니다.\n");
					printf(" ▩   스케쥴 출력   ▩\n");
					printf("     날짜 : %d월 %d일\n     대상 : %s\n     장소 : %s\n     내용 : %s\n", cur->month, cur->day, cur->name, cur->place, cur->content);
					printf(" ▩                 ▩\n\n");
				}
				if(cur->day == time)
				{
					printf("\n 일이 같습니다.\n");
					printf(" ▩   스케쥴 출력   ▩\n");
					printf("     날짜 : %d월 %d일\n     대상 : %s\n     장소 : %s\n     내용 : %s\n", cur->month, cur->day, cur->name, cur->place, cur->content);
					printf(" ▩                 ▩\n\n");
				}
				cur = cur->next;
			}}
		else if(button==2){
			printf(" 찾을 문자열 → ");
			scanf("%s", search);
			getchar();
			while(cur != NULL)
			{
				if(strcmp(search, cur->content) == 0)
				{
					printf("\n 내용이 같습니다.\n");
					printf(" ▩   스케쥴 출력   ▩\n");
					printf("     날짜 : %d월 %d일\n     대상 : %s\n     장소 : %s\n     내용 : %s\n", cur->month, cur->day, cur->name, cur->place, cur->content);
					printf(" ▩                 ▩\n\n");
				}
				if(strcmp(search, cur->place) == 0)
				{
					printf("\n 장소가 같습니다.\n");
					printf(" ▩   스케쥴 출력   ▩\n");
					printf("     날짜 : %d월 %d일\n     대상 : %s\n     장소 : %s\n     내용 : %s\n", cur->month, cur->day, cur->name, cur->place, cur->content);
					printf(" ▩                 ▩\n\n");
				}
				if(strcmp(search, cur->name) == 0)
				{
					printf("\n 이름이 같습니다.\n");
					printf(" ▩   스케쥴 출력   ▩\n");
					printf("     날짜 : %d월 %d일\n     대상 : %s\n     장소 : %s\n     내용 : %s\n", cur->month, cur->day, cur->name, cur->place, cur->content);
					printf(" ▩                 ▩\n\n");
				}
				cur = cur->next;
			}}
		cur = head;
	}else
	{
		printf(" 찾으시는 문자열이 없습니다!\n");
	}
}
void input(struct month *cur)//스케쥴 입력
{
	int select=0;
	int n;
	int month, day;
	char place[50], name[50], content[50];
	struct month *newNode;//마지막노드라고가정하자
	struct month *head;
	printf(" ▩   스케쥴 입력   ▩\n");
	do
	{
		n = countNode(cur);//node의 개수
		if(n == 0)
		{
			int lastday;//해당 월의 마지막일
			printf("    월   : ");
			scanf("%d", &month);
			getchar();
			if(!(month > 0 && month < 13))
			{
				printf("Error : month의 범위를 초과합니다. 다시 입력하세요.\n");
				printf("    월   : ");
				scanf("%d", &month);
				getchar();
			}
			lastday = lastdays[month-1];
			printf("    일   : ");
			scanf("%d", &day);
			getchar();
			if(!(day > 0 && day < lastday))
			{
				printf("Error : %d월은 %d일까지 있습니다. 다시 입력하세요.\n", month, lastday);
				printf("    일   : ");
				scanf("%d", &month);
				getchar();
			}
			printf("    이름 : ");
			scanf("%s", name);
			getchar();
			printf("    장소 : ");
			scanf("%s", place);
			getchar();
			printf("    내용 : ");
			scanf("%s", content);
			getchar();
			cur->month = month;
			cur->day = day;
			strcpy(cur->name, name);
			strcpy(cur->place, place);
			strcpy(cur->content, content);
			n++;
			printf(" ▩                 ▩\n\n");
		}else
		{
			int lastday;//해당 월의 마지막일
			newNode = (struct month *)malloc(sizeof(struct month));		
			printf("    월   : ");
			scanf("%d", &month);
			getchar();
			if(!(month > 0 && month < 13))
			{
				printf("Error : month의 범위를 초과합니다. 다시 입력하세요.\n");
				printf("    월   : ");
				scanf("%d", &month);
				getchar();
			}
			lastday = lastdays[month-1];
			printf("    일   : ");
			scanf("%d", &day);
			getchar();
			if(!(day > 0 && day < lastday))
			{
				printf("Error : %d월은 %d일까지 있습니다. 다시 입력하세요.\n", month, lastday);
				printf("    일   : ");
				scanf("%d", &month);
				getchar();
			}
			printf("    이름 : ");
			scanf("%s", name);
			getchar();
			printf("    장소 : ");
			scanf("%s", place);
			getchar();
			printf("    내용 : ");
			scanf("%s", content);
			getchar();
			printf(" ▩                 ▩\n\n");
 
			newNode->month = month;
			newNode->day = day;
			strcpy(newNode->name, name);
			strcpy(newNode->place, place);
			strcpy(newNode->content, content);   
			cur->next = newNode;
			newNode->next = NULL;
			cur = newNode;
		}
		printf(" 스케쥴 추가 1, 취소 0 → ");
		scanf("%d", &select);
		getchar();
	}while(select == 1);
}
void insertion(struct month *cur)//스케쥴 삽입
{
	int month, day;
	char place[50], name[50], content[50];
	struct month *newNode;//삽입할 데이터를 갖을 구조체(node)
	struct month *head = NULL;
	int node;
	int lastday;//해당 월의 마지막일
	head = cur;
	newNode = (struct month *)malloc(sizeof(struct month));
	newNode->month = 0;
	newNode->next = NULL;
	//새롭게 삽입할 구조체를 현재 Linked List 연결된 마지막 구조체에 연결시킨다. 연결 후 정렬함수를 통해 크기 순으로 정렬
	while(cur->next != NULL)//Linked List의 마지막 구조체로 이동
	{
		cur = cur->next;
	}
	printf(" ▩   스케쥴 입력   ▩\n");   
	printf("    월   : ");
	scanf("%d", &month);
	getchar();
	if(!(month > 0 && month < 13))
	{
		printf("Error : month의 범위를 초과합니다. 다시 입력하세요.\n");
		printf("    월   : ");
		scanf("%d", &month);
		getchar();
	}
	lastday = lastdays[month-1];
	printf("    일   : ");
	scanf("%d", &day);
	getchar();
	if(!(day > 0 && day < lastday))
	{
		printf("Error : %d월은 %d일까지 있습니다. 다시 입력하세요.\n", month, lastday);
		printf("    일   : ");
		scanf("%d", &month);
		getchar();
	}
	printf("    이름 : ");
	scanf("%s", name);
	getchar();
	printf("    장소 : ");
	scanf("%s", place);
	getchar();
	printf("    내용 : ");
	scanf("%s", content);
	getchar();
	printf(" ▩                 ▩\n\n");
 
	//삽입할 구조체를 Linked List의 마지막 고리에 연결
	newNode->month = month;
	newNode->day = day;
	strcpy(newNode->content, content);
	strcpy(newNode->place, place);
	strcpy(newNode->name, name);
	newNode->next = NULL;
	cur->next = newNode;
	cur = cur->next;
	cur = head;
	node = countNode(cur);//스케쥴의 개수
	if(node > 1)//스케쥴의 개수가 2개이상이면 크기순으로 정렬
	{
		sort(cur);
	}
	//파일에 순서대로 현재 프로그램의 구조체 데이터들을 출력(쓰기)한다.
	writeFile(cur); 
}
Exemple #26
0
int countNode (TreeNode * root)
{
	if (root == NULL) return 0;
	return 1 + countNode(root -> left) + countNode(root -> right);
}