Skip to content

Instantly share code, notes, and snippets.

@veritem
Created July 4, 2022 21:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save veritem/4ee93e98a0b311d927b7800b228387c2 to your computer and use it in GitHub Desktop.
Save veritem/4ee93e98a0b311d927b7800b228387c2 to your computer and use it in GitHub Desktop.
/// Files handling
int main()
{
fstream new_file;
new_file.open("new_file.txt", ios::in);
if (!new_file.is_open())
cout << "No such file";
else
{
while (!new_file.eof())
{
char ch;
ch = new_file.get();
cout << ch;
}
}
new_file.close();
return 0;
}
/////
/// linked list
struct Node
{
int data;
Node *next;
};
class LinkedList
{
private:
Node *head;
Node *tail;
public:
LinkedList()
{
head = NULL;
tail = NULL;
}
void add_node(int n)
{
Node *temp = new Node;
temp->data = n;
temp->next = NULL;
if (head == NULL)
{
head = temp;
tail = temp;
}
else
{
tail->next = temp;
tail = temp;
}
}
void get_list()
{
Node *temp = head;
while (temp != NULL)
{
cout << temp->data << " ";
temp = temp->next;
}
cout << endl;
}
bool search(int n)
{
Node *temp = head;
while (temp != NULL)
{
if (temp->data == n)
{
return true;
}
temp = temp->next;
}
return false;
}
void sort()
{
Node *temp = head;
Node *temp2 = head;
Node *temp3 = head;
int temp_data;
while (temp != NULL)
{
temp2 = temp;
while (temp2 != NULL)
{
if (temp->data > temp2->data)
{
temp_data = temp->data;
temp->data = temp2->data;
temp2->data = temp_data;
}
temp2 = temp2->next;
}
temp = temp->next;
}
}
void delete_node(int n)
{
Node *temp = head;
Node *temp2 = head;
if (temp->data == n)
{
head = temp->next;
delete temp;
return;
}
while (temp != NULL)
{
if (temp->data == n)
{
temp2->next = temp->next;
delete temp;
return;
}
temp2 = temp;
temp = temp->next;
}
}
};
int main()
{
LinkedList list;
list.add_node(2);
list.add_node(1);
list.add_node(3);
list.get_list();
// cout << "Does 2 exists in a linked list: " << list.search(2) << endl;
// cout << "Does 100 exists in a linked list: " << list.search(100) << endl;
// list.sort();
// list.get_list();
// list.delete_node(2);
// list.get_list();
return 0;
}
/////
/// merge arrays
void mergeArrays(int arr1[], int arr2[], int n1,
int n2, int arr3[])
{
int i = 0, j = 0, k = 0;
// Traverse both array
while (i < n1 && j < n2)
{
// Check if current element of first
// array is smaller than current element
// of second array. If yes, store first
// array element and increment first array
// index. Otherwise do same with second array
if (arr1[i] < arr2[j])
arr3[k++] = arr1[i++];
else
arr3[k++] = arr2[j++];
}
// Store remaining elements of first array
while (i < n1)
arr3[k++] = arr1[i++];
// Store remaining elements of second array
while (j < n2)
arr3[k++] = arr2[j++];
}
// main function code
int main()
{
// declaring the variables for the program
int arr1[] = {1, 3, 5, 9, 2, 7};
int n1 = sizeof(arr1) / sizeof(arr1[0]);
int arr2[] = {2, 4, 6, 8};
int n2 = sizeof(arr2) / sizeof(arr2[0]);
int arr3[n1 + n2];
mergeArrays(arr1, arr2, n1, n2, arr3);
cout << "Array after merging: ";
for (int i = 0; i < n1 + n2; i++)
cout << arr3[i] << " ";
return 0;
}
////
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment