BPFtrace is a high level tracing language for Linux Berkeley Packet Filter (BPF) available in recent Linux kernels. Built on top of LLVM and BCC (https://github.com/iovisor/bcc), BPFtrace provides an easier way of writing BPF programs for interacting with Linux tracing capabilites, such as kprobes, uprobes, kernel tracepoints, USDT and hardware events.
We'll go over Linux BPF itself and how BPFtrace fits in, followed by some demonstrations of new performance and monitoring tools written with BPFtrace.
https://github.com/iovisor/bpftrace
This presentation is designed to provide a basic overview of the following: System Organization, Memory Organization, Stack Organization (For Function Calls), A Vulnerable C Program, Exploiting Buffer Overflow
BPFtrace is a high level tracing language for Linux Berkeley Packet Filter (BPF) available in recent Linux kernels. Built on top of LLVM and BCC (https://github.com/iovisor/bcc), BPFtrace provides an easier way of writing BPF programs for interacting with Linux tracing capabilites, such as kprobes, uprobes, kernel tracepoints, USDT and hardware events.
We'll go over Linux BPF itself and how BPFtrace fits in, followed by some demonstrations of new performance and monitoring tools written with BPFtrace.
https://github.com/iovisor/bpftrace
This presentation is designed to provide a basic overview of the following: System Organization, Memory Organization, Stack Organization (For Function Calls), A Vulnerable C Program, Exploiting Buffer Overflow
Allison Kaptur: Bytes in the Machine: Inside the CPython interpreter, PyGotha...akaptur
Byterun is a Python interpreter written in Python with Ned Batchelder. It's architected to mirror the structure of CPython (and be more readable, too)! Learn how the interpreter is constructed, how ignorant the Python compiler is, and how you use a 1,500 line switch statement every day.
This is a presentation I put together and presented for my colleagues at IBM back in 2006. I started on the section featuring heap exploits and never finished it. I want to finish it someday.
Allison Kaptur: Bytes in the Machine: Inside the CPython interpreter, PyGotha...akaptur
Byterun is a Python interpreter written in Python with Ned Batchelder. It's architected to mirror the structure of CPython (and be more readable, too)! Learn how the interpreter is constructed, how ignorant the Python compiler is, and how you use a 1,500 line switch statement every day.
This is a presentation I put together and presented for my colleagues at IBM back in 2006. I started on the section featuring heap exploits and never finished it. I want to finish it someday.
Clara Luz Roldán Inscribe Candidatura en RegistraduriaClara Luz Roldán
Roldan González caminará desde las Canchas Panamericanas hasta la Registraduría acompañada de los integrantes del partido y líderes que la respaldan.
En un acto en el que cientos de caleños serán quienes apadrinen su campaña, Clara Luz Roldán González, hará mañana miércoles, 13 de julio la inscripción de su candidatura a la Alcaldía de Cali por el Partido de la U, ante la Registraduría Municipal.
Implementing Software Machines in Go and CEleanor McHugh
Early draft of a tutorial on techniques for implementing virtual machines and language interpreters. Contains example programs for functional stacks and despatch loops.
Write a C program that reads the words the user types at the command.pdfSANDEEPARIHANT
Write a C program that reads the words the user types at the command prompt (using the \'int
argc, char * argv[] and store each unique letter in a Binary Search Tree. When a duplicate is
encountered do not store the letter again and instead keep track of the count in the tree. Once the
Binary Search tree has been created print out the tree both inorder and reverse order. Also print
the highest and lowest alphabetically letter in the tree if any.
Solution
# include
# include
# include
typedef struct BST {
int data;
struct BST *lchild, *rchild;
} node;
void insert(node *, node *);
void preorder(node *);
findMinimum(struct node* root)
findMaximum(struct node* root)
void reverseLevelOrder(struct node* root)
void main(int argc,char argv) {
char argv = \'N\';
int key;
node *new_node, *root, *tmp, *parent;
node *get_node();
root = NULL;
clrscr();
printf(\"\ Program For Binary Search Tree \");
do {
printf(\"\ 1.Create\");
printf(\"\ 2.Search\");
printf(\"\ 3.Recursive Traversals\");
printf(\"\ 4.Exit\");
printf(\"\ Enter your choice :\");
scanf(\"%d\", &argc);
switch (argc) {
case 1:
do {
new_node = get_node();
printf(\"\ Enter The Element \");
scanf(\"%d\", &new_node->data);
if (root == NULL) /* Tree is not Created */
root = new_node;
else
insert(root, new_node);
printf(\"\ Want To enter More Elements?(y/n)\");
argv= getch();
} while (argv == \'y\');
break;
case 2:
if (root == NULL)
printf(\"Tree Is Not Created\");
else {
printf(\"\ The Preorder display : \");
preorder(root);
}
break;
}
} while (argv != 4);
}
/*
Get new Node
*/
node *get_node() {
node *temp;
temp = (node *) malloc(sizeof(node));
temp->lchild = NULL;
temp->rchild = NULL;
return temp;
}
/*
This function is for creating a binary search tree
*/
void insert(node *root, node *new_node) {
if (new_node->data < root->data) {
if (root->lchild == NULL)
root->lchild = new_node;
return newNode(key);
else
insert(root->lchild, new_node);
}
if (new_node->data > root->data) {
if (root->rchild == NULL)
root->rchild = new_node;
return newNode(key);
else
insert(root->rchild, new_node);
}
}
if (key == node->key)
{
(node->count)++;
return node;
}
/*
This function displays the tree in preorder fashion
*/
void preorder(node *temp) {
if (temp != NULL) {
printf(\"%d\", temp->data);
preorder(temp->lchild);
preorder(temp->rchild);
}
}
// Returns maximum value in a given Binary Tree
int findMaximum(struct node* root)
{
// Base case
if (root == NULL)
return INT_MAXIMUM;
// Return maximum of 3 values:
// 1) Root\'s data 2) Max in Left Subtree
// 3) Max in right subtree
int res = root->data;
int lres = findMaximum (root->lchild);
int rres = findMaximum (root->rchild);
if (lres > res)
res = lres;
if (rres > res)
res = rres;
return res;
}
// Returns minimum value in a given Binary Tree
int findMinimum(struct node* root)
{
// Base case
if (root == NULL)
return INT_MINIMUM;
// Return minimum of 3 values:
// 1) Root\'s data 2) Max in Left Subtree
// 3) Max in right subtree
int res = root->data;
int lres = findMinimum(r.
Implementation File:
---------------------------------------------------------
//----- List.cpp -----
#include <iostream>
using namespace std;
#include "linkedlist.h"
//-- Definition of the class constructor
List::List()
: first(0), mySize(0)
{ }
//-- Definition of the copy constructor
List::List( const List & origList)
{
mySize = origList.mySize;
first = 0;
if (mySize == 0) return ;
NodePointer origPtr, lastPtr;
first = new Node(origList.first->data); // copy first node
lastPtr = first;
origPtr = origList.first->next;
while (origPtr != 0)
{
lastPtr->next = new Node(origPtr->data);
origPtr = origPtr->next;
lastPtr = lastPtr->next;
}
}
//-- Definition of the destructor
inline List::~List()
{
NodePointer prev = first,
ptr;
while (prev != 0)
{
ptr = prev->next;
delete prev;
prev = ptr;
}
}
// Definition of empty()
bool List::empty()
{
return mySize == 0;
}
//-- Definition of the assignment operator
const List & List:: operator =( const List & rightSide)
{
mySize = rightSide.mySize;
first = 0;
if (mySize == 0) return * this ;
if ( this != &rightSide)
{
this ->~List();
NodePointer origPtr, lastPtr;
first = new Node(rightSide.first->data); // copy first node
lastPtr = first;
origPtr = rightSide.first->next;
while (origPtr != 0)
{
lastPtr->next = new Node(origPtr->data);
origPtr = origPtr->next;
lastPtr = lastPtr->next;
}
}
return * this ;
}
//-- Definition of insert()
void List::insert(ElementType dataVal, int index)
{
if (index < 0 || index > mySize)
{
cerr << "Illegal location to insert -- " << index << endl;
return ;
}
mySize++;
NodePointer newPtr = new Node(dataVal),
predPtr = first;
if (index == 0)
{
newPtr->next = first;
first = newPtr;
}
else
{
for ( int i = 1; i < index; i++)
predPtr = predPtr->next;
newPtr->next = predPtr->next;
predPtr->next = newPtr;
}
}
//-- Definition of erase()
void List::erase( int index)
{
if (index < 0 || index >= mySize)
{
cerr << "Illegal location to delete -- " << index << endl;
return ;
}
mySize--;
NodePointer ptr,
predPtr = first;
if (index == 0)
{
ptr = first;
first = ptr->next;
delete ptr;
}
else
{
for ( int i = 1; i < index; i++)
predPtr = predPtr->next;
ptr = predPtr->next;
predPtr->next = ptr->next;
delete ptr;
}
}
//-- Definition of display()
void List::display(ostream & out) const
{
NodePointer ptr = first;
while (ptr != 0)
{
out << ptr->data << " ";
ptr = ptr->next;
}
}
//-- Definition of the output operator
ostream & operator <<(ostream & out, const List & aList)
{
aList.display(out);
return out;
}
nstructions: For this lab, you will write a program that reads in a list of items for a camera store. The items for the list are in a file called InventoryFile.txt. The program will give the user options to insert an item, remove an item, display the list of items, search for an item, or exit the program. Each line of the file InventoryFile has the following format: Item Number - an integer Number in stock - an integer (between 0 and 999) Unit Price - a floating-point value Minimum inventory level - an inte.
-- Task 2- Debugging a program with stacks- queues- and doubly-linked.docxAdamq0DJonese
/* Task 2: Debugging a program with stacks, queues, and doubly-linked lists There are a number of errors in the following program. All errors are located in main() and structure definitions. Function declarations and definitions are correct! Locate all errors, fix them (as shown below), run the program and save its output as a comment at the end of the source file. Example: int num = 10; int *ptr; num = &ptr; // <== Error: Comment the line and write the correct line below // Write a short justification where appropriate // num = &ptr; // Error #1 ptr = # Name: */ #include #include #include #include #define DUMMY_TRAILER '\177' // octal ASCII code of the // last character in the ASCII table #define NUM_CITIES 10 typedef struct { char name[12]; int temperature[5]; } CITY; // Stack and Queue Node typedef struct node NODE; struct node { CITY city; node *next; }; // Doubly Linked List Node typedef struct d_node D_NODE; struct d_node { CITY city; NODE *forw; NODE *back; }; // Stack Functions NODE *push(NODE *stack, const CITY *pStu); NODE *pop(NODE **stack); // Queue Functions void enqueue(NODE **queue, NODE **rear, const CITY *pStu); NODE *dequeue(NODE **queue, NODE **rear); // Doubly Linked List Functions D_NODE *init_list(void); int insert(D_NODE *list, const CITY *pStu); void traverse_forw(D_NODE *list); void traverse_back(D_NODE *list); // Other Functions void printCity(const CITY *pCity); int main (void) { CITY cList[NUM_CITIES] = { {"Cupertino", {88, 89, 87, 85, 89}}, {"Flagstaff", {81, 80, 88, 89, 89}}, {"Los Angeles", {87, 88, 89, 89, 90}}, {"Philadelphia", {96, 99, 99, 90, 95}}, {"Phoenix", {106, 109, 109, 100, 105}}, {"Portland", {89, 90, 85, 89, 90}}, {"Reno", {108, 105, 109, 100, 108}}, {"Salem", {85, 90, 85, 89, 90}}, {"Tucson", {107, 100, 109, 100, 108}}, {"Yreka", {101, 109, 100, 108, 109}} }; NODE *stack = NULL; NODE *top = NULL; NODE *queue = NULL, *rear = NULL; NODE *front; D_NODE *list; list = init_list(); // build stack and queue with data from an array of CITY structures srand((unsigned int)time(NULL)); int count = rand() % 10; for ( int n = 0; n < count; n++) { int i = rand() % NUM_CITIES; int duplicate = insert(list, &cList[i]); if(duplicate) { // already in the list! push(stack, &cList[i]); enqueue(&queue, &rear, cList[i]); } } // display list printf("\nLIST contents (forwards):\n"); traverse_forw(list); printf("\nLIST contents (backwards):\n"); traverse_back(list); // display stack if (top) { printf("\nSTACK contents from top to bottom:\n"); while ((top = pop(stack))) { printCity(top->city); } } else printf ("Empty Stack!\n"); // display queue if (front) { printf("\nQUEUE contents from front to rear:\n"); while ((front = dequeue( queue, rear))) { printCity(front->city); } } else printf ("Empty Queue!\n"); return 0; } /*************************************************** Displays the fileds of a CIS_CLASS structure Pre pCls - a pointer to a CIS_CLASS structure Post */ void printCity(const CITY *pCity) { printf("%-20s %3d\n",.
pleaase I want manual solution forData Structures and Algorithm An.pdfwasemanivytreenrco51
pleaase I want manual solution for
Data Structures and Algorithm Analysis in C++, Third Edition
By Clifford A. Shaffer
because my question from this book.page270 ,question 7.4
The implementation for Mergesort given in Section 7.4 takes an array as input and sorts that
array. At the beginning of Section 7.4 there is a simple pseudocode implementation for sorting a
linked list using Mergesort. Implement both a linked list-based version of Mergesort and the
array-based version of Mergesort, and compare and analyze their running times.
Solution
Linked list:
#include
#include
struct node
{
int data;
struct node* next;
};
struct node* SortedMerge(struct node* a, struct node* b);
void FrontBackSplit(struct node* source,
struct node** frontRef, struct node** backRef);
void MergeSort(struct node** headRef)
{
struct node* head = *headRef;
struct node* a;
struct node* b;
if ((head == NULL) || (head->next == NULL))
{
return;
}
FrontBackSplit(head, &a, &b);
MergeSort(&a);
MergeSort(&b);
*headRef = SortedMerge(a, b);
}
struct node* SortedMerge(struct node* a, struct node* b)
{
struct node* result = NULL;
if (a == NULL)
return(b);
else if (b==NULL)
return(a);
if (a->data <= b->data)
{
result = a;
result->next = SortedMerge(a->next, b);
}
else
{
result = b;
result->next = SortedMerge(a, b->next);
}
return(result);
}
void FrontBackSplit(struct node* source,
struct node** frontRef, struct node** backRef)
{
struct node* fast;
struct node* slow;
if (source==NULL || source->next==NULL)
{
*frontRef = source;
*backRef = NULL;
}
else
{
slow = source;
fast = source->next;
while (fast != NULL)
{
fast = fast->next;
if (fast != NULL)
{
slow = slow->next;
fast = fast->next;
}
}
*frontRef = source;
*backRef = slow->next;
slow->next = NULL;
}
}
void printList(struct node *node)
{
while(node!=NULL)
{
printf(\"%d \", node->data);
node = node->next;
}
}
void push(struct node** head_ref, int new_data)
{
struct node* new_node =
(struct node*) malloc(sizeof(struct node));
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}
int main()
{
struct node* res = NULL;
struct node* a = NULL;
push(&a, 15);
push(&a, 10);
push(&a, 5);
push(&a, 20);
push(&a, 3);
push(&a, 2);
MergeSort(&a);
printf(\"\ Sorted Linked List is: \ \");
printList(a);
getchar();
return 0;
}
Time complexity:o(nlogn)
Array :
#include
#include
void merge(int arr[], int l, int m, int r)
{
int i, j, k;
int n1 = m - l + 1;
int n2 = r - m;
int L[n1], R[n2];
for (i = 0; i < n1; i++)
L[i] = arr[l + i];
for (j = 0; j < n2; j++)
R[j] = arr[m + 1+ j];
i = 0; // Initial index of first subarray
j = 0;
k = l;
while (i < n1 && j < n2)
{
if (L[i] <= R[j])
{
arr[k] = L[i];
i++;
}
else
{
arr[k] = R[j];
j++;
}
k++;
}
while (i < n1)
{
arr[k] = L[i];
i++;
k++;
}
while (j < n2)
{
arr[k] = R[j];
j++;
k++;
}
}
void mergeSort(int arr[], int l, int r)
{
if (l < r)
{
int m = l+(r-l)/2;
// Sort first and second halves
mergeSort(arr, l, m);
mergeSort(arr, m+1, r);
merge(arr, l, m, r);
}
}
void printArray(int A[], i.
Program In C You are required to write an interactive C program that.pdfamitbagga0808
Program In C You are required to write an interactive C program that prompts the user for
commands, accepts commands from the keyboard (stdin) and executes those commands. When a
command requires output, it must be written to stdout. The program must continue to accept and
process commands until the user types the end command ourput, it must be writen to stdout. The
program must continue to accept and proces.commands The program deals with linked lists.
Each node of such a list contains a string of length at most 10, a positive integer (i.e., an integer
value 2 1) and a pointer to the next node of the list. For any node, the string and the integer
stored in that node will be referred to as the symbol and count for that node respectively. At all
times, the list must satisfy the following two important properties. 1. The symbols appearing in
the list are all distinct; that is, no two nodes have the same symbol. 2. When the list is scanned
from left to right, the counts must be in non-increasing order. An example of such a linked list is
shown below. Search 4 Maxval4 begin/ Loop head
Solution
/**********************************************/
/****** NAMES ******************** COMMANDS ***/
/* Insert Command : ins str */
/* Delete Command : del str */
/* Forced Delete Command : fde val */
/* Print Statistics Command : pst */
/* Print List Command : prl */
/* Print using Count Range Command: pcr v1 v2 */
/* Print Prefix Command : ppr str */
/* Print Suffix Command : psu str */
/* End Command : end */
/***********************************************/
#include
#include
#include
#define CSIZE 4
#define SSIZE 11
/* Struct for each node in the linked list */
struct node
{
int count;
char symbol[SSIZE];
struct node *next;
};
struct node *head;
/* Prototypes for the functions */
void insert(char *);
void delete(char *);
void forced_delete(int value);
void print_stats();
void print_list();
void print_count(int, int);
void print_prefix(char pre[]);
void print_suffix(char suf[]);
void sort_list();
/****************
** Main Method **
****************/
int main(void)
{
int count;
char symbol[SSIZE];
char cmd[CSIZE];
int v1;
int v2;
/* Enter commands */
printf(\"Command? \"); fflush(stdout);
scanf(\"%s\", cmd);
/* Check for command*/
while(strcmp(cmd,\"end\") != 0)
{
/* check which function to use */
if(strcmp(cmd,\"ins\") == 0) { scanf(\"%s\", symbol); insert(symbol); }
if(strcmp(cmd,\"del\") == 0) { scanf(\"%s\", symbol); delete(symbol); }
else if(strcmp(cmd,\"fde\")==0){scanf(\"%d\",&count); forced_delete(count); }
if(strcmp(cmd,\"pst\") == 0) { print_stats();}
if(strcmp(cmd,\"prl\") == 0) { print_list(); }
if(strcmp(cmd,\"pcr\") == 0) { scanf(\"%d %d\",&v1,&v2); print_count(v1,v2); }
if(strcmp(cmd,\"ppr\") == 0) { scanf(\"%s\", symbol); print_prefix(symbol);}
if(strcmp(cmd,\"psu\") == 0) { scanf(\"%s\", symbol); print_suffix(symbol);}
printf(\"Command? \"); fflush(stdout);
scanf(\"%s\", cmd);
}// End of While Loop
return 0;
}// End of main method
/*********.
Program of sorting using shell sort #include stdio.h #de.pdfanujmkt
/* Program of sorting using shell sort */
#include
#define MAX 20
main()
{
int arr[MAX], i,j,k,n,incr;
printf(\"Enter the number of elements : \");
scanf(\"%d\",&n);
for(i=0;i=1)
{
for(j=incr;j= 0 && k < arr[i]; i = i-incr)
arr[i+incr]=arr[i];
arr[i+incr]=k;
}
printf(\"Increment=%d \ \",incr);
for (i = 0; i < n; i++)
printf(\"%d \", arr[i]);
printf(\"\ \");
incr=incr-2; /*Decrease the increment*/
}/*End of while*/
printf(\"Sorted list is :\ \");
for (i = 0; i < n; i++)
printf(\"%d \", arr[i]);
printf(\"\ \");
}/*End of main()*/
redix sort
/*Program of sorting using radix sort*/
# include
# include
struct node
{
int info ;
struct node *link;
}*start=NULL;
main()
{
struct node *tmp,*q;
int i,n,item;
printf(\"Enter the number of elements in the list : \");
scanf(\"%d\", &n);
for(i=0;iinfo=item;
tmp->link=NULL;
if(start==NULL) /* Inserting first element */
start=tmp;
else
{
q=start;
while(q->link!=NULL)
q=q->link;
q->link=tmp;
}
}/*End of for*/
printf(\"Unsorted list is :\ \");
display();
radix_sort();
printf(\"Sorted list is :\ \");
display ();
}/*End of main()*/
display()
{
struct node *p=start;
while( p !=NULL)
{
printf(\"%d \", p->info);
p= p->link;
}
printf(\"\ \");
}/*End of display()*/
radix_sort()
{
int i,k,dig,maxdig,mindig,least_sig,most_sig;
struct node *p, *rear[10], *front[10];
least_sig=1;
most_sig=large_dig(start);
for(k = least_sig; k <= most_sig ; k++)
{
printf(\"PASS %d : Examining %dth digit from right \",k,k);
for(i = 0 ; i <= 9 ; i++)
{
rear[i] = NULL;
front[i] = NULL ;
}
maxdig=0;
mindig=9;
p = start ;
while( p != NULL)
{
/*Find kth digit in the number*/
dig = digit(p->info, k);
if(dig>maxdig)
maxdig=dig;
if(diglink = p ;
rear[dig] = p ;
p=p->link;/*Go to next number in the list*/
}/*End while */
/* maxdig and mindig are the maximum amd minimum
digits of the kth digits of all the numbers*/
printf(\"mindig=%d maxdig=%d\ \",mindig,maxdig);
/*Join all the queues to form the new linked list*/
start=front[mindig];
for(i=mindig;ilink=front[i+1];
else
rear[i+1]=rear[i];
}
rear[maxdig]->link=NULL;
printf(\"New list : \");
display();
}/* End for */
}/*End of radix_sort*/
/* This function finds number of digits in the largest element of the list */
int large_dig()
{
struct node *p=start ;
int large = 0,ndig = 0 ;
while(p != NULL)
{
if(p ->info > large)
large = p->info;
p = p->link ;
}
printf(\"Largest Element is %d , \",large);
while(large != 0)
{
ndig++;
large = large/10 ;
}
printf(\"Number of digits in it are %d\ \",ndig);
return(ndig);
} /*End of large_dig()*/
/*This function returns kth digit of a number*/
int digit(int number, int k)
{
int digit, i ;
for(i = 1 ; i <=k ; i++)
{
digit = number % 10 ;
number = number /10 ;
}
return(digit);
}/*End of digit()*/
Solution
/* Program of sorting using shell sort */
#include
#define MAX 20
main()
{
int arr[MAX], i,j,k,n,incr;
printf(\"Enter the number of elements : \");
scanf(\"%d\",&n);
for(i=0;i=1)
{
for(j=incr;j= 0 && k < arr[i]; i = i-incr)
arr[i+incr]=arr[i];
arr[i+incr]=k;
}
printf(\.
This is a c++ binary search program I worked so far but still cant g.pdfkostikjaylonshaewe47
This is a c++ binary search program I worked so far but still cant get it right.
Can anyone help me? Big thanks!!
the client should not be modified
/*
*File: client.cpp
*Author: Yingwu Zhu
*Warning: do not change this file and use it as is.
*Last Modififcation: 10/21/2016
*/
#include
#include
#include
#include
#include
#include \"bst.h\"
using namespace std;
int main(int argc, char* argv[]) {
if (argc != 2) {
cout << \"Format: \" << argv[0] << \" [data file]\" << endl;
return 0;
}
ifstream fin(argv[1]);
int cases;
fin >> cases;
int passed = 0;
for (int i = 1; i <= cases; i++) {
cout << \"Checking test case #\" << i << \" ... \";
BST T;
set S;
int n, x;
fin >> n;
bool ok = true;
vector tmp;
int rem = 0;
for (int j = 0; j < n; j++) {
fin >> x;
T.Insert(x);
S.insert(x);
ok &= (T.Search(x) && T.RecurSearch(x));
if (tmp.empty())
tmp.push_back(x);
if (rand()%10 < 3) {
T.Erase(tmp[0]);
S.erase(tmp[0]);
ok &= (T.Search(tmp[0]) == false);
tmp.pop_back();
rem++;
}
}
if (rem
ok &= (T.MaxElement() == *S.rbegin());
while (!S.empty() && !T.Empty()) {
int x = T.MinElement();
ok &= (x == *S.begin());
T.Erase(x);
S.erase(S.begin());
}
cout << (ok ? \"Passed!\" : \"Failed\") << endl;
passed += ok;
}
cout << passed << \" of \" << cases << \" test cases have passed!\ \";
if (passed == cases)
cout << endl << \"Congratulations! Good to Submit Your Code\ \";
else if ((double)passed/cases >= 0.95)
cout << endl << \"You are almost there, but need to fix some bugs.\ \";
else
cout << endl << \"Your code needs a lot of fixes for submission\ \";
return 0;
}
=====================================================================
=======
//bst.h
#ifndef _BST_H_
#define _BST_H_
#include
using namespace std;
class BST {
private:
class Node {
public:
int data;
Node* left;
Node* right;
};
Node* root; //root node pointer
//you may add your auxiliary functions here!
public:
BST(); //constructor
~BST(); //destructor
bool Empty() const;
void Insert(int val);
int MinElement() const;
int MaxElement() const;
bool Search(int val) const;
bool RecurSearch(int val) const;
void Erase(int val);
};
#endif
=====================================================================
=======
/*
bst.cpp
Subject: Binary Seach Tree
Modification Time:10/26/2016
*/
#include
#include\"bst.h\"
using namespace std;
BST::BST(){
root= NULL;
}
BST::~BST(){
delete root;
delete root->left;
delete root->right;
}
bool BST::Empty() const {
return data;
}
void BST::Insert(int val){
Node* p= new Node;
p->data= val;
Node* cur = root;
if(val < cur->data){
cur = cur -> left;
}
else{
right->next=val;
}
}
int BST::MinElement() const {
Node* cur;
while(cur->left != NULL){
cur = cur->left;
}
return(cur->data);
}
int BST::MaxElement() const{
if(root==NULL){
return 0;
}
if(root->left>root->data){
root->data=root->right;
}
else if(root->right>root->data){
root->data=root->right;
}
return root->data;
}
bool BST:: Search(int val) const{
Node* cur;
cur = root;
while(cur!=NULL){
if(cur->data == val){
return true;
}
else if(cur->data .
Describe a data structure that supports both removeMin() and rem.pdfarihantstoneart
Describe a data structure that supports both removeMin() and removeMax() with O(log(n))
complexity. You need to show the implementation of insert(), removeMin(), and removeMax(),
and prove their complexity. Hint: one can build on top of the heap.
/*IF YOU WRITE CODE, PLEASE PROVIDE COMMENTS*/
I need to understand
Describe a data structure that supports both removeMin() and removeMax() with O(log(n))
complexity. You need to show the implementation of insert(), removeMin(), and removeMax(),
and prove their complexity. Hint: one can build on top of the heap.
/*IF YOU WRITE CODE, PLEASE PROVIDE COMMENTS*/
I need to understand
Describe a data structure that supports both removeMin() and removeMax() with O(log(n))
complexity. You need to show the implementation of insert(), removeMin(), and removeMax(),
and prove their complexity. Hint: one can build on top of the heap.
/*IF YOU WRITE CODE, PLEASE PROVIDE COMMENTS*/
I need to understand
Solution
The idea here is to use the concept of two binary heaps along with the concept of list(doubly
linked list).
The doubly linked list contains all input items and indexes of corresponding min and max heap
nodes. The nodes of min and max heaps store addresses of nodes of doubly linked list. The root
node of min heap stores the address of minimum item in doubly linked list. Similarly, root of
max heap stores address of maximum item in doubly linked list. Following are the details of
operations.
1) findMax(): We get the address of maximum value node from root of Max Heap. So this is a
O(1) operation.
1) findMin(): We get the address of minimum value node from root of Min Heap. So this is a
O(1) operation.
3) removeMin(): We get the address of minimum value node from root of Min Heap. We use this
address to find the node in doubly linked list. From the doubly linked list, we get node of Max
Heap. We delete node from all three. We can delete a node from doubly linked list in O(1) time.
delete() operations for max and min heaps take O(Logn) time.
4) removeMax(): is similar to deleteMin()
5) Insert() We always insert at the beginning of linked list in O(1) time. Inserting the address in
Max and Min Heaps take O(Logn) time. So overall complexity is O(Logn)
#include
#include
#include
// A node of doubly linked list
struct LNode
{
int data;
int minHeapIndex;
int maxHeapIndex;
struct LNode *next, *prev;
};
// Structure for a doubly linked list
struct List
{
struct LNode *head;
};
// Structure for min heap
struct MinHeap
{
int size;
int capacity;
struct LNode* *array;
};
// Structure for max heap
struct MaxHeap
{
int size;
int capacity;
struct LNode* *array;
};
// The required data structure
struct CDS
{
struct MinHeap* minHeap;
struct MaxHeap* maxHeap;
struct List* list;
};
// A utility function to create a new List node
struct LNode* newLNode(int data)
{
struct LNode* node =
(struct LNode*) malloc(sizeof(struct LNode));
node->minHeapIndex = node->maxHeapIndex = -1;
node->data = data;
node->prev = node->next = NULL;
retu.
1. C/C++
Create a inked list of up to 20 peripherals.
Hold data about peripheral in a structure.
Provide for adding new peripherals, finding and removing them.
Minimize code by leveraging code reuse.
struct peripheral
{
struct peripheral *next;
char *name;
char *description;
}
#define new(a) malloc(sizeof(struct a));
#define MAX_P 20;
int total = 0;
main
{
struct peripherial *root;
root = malloc(sizeof(struct peripheral));
(*root).next, or
root->next = new(peripheral);
root->name = “keyboard”;
root->next-> = “mouse”;
// find the mouse peripheral
struct peripherial *mouse = findByNameLast(root,”mouse”)->next; // ->next =’s findByName;