반응형
// 단순 연결 리스트  
#include<stdio.h>
#include<stdlib.h>
 
typedef int element; 

typedef struct ListNode
{ 
  element data; 
  struct ListNode *link; 
}ListNode; 
 
void error(char *message){ //오류처리함수 
	fprintf(stderr,"%s\n",message);
	exit(1);
}
 
void insert_node(ListNode **phead, ListNode *p/*null */, ListNode *new_node){ //노드에 삽입 
// ** = 이중포인터- 주소의 값을 값으로 하는 연결리스트(두번 찾아감) 
// * 한개만 쓰면 밑에 함수호출에서 이상한 현상 생김 
	if(*phead == NULL){
		//연결 되지 않음  
		new_node ->link = NULL; 
		*phead=new_node; //*phead = 주소, new_mode를 *phead에 삽입 
	}
	else if(p == NULL){  
		new_node ->link = *phead; //new_node의 link를 *phead에 보관 
		*phead=new_node; ] 
	}
	else{
		new_node ->link = p->link; // p의 link를 찾아와서 연결 
		p->link=new_node;
	}
}
 
void remove_node(ListNode **phead, ListNode *p, ListNode *removed){  //노드 삭제  
  if(p==NULL) 
  	*phead=(*phead)->link; 
  else 
    p->link=removed->link; 
  free(removed); 
}
 
void display(ListNode *head){ // 리스트 출력함수 
	ListNode *p=head; 
	while(p!=NULL){ //null이 나올때까지 출력하고 따라가는 거 반복 
		printf("%d->", p->data);
		p = p->link;
	}                                              
	printf("\n"); //데이터가 순서대로 출력  
}
 
ListNode *search(ListNode *head, int x){ //리스트 탐색 함수
	ListNode *p;
	p=head;
	while(p!=NULL)
	{
		if(p->data == x)return p;
		p=p->link;
	}
	return p;
}
ListNode *concat(ListNode *head1, ListNode *head2){ //리스트 합병함수 
	ListNode *p;
	if(head1==NULL)return head2;
	else if(head2==NULL)return head1;
	else
	{
		p=head1;
		while(p->link!=NULL){
			p = p->link;
		}
		p->link=head2;
		return head1;
	}
}
 
ListNode *reverse(ListNode *head){ //리스트 역함수 
	ListNode *p,*q,*r;
	p=head;
	p=NULL;
	while(p!=NULL){
		r=q;
		q=p;
		p=p->link;
		q->link = r;
	}
	return q;
}
ListNode *create_node(element data, ListNode *link){ //노드 생성함수 
	ListNode *new_node; // 한칸의 노드 생성
	new_node = (ListNode *)malloc(sizeof(ListNode));
    //자료 형태는 ListNdoe, ListNode *= 강제형 변환 
	//malloc = 메모리 할당 -new_node에 ListNode만큼의 ListNode *형을 할당
    if(new_node == NULL)error("메모장 할당 에러\n");//메모리가 꽉 차서 할당 안됨
	new_node->data = data; // data 와 data 다름 
	// -> = 개체의 세부내용을 가르킴 (특수문자 아님) 
	new_node->link = link;
	return new_node; 
}

int main()
{
	ListNode *list1 = NULL, *list2= NULL;
	ListNode *p;
	
	insert_node(&list1, NULL, create_node(10, NULL)); 
	insert_node(&list1, NULL, create_node(20, NULL));
	insert_node(&list1, NULL, create_node(30, NULL));
	display(list1);
	
	remove_node(&list1,NULL,list1);
	
	display(list1);
	
	insert_node(&list2, NULL, create_node(40, NULL));
	insert_node(&list2, NULL, create_node(50, NULL));
	insert_node(&list2, NULL, create_node(60, NULL));
	dispaly(list2);
	
	list1 = concat(list1, list2);
	display(list1);
	
	list1 = reverse(list1);
	display(list1);
	
	p = search(list1, 50);
	printf("Search : %d\n", p->data);
	
}
반응형

'2019~2020 > 자료구조' 카테고리의 다른 글

