Toggle menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

자료구족발보쌈/0908: Difference between revisions

From ZeroWiki
imported>박인서
No edit summary
imported>박인서
No edit summary
Line 62: Line 62:


== 박인서 ==
== 박인서 ==
 
//고통받기를 시작합니다.
//고통받기를 시작합니다.
#include <stdio.h>
#include <stdio.h>
#include &lt;stdlib.h&gt;
#include <stdlib.h>
#include &lt;string.h&gt;
#include <string.h>
 
typedef struct node{
typedef struct node{
int val;
int val;
struct node* lnext;
struct node* lnext;
struct node* rnext;
struct node* rnext;
}node;
}node;
 
void push(node *, int);
void push(node *, int);
int pop(node *);
int pop(node *);
int find(node * ,int);
int find(node * ,int);
long cnt=1;
long cnt=1;
 
int main()
int main()
{
{
int a;
int a;
char ch[5];
char ch&#91;5&#93;;
node head;
node head;
printf("초기값 설정 ㄱㄱ\n");
printf("초기값 설정 ㄱㄱ\n");
scanf("%d",&amp;head.val);
scanf("%d",&head.val);
head.lnext=NULL;
head.lnext=NULL;
head.rnext=NULL;
head.rnext=NULL;
for(;;)
for(;;)
{
{
printf("push나 pop이나 exit 입력(push/pop/exit)\n");
printf("push나 pop이나 exit 입력(push/pop/exit)\n");
scanf("%s",ch);
scanf("%s",ch);
if(ch[0]=='e' &amp;&amp; ch[1]=='x' &amp;&amp; ch[2]=='i' &amp;&amp; ch[3]=='t') break;
if(ch&#91;0&#93;=='e' && ch&#91;1&#93;=='x' && ch&#91;2&#93;=='i' && ch&#91;3&#93;=='t') break;
else if(ch[0]=='p' &amp;&amp; ch[1]=='u' &amp;&amp; ch[2]=='s' &amp;&amp; ch[3]=='h')
else if(ch&#91;0&#93;=='p' && ch&#91;1&#93;=='u' && ch&#91;2&#93;=='s' && ch&#91;3&#93;=='h')
{
{
printf("넣을 숫자 입력 ㄱㄱ(자연수만) : ");
printf("넣을 숫자 입력 ㄱㄱ(자연수만) : ");
scanf("%d",&amp;a);
scanf("%d",&a);
push(&amp;head,a);
push(&head,a);
}
}
else if(ch[0]=='p' &amp;&amp; ch[1]=='o' &amp;&amp; ch[2]=='p')
else if(ch&#91;0&#93;=='p' && ch&#91;1&#93;=='o' && ch&#91;2&#93;=='p')
{
{
a=pop(&amp;head);
a=pop(&head);
if(a==-1) printf("숫자 없음 ㅂㅅ아\n");
if(a==-1) printf("숫자 없음 ㅂㅅ아\n");
else printf("%d가 있었다 ㅂㅅ아\n",a);
else printf("%d가 있었다 ㅂㅅ아\n",a);
}
}
else
else
{
{
printf("push나 pop이나 exit만 입력하라고...\n");
printf("push나 pop이나 exit만 입력하라고...\n");
}
}
}
}
return 0;
return 0;
}
}
 
void push(node * target, int newval)
void push(node * target, int newval)
{
{
node * newnode=(node *)malloc(sizeof(node));
node * newnode=(node *)malloc(sizeof(node));
int a=1,c,i;
int a=1,c,i;
int * b;
int * b;
cnt++;
cnt++;
for(c=1;;c++)
for(c=1;;c++)
{
{
if(a&lt;=cnt &amp;&amp; cnt&lt;2*a) break;
if(a<=cnt && cnt<2*a) break;
a*=2;
a*=2;
}
}
a=cnt;
a=cnt;
b=(int *)malloc((sizeof(int))*c);
b=(int *)malloc((sizeof(int))*c);
for(i=0;i&lt;c;i++)
for(i=0;i<c;i++)
{
{
if(a%2==0) b[i]=0;
if(a%2==0) b&#91;i&#93;=0;
else b[i]=1;
else b&#91;i&#93;=1;
a/=2;
a/=2;
}
}
for(i=c-2;i&gt;=1;i--)
for(i=c-2;i>=1;i--)
{
{
if(b[i]==0)
if(b&#91;i&#93;==0)
target=target-&gt;lnext;
target=target->lnext;
else
else
target=target-&gt;rnext;
target=target->rnext;
}
}
newnode-&gt;val=newval;
newnode->val=newval;
newnode-&gt;lnext=NULL;
newnode->lnext=NULL;
newnode-&gt;rnext=NULL;
newnode->rnext=NULL;
if(b[0]==0) target-&gt;lnext=newnode;
if(b&#91;0&#93;==0) target->lnext=newnode;
else target-&gt;rnext=newnode;
else target->rnext=newnode;
free(b);
free(b);
}
}
 
int pop(node * target)
int pop(node * target)
{
{
int a=1,c,i;
int a=1,c,i;
int * b;
int * b;
if(cnt==0) return -1;
if(cnt==0) return -1;
if(cnt==1)
if(cnt==1)
{
{
cnt--;
cnt--;
return target-&gt;val;
return target->val;
}
}
for(c=1;;c++)
for(c=1;;c++)
{
{
if(a&lt;=cnt &amp;&amp; cnt&lt;2*a) break;
if(a<=cnt && cnt<2*a) break;
a*=2;
a*=2;
}
}
a=cnt;
a=cnt;
b=(int *)malloc((sizeof(int))*c);
b=(int *)malloc((sizeof(int))*c);
for(i=0;i&lt;c;i++)
for(i=0;i<c;i++)
{
{
if(a%2==0) b[i]=0;
if(a%2==0) b&#91;i&#93;=0;
else b[i]=1;
else b&#91;i&#93;=1;
a/=2;
a/=2;
}
}
for(i=c-2;i&gt;=1;i--)
for(i=c-2;i>=1;i--)
{
{
if(b[i]==0)
if(b&#91;i&#93;==0)
target=target-&gt;lnext;
target=target->lnext;
else
else
target=target-&gt;rnext;
target=target->rnext;
}
}
if(b[0]==0)
if(b&#91;0&#93;==0)
{
{
a=target-&gt;lnext-&gt;val;
a=target->lnext->val;
free(target-&gt;lnext);
free(target->lnext);
target-&gt;lnext=NULL;
target->lnext=NULL;
}
}
else
else
{
{
a=target-&gt;rnext-&gt;val;
a=target->rnext->val;
free(target-&gt;rnext);
free(target->rnext);
target-&gt;rnext=NULL;
target->rnext=NULL;
}
}
free(b);
free(b);
cnt--;
cnt--;
return a;
return a;
}
}
== ~~이정재~~ ==
== ~~이정재~~ ==



Revision as of 06:01, 15 September 2015

  2학기가 밝았습니다. 모두들 고통 받을 준비 하세요.

참여자 명단

함장 장용운 11학번 출석
선원 천준현 15학번 출석
최지혁 집감
박인서 출석
~~이정재~~ ~~탈주~~
이원준 출석
조종현 공강
남헌 출석

수업

진행

  1. 장소 : 6층 학회실
  2. 시간 : 15시~17시

내용

수심 3000m. 트리

  • 완전 이진 트리 만들기(push, pop)

~~수심이 계속 안 내려가는 것처럼 보이는 것은 기분 탓입니다.~~


코드

예제1

숙제

  1. 완성하지 못한 완전 이진 트리 완성해오기


숙제 제출

천준현

최지혁

박인서

//고통받기를 시작합니다.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct node{
	int val;
	struct node* lnext;
	struct node* rnext;
}node;

void push(node *, int);
int pop(node *);
int find(node * ,int);
long cnt=1;

int main()
{
	int a;
	char ch[5];
	node head;
	printf("초기값 설정 ㄱㄱ\n");
	scanf("%d",&head.val);
	head.lnext=NULL;
	head.rnext=NULL;
	for(;;)
	{
		printf("push나 pop이나 exit 입력(push/pop/exit)\n");
		scanf("%s",ch);
		if(ch[0]=='e' && ch[1]=='x' && ch[2]=='i' && ch[3]=='t') break;
		else if(ch[0]=='p' && ch[1]=='u' && ch[2]=='s' && ch[3]=='h')
		{
			printf("넣을 숫자 입력 ㄱㄱ(자연수만) : ");
			scanf("%d",&a);
			push(&head,a);
		}
		else if(ch[0]=='p' && ch[1]=='o' && ch[2]=='p')
		{
			a=pop(&head);
			if(a==-1) printf("숫자 없음 ㅂㅅ아\n");
			else printf("%d가 있었다 ㅂㅅ아\n",a);
		}
		else
		{
			printf("push나 pop이나 exit만 입력하라고...\n");
		}
	}
	return 0;
}

void push(node * target, int newval)
{
	node * newnode=(node *)malloc(sizeof(node));
	int a=1,c,i;
	int * b;
	cnt++;
	for(c=1;;c++)
	{
		if(a<=cnt && cnt<2*a) break;
		a*=2;
	}
	a=cnt;
	b=(int *)malloc((sizeof(int))*c);
	for(i=0;i<c;i++)
	{
		if(a%2==0) b[i]=0;
		else b[i]=1;
		a/=2;
	}
	for(i=c-2;i>=1;i--)
	{
		if(b[i]==0)
			target=target->lnext;
		else
			target=target->rnext;
	}
	newnode->val=newval;
	newnode->lnext=NULL;
	newnode->rnext=NULL;
	if(b[0]==0) target->lnext=newnode;
	else target->rnext=newnode;
	free(b);
}

int pop(node * target)
{
	int a=1,c,i;
	int * b;
	if(cnt==0) return -1;
	if(cnt==1)
	{
		cnt--;
		return target->val;
	}
	for(c=1;;c++)
	{
		if(a<=cnt && cnt<2*a) break;
		a*=2;
	}
	a=cnt;
	b=(int *)malloc((sizeof(int))*c);
	for(i=0;i<c;i++)
	{
		if(a%2==0) b[i]=0;
		else b[i]=1;
		a/=2;
	}
	for(i=c-2;i>=1;i--)
	{
		if(b[i]==0)
			target=target->lnext;
		else
			target=target->rnext;
	}
	if(b[0]==0)
	{
		a=target->lnext->val;
		free(target->lnext);
		target->lnext=NULL;
	}
	else
	{
		a=target->rnext->val;
		free(target->rnext);
		target->rnext=NULL;
	}
	free(b);
	cnt--;
	return a;
}

~~이정재~~

이원준

조종현

남헌



활동지도/2015 자료구족발보쌈