U E D R , A S I H C RSS

자료구족발보쌈/0708


1. 참여자 명단


함장 장용운 11학번 지각
선원 천준현 15학번 결석
최지혁 출석
박인서 출석
이정재 고향
이원준 출석
조종현 출석
남헌 숙면

2. 수업

2.1. 진행

1. 장소 : 6층 PC실
2. 시간 : 함장님 지각으로 인하여 14시 30분 ~ 17시

2.2. 내용

기억 속에서 사라진 기존 내용 복습
  • 구조체
  • 동적 메모리 할당
  • 연결 리스트

수심 1000m. 스택
  • 스택 만들기
  • 문제해결

3. 코드

3.1. 예제1

#include <stdio.h>
#include <malloc.h>

typedef struct _node{
	int value;
	struct _node * next;
} node;

void freeAll(node*);

int main(void) {
	node* head;
	head = (node*)malloc(sizeof(node));

	node* temp = head;
	for (int i = 0; i < 50; i++) {
		temp->next = (node*)malloc(sizeof node);
		temp->value = i;

		temp = temp->next;
	}
	temp->next = NULL;
	temp->value = 50;

	freeAll(head);

	return 0;
}

void freeAll(node* hd) {
	node* t;
	while (hd != NULL) {
		t = hd->next;
		printf("%d ", hd->value);
		free(hd);
		hd = t;
	}
}

3.2. 예제2

#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>

typedef struct _node{
	int value;
	struct _node* next;
} node;

void push(node**, int);
int pop(node**);

int main(void) {
	node* head = NULL;
	push(&head, 3);

	return 0;
}

void push(node** target, int val) {
	node* newnode = (node*)malloc(sizeof node);
	newnode->value = val;
	newnode->next = *target;
	
	*target = newnode;
}

int pop(node** target) {
	if (*target == NULL) abort();

	int res = (*target)->value;
	node* kill = *target;
	*target = (*target)->next;

	free(kill);
	return res;
}

3.3. 예제3

#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>

typedef struct _node{
	int value;
	struct _node* next;
} node;

void push(node*, int);
int pop(node*);

int main(void) {
	node head;
	head.next = NULL;
	push(&head, 3);
	printf("%d", pop(&head));

	return 0;
}

void push(node* target, int val) {
	node* newnode = (node*)malloc(sizeof node);
	newnode->value = val;
	newnode->next = target->next;
	
	target->next = newnode;
}

int pop(node* target) {
	if (target == NULL) abort();

	int res = target->next->value;
	node* kill = target->next;
	target->next = target->next->next;

	free(kill);
	return res;
}

4. 숙제

1. 스택 복습하기




5. 숙제 제출

5.1. 천준현


5.2. 최지혁


#include<stdio.h>
#include<malloc.h>

 //참조 자료형이 안에 있을 경우는 이렇게 위에 하나 더 써서 컴에게 미리 알려줌
typedef struct _node{
	int value;
	struct _node *next;
}node;

void freeall(node*);

int main(){
	node*head;
	head = (node*)malloc(sizeof(node));

	node* temp = head;//한번에 많이 생성,초기화
	for (int i = 0; i < 50; i++)
	{
		temp->next = (node*)malloc(sizeof node);
		temp->value = i;

		temp = temp->next;
	}
	temp->next = NULL;
	temp->value = 50;
	freeall(head);

	return 0;

}

void freeall(node* hd)
{
	node*t;
	while (hd != NULL)
	{
		t = hd->next;
		printf("%d ", hd->value);
		free(hd);
		hd = t;
	}
}


#include<stdio.h>
#include<stdlib.h>

typedef struct _node
{
	int value;
	struct _node*next;
} node;

void push(node**, int);
int pop(node*);

int main()
{
	node*head = NULL;
	push(&head, 3);

	return 0;
}

void push(node** target, int val)
{
	node*newnode = (node*)malloc(sizeof node);
	newnode->value = val;
	newnode->next = *target;

	*target = newnode;
}

int pop(node** target, int val)
{
	if (*target == NULL) abort(); //abort는 프로젝트 강제 종료

	int res = (*target)-> value;
	node*kill = *target;
	*target = (*target)->next;

	free(kill);
	return res;
}

5.3. 박인서

  • 연결 리스트

#include <stdio.h>
#include <stdlib.h>

typedef struct node{
	int value;
	struct node * next;
}node;

void freeall(node * hd)
{
	node * t=hd->next;
	while(hd!=NULL)
	{
		t=hd->next;
		printf("%d\n",hd->value);
		free(hd);
		hd=t;
	}
}

int main()
{
	node * head;
	node * temp;
	int i;
	head = (node *)malloc(sizeof(node));
	head->value=0;
	head->next=(node *)malloc(sizeof(node));
	temp=head;
	for(i=0;i<50;i++)
	{
		temp->next=(node *)malloc(sizeof(node));
		temp->value=i;
		temp=temp->next;
	}
	temp->next=NULL;
	temp->value=50;
	freeall(head);
	return 0;
}
  • 스택 1

#include <stdio.h>
#include <stdlib.h>

typedef struct node{
	int val;
	struct node* next;
}node;

void push(node **, int);
int pop(node **);

int main()
{
	node * head=NULL;
	push(&head,3);
	push(&head,4);
	printf("%d ",pop(&head));
	printf("%d ",pop(&head));
	return 0;
}

void push(node ** target, int val)
{
	node * newnode=(node *)malloc(sizeof(node));
	newnode->val=val;
	newnode->next=*target;
	*target=newnode;
}
int pop(node ** target)
{
	int res=(*target)->val;
	node * kill = *target;
	if(*target==NULL) abort();
	*target=(*target)->next;
	free(kill);
	return res;
}
  • 스택 2

#include <stdio.h>
#include <stdlib.h>

typedef struct node{
	int val;
	struct node* next;
}node;

void push(node *, int);
int pop(node *);

int main()
{
	node head;
	head.next=NULL;
	push(&head,1);
	push(&head,2);
	push(&head,3);
	push(&head,4);
	printf("%d ",pop(&head));
	printf("%d ",pop(&head));
	printf("%d ",pop(&head));
	printf("%d ",pop(&head));
	return 0;
}

void push(node * target, int val)
{
	node * newnode=(node *)malloc(sizeof(node));
	node * temp=target->next;
	newnode->val=val;
	target->next=newnode;
	newnode->next=temp;
}
int pop(node * target)
{
	int res=target->next->val;
	node * kill = target->next;
	if(target->next==NULL) abort();
	target->next=target->next->next;
	free(kill);
	return res;
}

5.4. 이정재


5.5. 이원준


5.5.1. 연결 리스트


 #include <stdio.h>

typedef struct node{
	int v;
	struct node* next;
}node;

void freeAll(node* hd); //값을 출력하면서 구조체를 free

int fNum(node* hd); //마지막 구조체의 값을 반환

void main(){
	node* head;
	head = (node*)malloc(sizeof(node));

	node* temp = head;

	for (int i = 0; i < 50; i++){
		temp->next = (node*)malloc(sizeof(node));

		temp->v = i;

		temp = temp->next;
	}

	temp->next = NULL;
	temp->v = 50;
	printf("%d\n", fNum(head));
	freeAll(head);
	printf("\n");

}

void freeAll(node* hd){
	node* ntp;
	while (hd != NULL){
		ntp = hd->next;
		printf("%d ", hd->v);
		free(hd);
		hd = ntp;
	}
}

int fNum(node* hd){
	int i;
	node* tp;
	while (hd != NULL){
		i = hd->v;
		hd = hd->next;
	}
	return i;
} 


5.5.2. 스택 1



#include <stdio.h>

typedef struct node{

	int v;
	struct node* next;

}node;

void push(node*, int);
int pop(node *);
void freeAll(node*);


void main(){

	node* head = NULL;
	push(&head, 3);
	push(&head, 4);
	push(&head, 9);
	printf("%d\n",pop(&head));
	printf("%d\n", pop(&head));
	printf("%d\n", pop(&head));

	freeAll(head);
}

void push(node** tar, int val){
	node* newn = (node*)malloc(sizeof(node));
	newn->v = val;
	newn->next = *tar;

	*tar = newn;
}

int pop(node** tar){
	if (*tar == NULL) abort();

	int res = (*tar)->v;
	node* kill = *tar;
	*tar = (*tar)->next;

	free(kill);
	return res;

}

void freeAll(node* hd){
	node* ntp;
	while (hd != NULL){
		ntp = hd->next;
		printf("%d ", hd->v);
		free(hd);
		hd = ntp;
	}
}


5.5.3. 스택2


#include<stdio.h>
#include<stdlib.h>



typedef struct node{

	int v;
	struct node* next;

}node;




void push(node*, int);
int pop(node*);
void freeAll(node*);




void main(){
	node head;
	head.next = NULL;
	push(&head, 1);
	push(&head, 2);
	push(&head, 3);
	push(&head, 4);
	printf("%d\n",pop(&head));
	printf("%d\n", pop(&head));
	printf("---freeAll----\n");
	freeAll(&head);
	printf("\n");
}




void push(node* hd, int val){
	node* tmp = (node*)malloc(sizeof(node));
	
	tmp->next = hd->next;
	tmp->v = val;
	hd->next = tmp;

}




int pop(node* hd){
	if (hd->next != NULL) printf("없는데 뽑다니!!! 프로그램끌거임\n"); abort;

	node* tmp = (node*)malloc(sizeof(node));
	int val;

	tmp = hd->next;
	val = tmp->v;
	hd->next = tmp->next;
	free(tmp);

	return val;

}



void freeAll(node* hd){

	node* ntp;

	while (hd->next != NULL){
		ntp = hd->next;
		printf("%d ", ntp->v);
		ntp = ntp->next;
		free(hd->next);
		hd->next = ntp;
	}

}

5.6. 조종현


Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:30:37
Processing time 0.0216 sec