재귀 알고리즘을 사용한 이진 트리 순회  (0) 2019.06.11
n*n 배열에 저장하여 출력하기  (0) 2019.06.05
swap 함수  (0) 2019.05.21
포인터 제 1법칙 - 2  (0) 2019.05.20
포인터 제 1 법칙 - 1  (0) 2019.05.20
반응형
#include<stdio.h>
int n,d[32]; 
int main() 
{ 
  	int i=0; 
 	scanf("%d",&n); 

  	do{ 
  	d[i]=n%2; 
 	 n/=2; 
  	i++; 
}while(n); 
	
    while(i){ 
		i--; 
   		printf("%d",d[i]); 
} 
    printf("\n"); 
  } 
/* 
11 
1011 

-------------------------------- 
Process exited after 2.463 seconds with return value 10 
계속하려면 아무 키나 누르십시오 . . . 
*/

 

반응형

'C, C++' 카테고리의 다른 글

(c언어) 표준 출력 함수  (0) 2019.08.23
별삼각형 출력하기  (0) 2019.06.12
윤년구하기 프로그램  (0) 2019.04.03
scanf() 함수로 문자 읽기2  (0) 2019.03.29
반응형

 

#include<stdio.h>
int main()  
{  
  int n,i,cnt=0;  
  printf("수를 입력하세요 :" );  
  scanf("%d",&n); 
  for(i=1; i<=n; i++)  
  {  
  	if(n%i==0)  
  		cnt+=1; 
    	else if(cnt==n)  
  		printf("%d\n",i);  
  }   
  printf("%d\n의 약수의 개수는 %d\n", n, cnt);  
} 
반응형

'C, C++ > 프로그램' 카테고리의 다른 글

[c]사칙연산 계산기  (0) 2020.06.19
연봉 구하기 프로그램  (0) 2019.03.24
연산자  (0) 2019.03.24
최솟값과 최댓값 구하기  (0) 2019.03.24
Over flow  (0) 2019.03.24
반응형
include <stdio.h>
void printAB(int x, int y) 
{ 
  printf("변수 값을 순서대로 출력하면 %d, %d입니다.\n",x,y); 
} 

void swap(int a,int b) { 
  	int tmp; 
  	tmp = a; 
  	a = b; 
  	b = tmp; 
  } 

int main(){ 
    int a=2, b = 3; 
    printAB(a,b); 
    swap(a,b); 
    printAB(a,b); 
    return 0; 
} 
/* 
변수 값을 순서대로 출력하면 2, 3입니다. 
변수 값을 순서대로 출력하면 2, 3입니다. 

-------------------------------- 
Process exited after 0.0135 seconds with return value 0 
계속하려면 아무 키나 누르십시오 . . . 
*/
반응형

'2019~2020 > 자료구조' 카테고리의 다른 글

n*n 배열에 저장하여 출력하기  (0) 2019.06.05
list3  (0) 2019.05.28
포인터 제 1법칙 - 2  (0) 2019.05.20
포인터 제 1 법칙 - 1  (0) 2019.05.20
포인터 실습  (0) 2019.05.17
반응형
#include <stdio.h>

void printIntVar(char *name, int value){ 
  printf("%s\t = %d\n", name, value); 
} 

int main(){ 
    int n; 

    printf("숫자 n를 입력해 주세요 : "); 
    scanf("%d",&n); 
    printIntVar("n",n); 
    printIntVar("*&n",*&n); 
    printf("%d",&n); 
    return 0; 
} 
/* 

숫자 n를 입력해 주세요 : 275 
n        = 275 
*&n      = 275 
&n       = 6487580 

-------------------------------- 
Process exited after 4.217 seconds with return value 0 
계속하려면 아무 키나 누르십시오 . . . 
*/



반응형

'2019~2020 > 자료구조' 카테고리의 다른 글

list3  (0) 2019.05.28
swap 함수  (0) 2019.05.21
포인터 제 1 법칙 - 1  (0) 2019.05.20
포인터 실습  (0) 2019.05.17
List(리스트)  (0) 2019.05.17
반응형
#include<stdio.h>
void printIntVar(char *name, int value){ 
  printf("%s\t = %d\n", name, value); 
 } 
