Data Journey

International Studies Grad. racing for AI Engineer

Download as .zip Download as .tar.gz View on GitHub
19 December 2020

Linear Data Structure: Array, List

by mervyn

Array

A data structure consisting of a collection of values

int score[10];//Data type Array name [Array size];

In C/C++ programming language,

Type d[10];
Type *d = new Type[size];//Dynamically assign the size of the array. 1) Declare the type and d as pointers 2) Use "new" operator
// We can assign the size of the array according to the specific number

Accessing an element by array index

d[5] = 2;

Release the allocated

delete [] d;

Creation of Arrays in C++ Programming Language

int arr[10];

or

int* arr = new int[10];
int a[3][4];

or

int** a = new int* [3];
  for(int i=0;i<3;i++)
      a[i] = new int[4];

Array in Memory

int score[3] = {52, 17, 61};
// When we declare an array, each value gets stored sequentially in the actual RAM.

Example

#include <cstdio>

int matrix[6][6];

for(int row = 0; row<6; row++)
{
  for(int col = 0; col <6; col++)
  {
    if(row<col)
      matrix[row][col] = 1;
    else if(row==col)
      matrix[row][col] = 0;
    else
      matrix[row][col] = -1;
  }
}

Pointer

A variable storing a memory address

int n = 3; //If you put *, it becomes the pointer data type.
int* pn = &n; //pointer called pn. Initialize the variable pn with the address of n.
// the value of pn becomes the address of n
// *pn is an integer

& (Ampersand) Operator

Reference operator

#include <cstdio>

int main()
{
  char c = 'A';
  char* pc = &c;// pointer pc points the location of

A  printf("%c %p\n", c, pc);
  printf("%p %p\n", &c, &pc);
  printf("%d %d\n", sizeof(c), sizeof(pc));//size of character, size of pointer

  return 0;
}

* (Asterisk) Operator

Deference operator

#include <cstdio>

int main()
{
  char c = 'A';
  char* pc = &c;// a pointer pc is pointing to a variable c
  //If you put * to the pointer pc, it (*pc) prints the value of variable c.

  printf("%c %c\n", c *pc);// A A

  *pc = 'C'; // If you declare 'C' to the *pc,
  //You can change the value of variable c
  //directly through the pointer (pc) that is pointing //to the variable c
  printf("%c %c\n", c, *pc);// C C

  return 0;
}

answer: A A C C

Example: What are the results?

#include <cstdio>

int main()
{
  int a, b, c;
  int* pa = &a, *pb = &b, *pc = &c;

  *pa = 10, *pb = 20;
  *pc = *pa + *pb;

  printf("%d %d %d", a, b, c);

  return 0;
}

answer: 10 20 30

Function Call with Pointers

Two types of passing augments to a function:

#include <cstdio>

 void swap1(int x, int y);//Call by value
 void swap2(int* px, int* py);//Call by reference

 int main()
 {
  int a = 5, b = 7;
  swap1(a, b);// swap1 cannot change the value of a and b in the main function.
  printf("%d %d\n", a, b);// 5 7
  swap2(&a, &b);// *px == a, *py == b. Change the addresses of a and b.
  // swap2 can change the value of a and b. a == *py, b == *px.
  printf("%d %d\n", a, b);// 7 5
  return 0;
 }
void swap1(int x, int y)
{
  int temp = x;
  x = y;
  y = temp;
}
void swap2(int* px, int* py)
{
  int temp = *px;
  *px = *py;
  *py = temp;
}

Insertion and Deletion in Arrays

Insertion and deletion can require significant number of operations in arrays.

Linked Lists

A linear collection of data elements

Linked List Implementation

typedef int Data;

typedef struct _Node
{
  Data item;
  struct _Node* next;
} Node;

typedef struct
{
  Node* head;
  int len;
} LinkedList;

Arrow and Box Representation

source “K-MOOC 허재필 교수님의 <인공지능을 위한 알고리즘과 자료구조: 이론, 코딩, 그리고 컴퓨팅 사고> 강좌의 2-1 강좌의 배열과 리스트 중(http://www.kmooc.kr/courses/course-v1:SKKUk+SKKU_46+2020_T1)”

back next

tags:

Comments

Post comment
Loading...

Share this: