알고리즘을 C언어로 변환했느뎅 에러가 발생해서 그런데요 어디가 문제인건가요?
이송이
질문 제목 : 원형연결리스트 알고리즘을 c언어로 변환했느뎅 에러가 발생해서 그런데요 어디가 문제인건가요?
그리고 메인에 멀 넣어야되는지좀 알려주세요 ㅜㅜ
에러좀 찾아주세요
질문 내용 :
원형 연결리스트를 모의실행
alg candle(n, k)
input integer n, k
output integer
1. l ← buildlist(n)
2. return runsimulation(l, n, k)
alg buildlist(n)
1. p ← getnode()
2. l ← p
3. p.elem ← 1
4. for i ← 2 to n
p.next ← getnode()
p ← p.next
p.elem ← i
5. p.next ← l
6. return l
alg runsimulation(l, n, k)
input circularly linked list l, integer n, k
output integer
1. p ← l
2. while(p≠p.next)
for i ← 1 to k - 1
p ← p.next
remove(p.next)
3. return p.elem
이 알고리즘을 변환했는뎅 에러가 발생하네요 무슨 문제인건가요 ...
#includestido.h
#includestring.h
typedef struct candle //구조체 선언, candle 형으로 다시 재정의
{
int elem;
candle *next;
}candle;
candle *l=null; //시작 포인터를 null로 리셋
candle *t=null;
candle * buildlist(int n)
{
int i;
candle *p=null;
p=(candle *)malloc(sizeof(candle));
l=p;
p-elem=1;
for(i=2;i=n;i++)
{
p-next=(candle *)malloc(sizeof(candle));
p=p-next;
p-elem=i;
}
t=p;
p-next=l;
}
int runsimulation(int n) // n : 총 촛불의 갯수.
{
int i;
int k;
candle *p=l;
candle *t=null;
candle *a=null; //임시 저장 변수 하나 선언.
while(p!=p-next)
{
for(i=0;ik%n;i++)
{
p=p-next;
t=p;
}
t-next=p-next;
p-next=null;
a=p;
free(a); // p 제거
p=p-next; //p는 다음 촛불 t는 삭제한 촛불 이전 촛불을 가리킨다.
n--; //그리고 n은 하나 감소!
}
return p-elem;
}
int main(void){return 0;
}