int main() 
{ 
  int one = 1; 
  int *to_one; 

  to_one = &one; 
  printIntVar("one", one); 
  *to_one = one + 1; 
  printIntVar("one", one); 
  *to_one = *to_one + 1; 
  printIntVar("one", one); 
  (*to_one)++; 
  printIntVar("one" ,one); 

  return 0; 
} 
 




반응형

'2019~2020 > 자료구조' 카테고리의 다른 글

swap 함수  (0) 2019.05.21
포인터 제 1법칙 - 2  (0) 2019.05.20
포인터 실습  (0) 2019.05.17
List(리스트)  (0) 2019.05.17
Queue  (0) 2019.05.13
반응형
#include<stdio.h> 
int main() 
{ 
  int num = 10; //정수형 변수 선언  
  int *p; // 정수형 포인터 선언  
  p = # // 포인터가 num 변수의 주소값을 가리킴  
  printf("%d\n",&num); //포인터가 가리키는 변수의 주소 출력  
  printf("%d\n",p); // 포인터가 가리키는 변수의 주소 출력  
  printf("%d\n",*p); // 포인터가 가리키는 변수의 값 출력  
  printf("%d\n",&p); // 포인터 변수의 주소 출력 (주소가 달라짐 6487580의 위치주소 값  
}
반응형

'2019~2020 > 자료구조' 카테고리의 다른 글

포인터 제 1법칙 - 2  (0) 2019.05.20
포인터 제 1 법칙 - 1  (0) 2019.05.20
List(리스트)  (0) 2019.05.17
Queue  (0) 2019.05.13
스택 함수  (0) 2019.04.09
반응형
typedef int element; 
typedef struct ListNode{ 
  element date; 
  struct ListNode *link; 
}ListNode; 
ListNode *pl; 
pl = (ListNode *)malloc( sizeof(ListNode)); 

void insert_node(ListNode *phead, ListNode *p,ListNode *new_node); 
{ 
  if(phead==NULL) 
  { 
    new_nod->link = NULL; 
    phead = new_node; 
  } 
  else if(p == NULL) 
  { 
    new_node->link = phead; 
    phead = new_node; 
  } 
  else 
  { 
    new_nod->link =p1->link:; 
    p1->link = new_node; 
  } 
} 

void remove_node(ListNode phead, ListNode *p, ListNode *removed); 
{ 
  if(p == NULL) 
  	phead = (phead)->link; 
  else 
    p->link = removed->link; 
  free(removed); 
}
반응형

'2019~2020 > 자료구조' 카테고리의 다른 글

포인터 제 1 법칙 - 1  (0) 2019.05.20
포인터 실습  (0) 2019.05.17
Queue  (0) 2019.05.13
스택 함수  (0) 2019.04.09
함수를 이용한 팩토리얼 실습  (0) 2019.04.08
반응형
#include <stdio.h>
#include <stdlib.h>
int main() 
{ 
  srand(time(NULL)); 
  int num1, num2, num3; 

  num1=rand()%10; 
  do{num2=rand()%10; }while(num2==num1); 
  do{num3=rand()%10; }while(num3==num2||num3==num1); 

  int cnt=0; 
  int strike, ball; 
do 
{ 
  if(cnt>=7)break; 
  	strike=0,ball=0; 

  int input; 
  scanf("%d", &input); 
  if(input>=1000||input<100) 
  { 
    printf("input3-digit numbers\n"); 
    continue; 
  } 

  int d1, d2, d3; 
  d1=input/100; 
  d2=(input%100)/10; 
  d3=input%10; 
  cnt++; 
  if(d1==d2||d2==d3||d1==d3) 
  { 
  printf("%d count : number overlap\n",cnt); 
  continue; 
  } 

  if(num1==d1)strike++; 
  	else if(num1==d2||num1==d3)ball+=1; 

  if(num2==d2)strike++; 
 	 else if(num2==d1||num2==d3)ball+=1; 

  if(num3==d3)strike++; 
  	else if(num3==d1||num3==d2)ball+=1; 

  printf("%d count : %d Strike, %d Ball\n", cnt, strike, ball); 
  }while(strike!=3); 

  if(cnt>=10) 
  	printf("Fall! 당신은 바보 입니까?  Answer Number : %d%d%d", num1, num2, num3); 
  else  
  	printf("SUCCESS! 당신은 천재..!! Try count : %d",cnt); 

  return 0; 
}
반응형

