5 bài của tau: Question 1: Pointer is a variable which - TopicsExpress



          

5 bài của tau: Question 1: Pointer is a variable which contains the address of a block memory. When we declare a pointer and initial it by the new keyword, it will point to a block memory in heap memory. To delete a dynamically allocated, we must make sure that the dynamically allocated array exists in heap memory, in an other word, the dynamically allocated array must be allocated complete in the heap memory, so we can delete an allocated array by: delete[] A; Question 2: In a singly linked list with a head and a tail pointer, remove at the beginning is easier then remove a node at the end of the list, because: - When we delete a node at the beginning of the list, we can know the next node, we delete head and point head to the next node with complexity O(1). - When we delete a node at the end of the list, we dont know the node before it, so we must use a pointer variable and move it from the head to the node before the tail, this make a complexity O(N) with n is the number of nodes of the list. After that, we delete tail and move tail to the node before it. Question 3: I assume the list has the beginning is head and the end is tail. struct Node { int value; Node* pNext; } int dequeue(Node* &head, Node* &tail) { int result = head->value; if(head == tail) { delete head; head = NULL; tail = NULL; return result; } Node* cur = head->pNext; delete head; head = cur; return result; } void dequeue(Node* &head, Node* &tail, int nValue) { Node* newNode = new Node; newNode->pNext = NULL; newNode->value = nValue; if(head == NULL) { head = newNode; tail = newNode; return; } tail->pNext = newNode; tail = newNode; } Question 4: bool isBiggestInRow(intx, int y, int** a, int m, int n) { for(int i = 0; i < n; i++) if(a[x][i] > a[x][y]) return false; return true; } bool isSmallestInCollumn(int x, int y, int** a, int m, int n) { for(int i = 0; i < m; i++) if(a[i][y] < a[x][y]) return false; return true; } int findTheResult(int** a, int m, int n) { int sum = 0; for(int i = 0; i < m; i++) for(int j = 0; j < n; j++) if(isBiggestInRow(x, y, a, m, n) && isSmallestInCollumn(x, y, a, m, n) sum += a[x][y]; return sum; } Question 5: void printAllSubset(int* a, int* b, int n, int index) { for(int j = 0; j < index; j++) cout
Posted on: Mon, 14 Apr 2014 03:38:23 +0000

Trending Topics



>

Recently Viewed Topics




© 2015