Linked List
1.Linked List Definition
//Tree Declaration
struct Node
{
int val;
Node *next;
Node():val(0),next(nullptr){};
Node(x):val(x),next(nullptr){};
Node(x,Node* node):val(x),next(node)();
};
//Tree Initializaiton with default value zero
Node *node=new Node();
Linux Command Line - Bash Scripting
Shells
hadley@hadley-MacBookPro:~$ which bash
/usr/bin/bash
hadley@hadley-MacBookPro:~$ cat /etc/shells
# /etc/shells: valid login shells
/bin/sh
/bin/bash
/usr/bin/bash
/bin/rbash
/usr/bin/rbash
/bin/dash
/usr/bin/dash
Linux Command Line - Process Management
Definition
-
Instance of a program An instance of a program is a copy of an executable version of the program that has been written to the computer’s memory.
-
Process When we run a program, those instructions are copied into memory and space is allocated for variables and other stuff required to manage its execution. This running instance of a program is called a process and it’s processes which we manage.
Top
Linux Command Line - Regular Expressions
Egrep
egrep is a program which will search a given set of data and print every line which contains a given pattern. It is an extension of a program called grep. It’s name is odd but based upon a command which did a similar function, in a text editor called ed. It has many command line options which modify it’s behaviour so it’s worth checking out it’s man page. ie the -v option tells grep to instead print every line which does not match the pattern.
hadley@hadley-MacBookPro:~/Developments/parentDirectory$ egrep 'mellon' data.txt
Mark watermellons 12
Oliver rockmellons 2
-v to tells egrep to instread print every line which does not match the pattern
Linux Command Line - Piping and Redirection
Redirecting to a file
save current command output to a file instead of printed to the screen.
hadley@hadley-MacBookPro:~/Developments/parentDirectory$ ls -l > list
hadley@hadley-MacBookPro:~/Developments/parentDirectory$ cat list
total 16
-rw-rw-r-- 1 hadley hadley 230 Okt 21 22:08 data_test.txt
-rw-rw-r-- 1 hadley hadley 230 Okt 21 21:01 data.txt
-rwxrwxrwx 1 hadley hadley 92 Okt 20 21:28 file.txt
-rw-rw-r-- 1 hadley hadley 0 Okt 21 22:08 list
drwxrwxr-x 2 hadley hadley 4096 Okt 21 21:09 test1
-rw-rw-r-- 1 hadley hadley 0 Okt 20 21:42 test.cpp
-rw-rw-r-- 1 hadley hadley 0 Okt 20 21:01 ttt
-rw-rw-r-- 1 hadley hadley 0 Okt 20 21:42 x
-rw-rw-r-- 1 hadley hadley 0 Okt 20 21:43 x1
-rw-rw-r-- 1 hadley hadley 0 Okt 20 21:42 y
Linux Command Line II
Permissions
view permissions
hadley@hadley-MacBookPro:~/Developments/parentDirectory$ ls -l
total 8
-rw-rw-r-- 1 hadley hadley 92 Okt 20 21:28 file.txt
drwxrwxr-x 2 hadley hadley 4096 Okt 20 21:33 test1
-rw-rw-r-- 1 hadley hadley 0 Okt 20 21:42 test.cpp
-rw-rw-r-- 1 hadley hadley 0 Okt 20 21:01 ttt
-rw-rw-r-- 1 hadley hadley 0 Okt 20 21:42 x
-rw-rw-r-- 1 hadley hadley 0 Okt 20 21:43 x1
-rw-rw-r-- 1 hadley hadley 0 Okt 20 21:42 y
- The first character identifies the file type. If it is a dash ( - ) then it is a normal file. If it is a d then it is a directory.
- The following 3 characters represent the permissions for the owner. A letter represents the presence of a permission and a dash ( - ) represents the absence of a permission. In this example the owner has all permissions (read, write and execute).
- The following 3 characters represent the permissions for the group. In this example the group has the ability to read but not write or execute. Note that the order of permissions is always read, then write then execute.
- Finally the last 3 characters represent the permissions for others (or everyone else). In this example they have the execute permission and nothing else.
Two Pointers
Two Pointers Classical Problem
Sum
Description
Given a sorted array A, having N integers, find if there exists any pair of elements such that their sum is equal to target.
bool isPairSum(int array[], int N, int target){
//two pointers
int i=0,j=N-1;
while(i<j){
if(array[i]+array[j]==target){
return true;
}else if(array[i]+array[j]<target){
i++;
}else{
j--;
}
}
return false;
}
Backtracking and DFS
String Definition
Standard Backtracking
Permutations
Permutations for dictinct data variables
void permutation(vector<int> &num, int l, vector<vector<int> > &res) {
if (l == num.size()-1) {
res.push_back(num);
return;
}
for (int i = l; i < num.size(); i++) {
swap(num[i], num[l]);
permutation(num, l+1, res);
swap(num[i], num[l]);
}
}