'2019~2020 > 정보 과학' 카테고리의 다른 글

단어 저장하기 프로그램  (0) 2019.05.29
문자개수 세기 프로그램  (0) 2019.05.29
소인수로 분해하기  (0) 2019.04.12
약수의 개수 계산 프로그램  (0) 2019.04.12
계절 출력하기  (0) 2019.04.10
반응형

 

#include<stdio.h>
#include<sidlib.h>
#define MAX_SIZE 10  

// 큐를 구조체로 선언  
typedef int element;  
typedef struct{  
  element queue[MAX_SIZE];  
  int front, rear;  
 } QueueType;  

// 포화 상태, 공백상태 error 처리  
void error(char *message){  
  fprintf(stderr,"%s\n",message);  
  exit(1);  
}   

// 초기화 함수  
void init(QueueType *q){                                                                                
  q->front = q->rear = 0;  
}    

// 삽입함수   
void enqueue(QueueType *q, element item){  
  if((q->rear+1)%MAX_SIZE == q->front)  
  	error("큐가 포화 상태입니다");  
  q->rear = (q->rear+1) % MAX_SIZE;  
  q->queue[q->rear] = item;  
}  

//삭제 함수  
element dequeue(QueueType *q){  
  if(q->front == q->rear)  
  	error("큐가 공백 상태입니다");  
  q->front = (q->front + 1)%MAX_SIZE;  
  return q->queue[q->front];  
}  
int main() {  
  QueueType q;  
  init(&q); //item이 queue인데 q안에 queue 안에 배열 item = q.queue[q.front]  
  printf("\nitem=%d\n",q.queue[q.front]);  
  printf("front=%d\n",q.front);  
  printf("rear=%d\n",q.rear);  
  enqueue(&q,99); 
  printf("\nitem=%d\n",q.queue[q.front+1]);  
  printf("front=%d\n",q.front);  
  printf("rear=%d\n",q.rear); 
  enqueue(&q,88); 
  enqueue(&q,77); 
  enqueue(&q,66); 
  printf("\nitem=%d\n",q.queue[q.rear]);  
  printf("front=%d\n",q.front);  
  printf("rear=%d\n",q.rear); 
  enqueue(&q,55); 
  enqueue(&q,44); 
  enqueue(&q,33); 
  enqueue(&q,22); 
  enqueue(&q,11); 
  printf("\nitem=%d\n",q.queue[q.rear]);  
  printf("front=%d\n",q.front);  
  printf("rear=%d\n",q.rear); 
  dequeue(&q); 
  printf("\nitem=%d\n",q.queue[q.front]);  
  printf("front=%d\n",q.front);  
  printf("rear=%d\n",q.rear); 
  enqueue(&q,98); 
  printf("\nitem=%d\n",q.queue[q.rear]);  
  printf("front=%d\n",q.front);  
  printf("rear=%d\n",q.rear); 
  dequeue(&q); 
  dequeue(&q); 
  dequeue(&q); 
  dequeue(&q); 
  dequeue(&q); 
  dequeue(&q); 
  dequeue(&q); 
  dequeue(&q); 
  dequeue(&q); 
  printf("\nitem=%d\n",q.queue[q.front]);  
  printf("front=%d\n",q.front);  
  printf("rear=%d\n",q.rear); 
} 
/* 

item=1 
front=0 
rear=0 

item=99 
front=0 
rear=1 

item=66 
front=0 
rear=4 

item=11 
front=0 
rear=9 

item=99 
front=1 
rear=9 

item=98 
front=1 
rear=0 

item=98 
front=0 
rear=0 

-------------------------------- 
Process exited after 0.02195 seconds with return value 7 
계속하려면 아무 키나 누르십시오 . . . 
*/
반응형

'2019~2020 > 자료구조' 카테고리의 다른 글

포인터 실습  (0) 2019.05.17
List(리스트)  (0) 2019.05.17
스택 함수  (0) 2019.04.09
함수를 이용한 팩토리얼 실습  (0) 2019.04.08
square 함수  (0) 2019.04.08

+ Recent posts