반응형
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// pointer.cpp: 콘솔 응용 프로그램의 진입점을 정의합니다.
//
 
#include "stdafx.h"
void swap(int a, int b) {
    int t;
    t = a; a = b; b = t;
}
void swap_point(int *a, int*b) {
    int t;
    t = *a; *= *b; *= t;
}
// 배열과 포인터 이해
int main()
{
    int A[100= { 5,6,7,8 };
 
    printf("%d\n"*A);
    printf("%d\n"*(A+1));
    printf("%d\n"*+11);
    printf("\n");
    int *p;
    p = A;
    
    printf("%d\n"*p);
    printf("%d\n"*(p+1));
    printf("%d\n"*(p + 2));
    printf("%d\n"*(p + 3));
    printf("%d\n"*(++p));
    printf("\n");
    // & 기호 + 변수 A == > 변수 A 의 위치
    int a = 10
    int *p1 = &a;
    printf("%d\n"*p1);
    printf("%d\n"&a);
 
    //포인터 사용
    int i=50, j=70;
    swap(i, j);
    printf("%d %d\n", i, j);
    swap_point(&i, &j);
    printf("%d %d\n", i, j);
 
    return 0;
}
 
 
cs

 

반응형

'Study > C' 카테고리의 다른 글

C언어 - 자연수의 조합  (0) 2018.09.14
C언어 - 자판기  (0) 2018.09.14
C언어 - EOF  (0) 2018.09.07
C언어 - 학생 Table(총점, 평균, 석차)  (0) 2018.09.07
C언어 - 탐색, 정렬, 순위  (0) 2018.09.07
반응형

/* 출력결과

1 2 3 4 5

6 7 8 9 10

11 12 13 14 15

16 17 18 19 20

21 22 23 24 25

*/

 

#include "stdafx.h"
#include <stdio.h>

 

int main()
{

 int a[5][5] = { 0 };
 int cnt = 1;

 for (int i = 1; i < 6; i++) {
  for (int j = 1; j < 6; j++) {
   a[i-1][j-1] = cnt++;
   printf("%3d", a[i - 1][j - 1]);
   }
  printf("\n");
 }

 


 return 0;
}

****************************************************************************************************************************************

 

 

/*

1 1 1 1 1

2 2 2 2 2

3 3 3 3 3

4 4 4 4 4

5 5 5 5 5

*/


#include "stdafx.h"
#include <stdio.h>


int main()
{
 int a[5][5] = { 0 };
 
 for (int i = 1; i < 6; i++) {
   for (int j = 1; j < 6; j++) {
    a[i - 1][j - 1] = i;
    printf("%d ", a[i - 1][j - 1]);
   }
   printf("\n");
 }
 
   
 
    return 0;
}

 

****************************************************************************************************************************************

 

 

 

/*

1 2 3 4 5

1 2 3 4 5

1 2 3 4 5

1 2 3 4 5

1 2 3 4 5

*/


#include "stdafx.h"
#include <stdio.h>


int main()
{
 
 int a[5][5] = { 0 };
 
 for (int i = 1; i < 6; i++) {
  for (int j = 1; j < 6; j++) {
   a[i-1][j-1] = j;
   printf("%d ", a[i-1][j-1]);
  }
  printf("\n");
 }

 
   
 
    return 0;
}

 

 

****************************************************************************************************************************************

 

/*

1 2 3 4 5

10 9 8 7 6

11 12 13 14 15

20 19 18 17 16

21 22 23 24 25

*/


#include "stdafx.h"
#include <stdio.h>

 

int main()
{
 int a[5][5] = { 0 };
 int cnt = 1;

 for (int i = 1; i < 6; i++) {
  for (int j = 1; j < 6; j++) {
   a[i - 1][j - 1] = cnt++;
   
  }
  
 }


 for (int i = 1; i < 6; i++) {
  if (i % 2 == 1) {
   for (int j = 1; j < 6; j++) {
    

     printf("%3d", a[i - 1][j - 1]);
    }
    
   }
  else
   for (int j = 5; j > 0; j--) {

 

    printf("%3d", a[i - 1][j - 1]);
   }
 
  printf("\n");
 }

 return 0;
}

****************************************************************************************************************************************

 

/*

1 2 3 4 5

16 17 18 19 6

15 24 25 20 7

14 23 22 21 8

13 12 11 10 9

*/

 

 

#include "stdafx.h"
#include <stdio.h>

 

int main()
{
 int a[5][5] = { 0 };
 int cnt = 0;
 int x = 0, y = 0, dir = 0;

 //dir : 0오른쪽 1아래 2왼쪽 3 위
 while (cnt < 26) {
  cnt++;
  a[y][x] = cnt;

  if (dir == 0) {
   if (a[y][x + 1] == 0 && x < 4)
    x++; 
   else
    dir++;
  }

  if (dir == 1) {
   if (a[y + 1][x] == 0 && y < 4)
    y++;
   else
    dir++;
  }

  if (dir == 2) {
   if (a[y][x - 1] == 0 && x > 0)
    x--;
   else
    dir++;
  }

  if (dir == 3) {
   if (a[y - 1][x] == 0 && y > 0)
    y--;
   else {
    dir = 0;
    x++;
   }
  }
 }


 for (int i = 1; i < 6; i++) {
  for (int j = 1; j < 6; j++) {
   printf("%3d", a[i-1][j-1]);
  }
  printf("\n");
 }
  return 0;
 
}

 

 

반응형

'Study > C' 카테고리의 다른 글

C언어 - 배열 (LED 켜기 게임)  (0) 2018.08.27
C언어 - 배열 (빙고판 만들기)  (0) 2018.08.24
C언어 - 배열 (로또번호 난수발생기)  (0) 2018.08.24
C언어 과제1  (0) 2018.08.21
C언어 과제2  (0) 2018.08.21
반응형

** 배열 사용하여 로또번호 6개 뽑기 **

 


#include "stdafx.h"
#include <stdio.h>
#include <random>
using namespace std;

 

//로또번호 생성기


int main()
{
 random_device rn;
 mt19937_64 rnd(rn());
 uniform_int_distribution<int> range(1, 45);

// 랜덤수 출력 함수

 


 int a[45] = { 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45 };
 int tmp=0;
 
 int ran1 = 0;
 int ran2 = 0;

 

 for (int i = 0; i < 100000; i++) {
  
  ran1 = range(rnd);
  ran2 = range(rnd);
  
  tmp = a[ran1-1];
  a[ran1-1] = a[ran2-1];
  a[ran2-1] = tmp;
  
 } // 카드를 2장뽑아서 섞는다.
 
 for (int j = 0; j < 6; j++) {
  printf("%d ", a[j]);
 } // 배열의 1~6까지 출력

 

    return 0;
}

 

********************************************************************************************************************************************

 

** 로또 번호 출력하여 10장 뽑기 **


#include "stdafx.h"
#include <stdio.h>
#include <random>
using namespace std;

//로또번호 생성기
int main()
{
 random_device rn;
 mt19937_64 rnd(rn());
 uniform_int_distribution<int> range(1, 45);
 int a[45] = { 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45 };
 int tmp=0;
 
 int ran1 = 0;
 int ran2 = 0;
 for (int k = 0; k < 10; k++) {
  for (int i = 0; i < 100000; i++) {

   ran1 = range(rnd);
   ran2 = range(rnd);

   tmp = a[ran1 - 1];
   a[ran1 - 1] = a[ran2 - 1];
   a[ran2 - 1] = tmp;

  }

  for (int j = 0; j < 6; j++) {
   if (0 < a[j] && a[j] < 10) {
    printf("  %d", a[j]);
   }
   else
    printf(" %d", a[j]);
  }
  printf("\n");
 }
    return 0;
}

 

********************************************************************************************************************************************

 

 

**  로또번호 생성기 OMR 마킹 방식  **

 


#include "stdafx.h"
#include <stdio.h>
#include <random>
using namespace std;

//로또번호 생성기 OMR 마킹 방식
int main()
{
 random_device rn;
 mt19937_64 rnd(rn());
 uniform_int_distribution<int> range(1, 45);
 
 int a[45];

 


 int ran1 = 0;
 int cnt = 0;

 for (int i = 0; i < 11; i++) {
  for (int q = 0; q < 45; q++)
  {
   a[q] = q + 1;
  }
  while (cnt < 6) {

   ran1 = range(rnd);
   if (a[ran1 - 1] != 0) {
    printf("%3d ", a[ran1 - 1]);
    a[ran1 - 1] = 0;
    cnt++;
   }

  }
  printf("\n");
  cnt = 0;
 }
   
   
 
    return 0;
}

 

********************************************************************************************************************************************

 

 

반응형

'Study > C' 카테고리의 다른 글

C언어 - 배열 (빙고판 만들기)  (0) 2018.08.24
C언어 - 배열 (2차원배열)  (0) 2018.08.24
C언어 과제1  (0) 2018.08.21
C언어 과제2  (0) 2018.08.21
연산자의 활용  (0) 2016.02.26
반응형

1.3 LinkedList

 

배열은 가장기본적인 형태의 자료구조로 사용하기 쉽고 데이터를 읽어오는데 걸리는 시간이 가장 빠르다는 장점을 가지고 있다.

 

단점 1 : 크기를 변경 할 수 없다.

2. 비순차적인 데이터의 추가 또는 삭제에 시간이 많이 걸린다.

 

이를 보완하기 위해 링크드 리스트 자료구조가 고안되었다.

 

Class Node {

Node next; // 다음 요소의 주소 저장

Object obj; // 데이터를 저장.

}

 

LinkedList 클래스

 

- LinkedList() : LinkedList 객체 생성

- LinkedList(Collection c) : 주어진 컬렉션을 포함하는 LinkedList객체를 생성

- boolean add(Object o) : 지정된 객체(o)를 LinkedList의 끝에 추가, 저장에 성공하면 true, 실패하면 false

- void add(int index, Object element) : 지정된 위치(index)에 객체(element)를 추가

- boolean addAll(Collection c) : 주어진 컬렉션에 포함된 모든 요소를 LinkedList의 끝에 추가한다. 성공하면 true, 실패하면 false

- boolean addAll(int index, Collection c) : 지정된 위치에 주어진 컬렉션에 포함된 모든 요소를 추가. 성공하면 true, 실패하면 false

- void clear() : LinkedList의 모든 요소를 삭제

- boolean contains(Object c) : 지정된 객체가 LinkedList에 포함되었는지 알려줌

- boolean containsAll(Collection c) : 지정된 컬렉션의 모든 요소가 포함되었는지 알려줌

- Object get(int index) : 지정된 위치의 객체를 반환

- int indexOf(Object o) : 지정된 객체가 저장된 위치를 반환

- boolean isEmpty() : LinkedList가 비어있는지 알려준다. 비어있으면 true

- Iterator iterator() : Iterator를 반환한다.

- int lastindexOF(Object o) : 지정된 객체의 위치를 반환(끝부터 역순검색)

- ListIterator listiterator() : listIterator 를 반환

- ListIterator listiterator(int index) : 지정된 위치에서부터 시작하는 listIterator를 반환

- Object remove(int index) : 지정된 위치의 객체를 LinkedList에서 제거

- boolean remove(Object o) : 지정된 객체를 LinkedList에서 제거. 성공하면 true, 실패하면 false

- boolean removeAll(Collection c) : 지정된 컬렉션의 요소와 일치하는 요소를 모두 삭제

- boolean retainAll(Collection c) : 지정된 컬렉션의 모든 요소가 포함되어 있는지 확인.

- Object set(int index, Object element) : 지정된 위치의 객체를 주어진 객체로 바꿈

- int size() : LinkedList에 저장된 객체 수를 반환

- List subList(int fromindex, ind tolndex) : LinkedList의 일부를 list로 반환

- Object[] toArray() : LinkedList저장된 객체를 배열로 반환

- Object[] toArray(Object[] a) : LinkedList저장된 객체를 주어진 배열에 저장하여 반환

- Object element() : LinkedList의 첫 번째 요소를 반환

- boolean offer(Object o) : 지정된 객체 를 LinkedList의 끝에 추가, 성공하면 ture, 실패하면 false

- Object peek() : LinkedList의 첫 번째 요소를 반환

- Object poll() : LinkedList의 첫 번째 요소를 반환. LinkedList에서는 제거된다.

- Object remove() : LinkedList의 첫 번째 요소를 제거

- void addFirst(Object o) : LinkedList의 맨 앞의 객체를 추가

- void addLast(Object o) : LinkedList의 맨 끝의 객체를 추가.

- Iterator dexcendingiterator() : 역순으로 조회하기 위한 DescendingIterator를 반환

- Object getFirst() : LinkedList의 첫번째 요소를 반환

- Object getLast() : LinkedList의 마지막 요소를 반환

- boolean offerFirst(Object o) : LinkedList의 맨 앞에 객체를 추가 성공하면 true

- boolean offerLast(Object o) : LinkedList의 맨 뒤에 객체를 추가 성공하면 true

- Object peekFirst() : LinkedList의 첫번째 요소를 반환

- Object peekLast() : LinkedList의 마지막 요소를 반환

- Object pollFirst() : LinkedList의 첫번째 요소를 반환하면서 제거

- Object pollLast() : LinkedList의 마지막 요소를 반환하면서 제거

- Object pop() : removeFirst()와 동일

- void push(Object o) : addFirst()와 동일

- Object removeFirst() : LinkedList의 첫번째 요소를 제거

- Object removeLast() : LinkedList의 마지막 요소를 제거

- boolean removefirstOccurrence(Object o) : LinkedList에서 첫번쨰로 일치하는 객체를 제거

- boolean removeLastOccurrence(Object o) : LinkedList에서 마지막으로 일치하는 객체를 제거

 

실습1. ArrayList와 LinkedList 수행 속도 차이

 

package Collection_Framework;

import java.util.*;

public class LinkedList1 {

 

public static void main(String[] args) {

ArrayList a1 = new ArrayList(2000000);

ArrayList ll = new ArrayList();

 

System.out.println("=순차적으로 추가하기=");

System.out.println("ArrayList :"+add1(a1));

System.out.println("LinkedList :"+add1(ll));

System.out.println();

System.out.println("=중간에 추가하기=");

System.out.println("ArrayList :"+add2(a1));

System.out.println("LinkedList :"+add2(ll));

System.out.println();

System.out.println("=중간에서 삭제하기 =");

System.out.println("ArrayList :"+remove2(a1));

System.out.println("LinkedList :"+remove2(ll));

System.out.println();

System.out.println("=순차적으로 삭제하기=");

System.out.println("ArrayList :"+remove1(a1));

System.out.println("LinkedList :"+remove1(ll));

}

 

public static long add1(List list) {

long start = System.currentTimeMillis();

for(int i = 0; i<100000;i++)

list.add(i+"");

long end = System.currentTimeMillis();

return end -start;

}

 

public static long add2(List list) {

long start = System.currentTimeMillis();

for(int i = 0; i<10000;i++)

list.add(500,"X");

long end = System.currentTimeMillis();

return end -start;

}

 

public static long remove1(List list) {

long start = System.currentTimeMillis();

for(int i=list.size()-1;i>=0;i--)

list.remove(i);

long end = System.currentTimeMillis();

return end - start;

}

 

public static long remove2(List list) {

long start = System.currentTimeMillis();

for(int i=0;i<10000;i++)

list.remove(i);

long end = System.currentTimeMillis();

return end - start;

}

}

 

 

출력 결과 :

=순차적으로 추가하기=
ArrayList :62
LinkedList :49

=중간에 추가하기=
ArrayList :316
LinkedList :295

=중간에서 삭제하기 =
ArrayList :294
LinkedList :279

=순차적으로 삭제하기=
ArrayList :5
LinkedList :1

반응형

'Study > Java' 카테고리의 다른 글

열거형 개념 정리  (1) 2018.03.26
열거형 enums  (0) 2018.03.19
Collection Framework 1.2 ArrayList  (0) 2018.03.05
java.time 패키지를 이용하여 달력 만들기  (0) 2018.03.05
Collection Framework 1.7 Comparator, Comparable  (0) 2018.02.26

+ Recent posts