Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

//========== 3.1 REMOVE ALL X//========== 3.2 REMOVE DUPLICATE //========== 3.

3 REVERSE LIST
void removeAll(SList& l, int x){ void removeDuplicates(SList& l) { void reverseList(SList& l) {
SNode* cur = l.head; SNode* cur = l.head; SNode* prev = NULL; //========== 3.4 INSERT
SNode* prev = NULL; SNode* curr = l.head; EVEN NODE
SNode* nextNode = NULL; void insertNodes(SList& l){
struct SNode{ while (cur != NULL && cur->next != NULL) {
int data; int count = 2;
while(cur != NULL){ SNode* runner = cur;
};
SNode* S next;
if(cur->data == x){ while (runner->next != NULL) { while (curr != NULL) {
SNode* newNode = new
SNode();
struct SList{ SNode* temp = cur; if (runner->next->data == cur->data) { nextNode = curr->next;
SNode* head; newNode->data = count;
if(prev == NULL){ SNode* temp = runner->next; curr->next = prev;
}; newNode->next = l.head;
SNode* makeNode(int x){ l.head = cur->next; runner->next = runner->next->next; prev = curr;
l.head = newNode;
SNode* newNode = new SNode; } delete temp; curr = nextNode;
newNode->data = x; else{ } else { }
newNode->next = NULL;
return newNode; prev->next = cur->next; runner = runner->next; SNode* curr = l.head->next;
} } } count += 2;
void addHead(SList &l, SNode* node){ l.head = prev;
cur = cur->next; } while (curr != NULL && curr-
if(l.head == NULL){ }
l.head = node; delete temp; cur = cur->next; >next != NULL){
return; } } SNode* newNode = new
} else{ } //========== 3.6 PREFIX SUM SNode();
node->next = l.head;
l.head = node; prev = cur; void prefixSum(SList& in, SList& out) { newNode->data = count;
} cur = cur->next; int sum = 0; newNode->next = curr-
void addTail(SList &l, SNode* node){ //========== 3.5 INSERT SORTED SNode* inCurr = in.head;
} >next;
if(l.head == NULL){ VALUE SNode* outCurr = NULL;
l.head = node; } curr->next = newNode;
void insertSorted(SList& l, int value){
return; } curr = newNode->next;
} SNode* newNode = makeNode(value);
SNode* prev = NULL; while (inCurr != NULL) { count += 2;
SNode* curr = l.head;
while(curr->next != NULL){ sum += inCurr->data; }
SNode* curr = l.head; //========== 3.7 SPLIT LIST
curr = curr->next;
SNode* newNode = makeNode(sum); }
} if(l.head == NULL || l.head->data >= void splitList(SList& l, SList& first,
curr->next = node; value){ if (outCurr == NULL) { SList& second) {
node->next = NULL; newNode->next = l.head; out.head = newNode; SNode* curr = l.head;
}
void readList(string filename, SList &l){ l.head = newNode; outCurr = out.head; SNode* next;
ifstream File(filename.c_str()); return; } else {
if(!File){ } outCurr->next = newNode;
cout << "Unable to open " << filename << endl; while (curr != NULL) {
return; while(curr != NULL && curr->data < outCurr = outCurr->next;
} value){ } next = curr->next;
int num; prev = curr; inCurr = inCurr->next; curr->next = NULL;
while(File >> num && num != 0){ addTail(first, curr);
SNode* newNode = makeNode(num); curr = curr->next; } //========== 3.8 COMBINE LIST ==========//
addTail(l, newNode); } } void combineLists(SList& l1, SList& l2) {
} newNode->next = curr; SNode* curr1 = l1.head;
} if (next != NULL) {
prev->next = newNode; SNode* curr2 = l2.head; curr = next;
} next = curr->next;
void printList(SList& l){
for(SNode* p = l.head; p != NULL; p = p->next){ while (curr1 != NULL && curr2 != NULL) { curr->next = NULL;
cout << p->data << " "; SNode* next1 = curr1->next; addTail(second, curr);
} curr = next;
cout << endl; SNode* next2 = curr2->next;
} } else {
void writeList(string filename, SList& l) { break;
ofstream outFile(filename); curr1->next = curr2; }
if (!outFile) { curr2->next = next1;
cout << "Unable to open " << filename << " for writing." << endl; }
return; l.head = NULL;
} }
//Push studentfor (SNode* p = l.head; p != NULL; p = p->next) { curr1 = next1;
void push(Stack& stack,
outFile Student
<< p->data << " s){
"; curr2 = next2;
Node* temp } = makeNode(s); }
outFile << "0" << endl;
if(!temp){ cout << "List written to " << filename << endl; }
cout <<} "Stack Overflow!";
return; //Display all students in the stack withou removing them and display student at the
} //Remove multiple student top without popping it
temp->data = s; void removeMultipleStudent(Stack& void display(Stack& stack, bool popFlag = false){
temp->next = stack.top; stack, Student students[], int count){ Node* temp;
stack.top = temp; for(int i = 0; i < count; i++){ if(stack.top == NULL){
} pop(stack); cout << "Stack Overflow!";
//Pop student } return;
void pop(Stack& stack){ } }
Node* temp; //Calculate Average Points else{
if(stack.top == NULL){ double calcAvgPoints(Stack& stack){ temp = stack.top;
cout << "Stack Overflow!"; Node* temp; if(popFlag){
return; double sum = 0; Student topStudent = temp->data;
} double count = 0; cout << "Top of the stack: " << topStudent.name << " - " <<
else{ if(stack.top == NULL){ topStudent.points << endl;
temp = stack.top; cout << "Stack Overflow!"; }
stack.top = stack.top->next; } else{
delete(temp); else{ while(temp != NULL){
} temp = stack.top; cout << temp->data.name << " - " << temp->data.points;
} while(temp != NULL){ temp = temp->next;
//Add multiple student sum += temp->data.points; if(temp != NULL) cout << " -> ";
void addMultipleStudent(Stack& stack, temp = temp->next; }
Student students[], int count){ count++; }
for(int i = 0; i < count; i++){ } }
push(stack, students[i]); } }
} return (sum / count); //Clear stack
} } void clearStack(Stack& stack){
//Search Student by name while (!isEmpty(stack)){
bool searchStudent(Stack& stack, string pop(stack);
s){ }
Node* temp; }
if(stack.top == NULL){
cout << "Stack Overflow!";
}
else{
temp = stack.top;
void generatePermutations(int* nums, int int tribonacci(int n) {
bool isSafe(int x, int y, int sol[N][N]){ size, int start, int** result, int& count){ if (n == 0) return 0; ListNode
return (x >= 0 && x < N && y >= 0 && y < N if (start == size - 1){ else if (n == 1 || n == 2) return 1; *getIntersectionNode(ListNode
&& sol[x][y] == -1); for (int i = 0; i < size; ++i){ *headA, ListNode *headB) {
} result[count][i] = nums[i]; if (!headA || !headB) return NULL;
int* trib = new int[n+1];
int solveKTUtil(int x, int y, int move, int sol[N][N], }
trib[0] = 0;
int X[8], int Y[8]){ count++;
trib[1] = trib[2] = 1; int lenA = length(headA), lenB =
int nextx, nexty; return;
} length(headB);
if(move == N*N){
return 1; for (int i = 3; i <= n; ++i) {
} trib[i] = trib[i - 1] + trib[i - 2] + trib[i - 3]; if (lenA > lenB) {
for (int i = start; i < size; ++i){
for(int i = 0; i < 8; i++){ } while (lenA > lenB) {
if (i != start && nums[i] ==
nextx = x + X[i]; int result = trib[n]; headA = headA->next;
nums[start])
nexty = y + Y[i]; continue; lenA--;
if(isSafe(nextx, nexty, sol)){ delete[] trib; }
sol[nextx][nexty] = move; } else if (lenA < lenB){
if(solveKTUtil(nextx, nexty, move + 1, sol, swap(nums[start], nums[i]); while (lenA < lenB){
X, Y) == 1){ generatePermutations(nums, size, return result; headB = headB->next;
start + 1, result, count); ListNode*
} reverseBetween(ListNode* head, int lenB--;
return 1;
swap(nums[start], nums[i]); left, int right){ }
}
} if (head == NULL || left >= right){ }
else{
} return head; while (headA && headB) {
sol[nextx][nexty] = -1;
bool hasCycle(ListNode* head){ } if (headA == headB) return headA;
}
if(head == NULL){ ListNode temp(0); headA = headA->next;
}
return false; temp.next = head; headB = headB->next;
}
} ListNode *prev = &temp; }
return 0;
ListNode* curr = head; for (int i = 1; i < left; i++){ return NULL;
}
ListNode* nextNode = head; prev = prev->next; }
while(nextNode != NULL && nextNode->next != }
bool solveKT(){ NULL){ ListNode *curr = prev->next;
int sol[N][N]; curr = curr->next; ListNode *nextNode = NULL;
for(int i = 0; i < N; i++){ nextNode = nextNode->next->next; for (int i = 0; i < right - left; i++){
for(int j = 0; j < N; j++){ if(curr == nextNode){ nextNode = curr->next;
sol[i][j] = -1; return true; curr->next = nextNode->next;
} } nextNode->next = prev->next;
} } prev->next = nextNode;
int X[8] = {2, 1, -1, -2, -2, -1, 1, 2}; return false; }
int Y[8] = {1, 2, 2, 1, -1, -2, -2, -1}; } return temp.next;
}
sol[0][0] = 0;
if(solveKTUtil(0, 0, 1, sol, X, Y) == 0){
cout << "Solution does not exist" << endl;
return 0;
}
else{
print(sol);
}
return 1;
}

bool isSafe(int board[][10], int row, int col, int N){


for (int i = 0; i < row; ++i){
if (board[i][col])
return false;
}
for (int i = row, j = col; i >= 0 && j >= 0; --i, --j){
if (board[i][j])
return false;
}
for (int i = row, j = col; i >= 0 && j < N; --i, ++j){
if (board[i][j])
return false;
}
return true;
}
bool solveNQueensUtil(int board[][10], int row, int N){
if(row == N){
return true;
}
for(int col = 0; col < N; col++){
if(isSafe(board, row, col, N)){
board[row][col] = 1;
if(solveNQueensUtil(board, row + 1, N)){
return true;
}
board[row][col] = 0;
}
}
return false;
}

You might also like