how can I disable windows micphone enhancement by c++ code? - windows-core-audio

is there a way to disable the microphone audio enhancements from C++ code?
We are doing some audio processing things on the incoming microphone signal, and the microphone enhancements cause all kinds of trouble. We know we can turn it off manually on Windows10 by going to the Control panel --> Sound --> Recording --> Microphone --> Properties --> advanced--> Enhancements, but we need a way to do this from code.
enter image description here
when I try to access it via C++ It comes back saying there is no AGC on the device.
void AudioDeviceWindowsCore::DisableHardwareAGC() {
// get device topology object for that endpoint
CComPtr<IDeviceTopology> pTopo = NULL;
HRESULT ret = _ptrDeviceIn->Activate(__uuidof(IDeviceTopology), CLSCTX_ALL, NULL,
(void**)&pTopo);
if (FAILED(ret)) {
RTC_LOG(LS_ERROR) << "Couldn't get device topology object: hr = 0x"
<< rtc::ToHex(ret);
} else {
UINT count = 0;
pTopo->GetConnectorCount(&count);
CComPtr<IConnector> pConn = NULL;
ret = pTopo->GetConnector(0, &pConn);
if (FAILED(ret)) {
RTC_LOG(LS_ERROR) << __FUNCTION__ << " GetConnector:" << rtc::ToHex(ret);
return ;
}
CComPtr<IConnector> pConnNext = NULL;
ret = pConn->GetConnectedTo(&pConnNext);
if (FAILED(ret)) {
RTC_LOG(LS_ERROR) << __FUNCTION__
<< " GetConnectedTo:" << rtc::ToHex(ret);
return ;
}
CComPtr<IPart> pPart = NULL;
const IID IID_IPart = __uuidof(IPart);
ret = pConnNext->QueryInterface(IID_IPart, (void**)&pPart);
if (FAILED(ret)) {
RTC_LOG(LS_ERROR) << __FUNCTION__
<< " QueryInterface:" << rtc::ToHex(ret);
return ;
}
// all the real work is done in this function
ret = WalkTreeForwardsFromPart(pPart);
if (SUCCEEDED(ret)) {
RTC_LOG(INFO) << " now close hardware AGC ret:" << rtc::ToHex(ret);
} else {
RTC_LOG(INFO) << " Hardware not Supports Microphone AGC";
}
}
}
HRESULT WalkTreeForwardsFromPart(IPart* pPart) {
HRESULT hr = S_OK;
LPWSTR pwszPartName = NULL;
hr = pPart->GetName(&pwszPartName);
if (FAILED(hr)) {
RTC_LOG(LS_ERROR) << "Could not get part name: hr = 0x"<<rtc::ToHex(hr);
return hr;
}
RTC_LOG(INFO) << "Part name: " << *pwszPartName ? pwszPartName : L"Unnamed";
CoTaskMemFree(pwszPartName);
GUID type_id;
hr = pPart->GetSubType(&type_id);
if (hr == S_OK){
if (IsEqualGUID(type_id,KSNODETYPE_LOUDNESS) ) {
const IID IID_IAudioLoudness = __uuidof(IAudioLoudness);
IAudioLoudness* loudness = NULL;
hr = pPart->Activate(CLSCTX_ALL, IID_IAudioLoudness, (void**)&loudness);
if (E_NOINTERFACE == hr) {
RTC_LOG(LS_ERROR) << "NO AudioLoudness CONTROL";
// not a Microphone node
} else if (FAILED(hr)) {
RTC_LOG(LS_ERROR) << "Unexpected failure trying to activate "
"IID_IAudioLoudness : hr = 0x"
<< rtc::ToHex(hr);
} else {
// it's an AGC node...
RTC_LOG(INFO) << "HAS IAudioLoudness CONTROL";
hr = loudness->SetEnabled(0, NULL);
if (FAILED(hr)) {
RTC_LOG(LS_ERROR)
<< "AudioLoudness Failed: hr = 0x" << rtc::ToHex(hr);
loudness->Release();
return hr;
}
loudness->Release();
}
} else if (IsEqualGUID(type_id, KSNODETYPE_AGC)) {
// Check AGC settings
const IID IID_IAudioAutoGainControl = __uuidof(IAudioAutoGainControl);
IAudioAutoGainControl* aGCcontrol = NULL;
hr = pPart->Activate(CLSCTX_ALL, IID_IAudioAutoGainControl,
(void**)&aGCcontrol);
if (E_NOINTERFACE == hr) {
RTC_LOG(LS_ERROR) << "NO AGC CONTROL";
// not a Microphone node
} else if (FAILED(hr)) {
RTC_LOG(LS_ERROR) << "Unexpected failure trying to activate "
"IAudioAutoGainControl : hr = 0x"
<< rtc::ToHex(hr);
return hr;
} else {
// it's an AGC node...
RTC_LOG(INFO) << "HAS AGC CONTROL";
aGCcontrol->SetEnabled(0, NULL);
if (FAILED(hr)) {
RTC_LOG(LS_ERROR) << "AGC Failed: hr = 0x" << rtc::ToHex(hr);
aGCcontrol->Release();
return hr;
}
aGCcontrol->Release();
}
}
}
// get the list of incoming parts
IPartsList* pOutgoingParts = NULL;
hr = pPart->EnumPartsOutgoing(&pOutgoingParts);
if (E_NOTFOUND == hr) {
// not an error... we've just reached the end of the path
RTC_LOG(INFO) << "No incoming parts at this part";
return hr;
}
if (FAILED(hr)) {
RTC_LOG(LS_ERROR) << " Couldn't enum outgoing parts";
return hr;
}
UINT nParts = 0;
hr = pOutgoingParts->GetCount(&nParts);
if (FAILED(hr)) {
RTC_LOG(LS_ERROR) << "Couldn't get count of outgoing parts";
pOutgoingParts->Release();
return hr;
}
// walk the tree on each incoming part recursively
for (UINT n = 0; n < nParts; n++) {
IPart* pOutgoingPart = NULL;
hr = pOutgoingParts->GetPart(n, &pOutgoingPart);
if (FAILED(hr)) {
RTC_LOG(LS_ERROR) << "Couldn't get part ";
pOutgoingParts->Release();
return hr;
}
hr = WalkTreeForwardsFromPart(pOutgoingPart);
if (FAILED(hr)) {
RTC_LOG(LS_ERROR) << " Couldn't walk tree on part";
pOutgoingPart->Release();
pOutgoingParts->Release();
return hr;
}
pOutgoingPart->Release();
}
pOutgoingParts->Release();
return S_OK;
}

Related

invalid conversion from 'int' to 'int*' [-fpermissive]

I am new to C++ language and I have no idea of pointers and their usage. I'm facing the error "[Error] invalid conversion from 'int' to 'int*' [-fpermissive]" while compiling.
Here is the code.
#include <iostream>
#define TRUE 1
#define FALSE 0
using namespace std;
class tree
{
private:
struct node
{
node *l;
int data;
node *r;
} *p;
public:
tree();
void search(int n, int &found, int *&parent);
void insert(int n);
void traverse();
void in(node *q);
void pre(node *q);
void post(node *q);
int compare(node *pp, node *qq);
void operator=(tree t);
int operator==(tree t);
node *copy(node *q);
};
tree ::tree()
{
p = NULL;
}
void tree ::search(int n, int &found, int *&parent)
{
node *q;
found = FALSE;
parent = TRUE;
if (p == NULL)
return;
q = p;
while (q != NULL)
{
if (q->data == n)
{
found = TRUE;
return;
}
if (q->data > n)
{
parent = q;
q = q->l;
}
else
{
parent = q;
q = q->r;
}
}
}
void tree::insert(int n)
{
int found;
node *t, *parent;
search(n, found, parent);
if (found == TRUE)
{
cout << endl << "Such a node already exists";
}
else
{
t = new node;
t->data = n;
t->l = NULL;
t->r = NULL;
if (parent == NULL)
{
p = t;
}
else
{
parent->data > n ? parent->l : parent->r = t;
}
}
}
void tree::traverse()
{
int choice;
cout << endl
<< "1. Inorder" << endl
<< "2. Preorrder" << endl
<< "3. Postorder" << endl
<< "4. Your choice";
cin >> choice;
switch (choice)
{
case 1:
in(p);
break;
case 2:
pre(p);
break;
case 3:
post(p);
break;
}
}
void tree::in(node *q)
{
if (q != NULL)
{
in(q->l);
cout << "\t" << q->data;
in(q->r);
}
}
void tree::pre(node *q)
{
if (q != NULL)
{
cout << "\t" << q->data;
pre(q->l);
pre(q->r);
}
}
void tree ::post(node *q)
{
if (q != NULL)
{
post(q->l);
post(q->r);
cout << "\t" << q->data;
}
}
int tree::operator==(tree t)
{
int flag;
flag = compare(p, t.p);
return (flag);
}
int tree::compare(node *pp, node *qq)
{
static int flag;
if ((pp == NULL) && (qq != NULL))
{
if ((pp != NULL) && (qq != NULL))
{
if (pp->data != qq->data)
{
flag = FALSE;
}
else
{
compare(pp->l, qq->l);
compare(pp->r, qq->r);
}
}
}
return (flag);
}
void tree::operator=(tree t)
{
p = copy(t.p);
}
tree::node *tree::copy(node *q)
{
if (q != NULL)
{
t = new node;
t->data = q->data;
t->l = copy(q->l);
t->r= copy(q->r);
return (t);
}
else
{
return(NULL);
}
}
int main()
{
tree tt, ss;
int i, num;
for (i = 0; i <= 6; i++)
{
cout << endl
<< "Enter the data for the node to be inserted";
cin >> num;
tt.insert(num);
}
tt.traverse();
ss = tt;
ss.traverse();
if (ss == tt)
cout << endl
<< "Trees are equal";
else
cout << endl
<< "Trees are not equal";
return 0;
}
I was trying to implement Binary search tree using class and traverse the tree using any traversal scheme. In addition to it the class must have capability to copy the contents from one tree to another and compare the contents of two binary trees.

Getting Bluetooth LMP version via Powershell

I have taken a look at CIM_NetworkAdapter, Get-NetAdapterAdvancedProperty CIM_LogicalDevice, Win32_PnPDevice and more.
How can I go about grabbing the LMP version of a Bluetooth adapter from its "Bluetooth Radio Information" in its advanced properties?
Is this possible in Regedit -or any other way?
EDIT,
ANSWER:
Here's a modified version of an answer I received on Reddit from u/ChaosTheoryRules
$BTDevices = Get-PnpDevice -Class "Bluetooth"
Foreach ($device in $BTDevices) {
$BTDeviceProperty = Get-PnpDeviceProperty -KeyName "DEVPKEY_Bluetooth_RadioLmpVersion" -InstanceId $device.InstanceID
If ($BTDeviceProperty -And $BtDeviceProperty.Data) {
$bluetoothVersion = Switch ($BTDeviceProperty.Data) {
0 { '1.0b' }
1 { '1.1' }
2 { '1.2' }
3 { '2.0 + EDR' }
4 { '2.1 + EDR' }
5 { '3.0 + HS' }
6 { '4.0' }
7 { '4.1' }
8 { '4.2' }
9 { '5.0'}
10 { '5.1' }
11 { '5.2' }
default { "UKNOWN" }
}
}
}
Write-Host $bluetoothVersion
https://www.reddit.com/r/PowerShell/comments/vmexhm/getting_bluetooth_lmp_version_via_powershell/
I don't have the equipment to test, so I can only provide some reference documentation.
You can use BluetoothGetRadioInfo and the structure BLUETOOTH_RADIO_INFO to get relevant information lmpSubversion.
#include <windows.h>
#include <BluetoothAPIs.h>
#include <conio.h>
#include <iostream>
#include <string>
#include <locale>
#pragma comment(lib,"Bthprops.lib")
using namespace std;
int main(void)
{
wcout.imbue(locale(""));
HBLUETOOTH_RADIO_FIND hbf = NULL;
HANDLE hbr = NULL;
HBLUETOOTH_DEVICE_FIND hbdf = NULL;
BLUETOOTH_FIND_RADIO_PARAMS btfrp = { sizeof(BLUETOOTH_FIND_RADIO_PARAMS) };
BLUETOOTH_RADIO_INFO bri = { sizeof(BLUETOOTH_RADIO_INFO)};
BLUETOOTH_DEVICE_SEARCH_PARAMS btsp = { sizeof(BLUETOOTH_DEVICE_SEARCH_PARAMS) };
BLUETOOTH_DEVICE_INFO btdi = { sizeof(BLUETOOTH_DEVICE_INFO) };
hbf=BluetoothFindFirstRadio(&btfrp, &hbr);
bool brfind = hbf != NULL;
while (brfind)
{
if (BluetoothGetRadioInfo(hbr, &bri) == ERROR_SUCCESS)
{
cout << "Class of device: 0x" << uppercase << hex << bri.ulClassofDevice << endl;
wcout <<"Name:"<< bri.szName << endl;
cout <<"Manufacture:0x"<< uppercase << hex << bri.manufacturer << endl;
cout << "Subversion:0x" << uppercase << hex << bri.lmpSubversion << endl;
//
btsp.hRadio = hbr;
btsp.fReturnAuthenticated = TRUE;
btsp.fReturnConnected = FALSE;
btsp.fReturnRemembered = TRUE;
btsp.fReturnUnknown = TRUE;
btsp.cTimeoutMultiplier = 30;
hbdf=BluetoothFindFirstDevice(&btsp, &btdi);
bool bfind = hbdf != NULL;
while (bfind)
{
wcout << "[Name]:" << btdi.szName;
cout << ",[Address]:0x" << uppercase << hex << btdi.Address.ullLong << endl;
bfind=BluetoothFindNextDevice(hbdf, &btdi);
}
BluetoothFindDeviceClose(hbdf);
}
CloseHandle(hbr);
brfind=BluetoothFindNextRadio(hbf, &hbr);
}
BluetoothFindRadioClose(hbf);
_getch();
return 0;
}

Secure Socket with Poco

I am implementing a tcp server and client using secure sockets (Poco::Net::SecureServerSocket), I attach here the code I am using:
void serverClientTest()
{
try {
Poco::Net::initializeSSL();
// Socket server
Poco::Net::Context::Ptr ptrContext =
new Poco::Net::Context(Poco::Net::Context::TLS_SERVER_USE,
"./cert4/myKey.pem",
"./cert4/myCert.pem",
"./cert4/myCert.pem",
Poco::Net::Context::VERIFY_ONCE);
Poco::SharedPtr<Poco::Net::InvalidCertificateHandler> ptrCert = new Poco::Net::AcceptCertificateHandler(true);
Poco::SharedPtr<Poco::Net::PrivateKeyPassphraseHandler> ptrPrivateKeyPassphraseHandler;
ptrPrivateKeyPassphraseHandler = new Poco::Net::KeyConsoleHandler(true);
Poco::Net::SSLManager::instance().initializeServer(ptrPrivateKeyPassphraseHandler, ptrCert, ptrContext);
Poco::Net::SocketAddress serverAddress("0.0.0.0", 8085);
Poco::Net::SecureServerSocket serverSecureSocket(serverAddress);
Poco::Net::TCPServer srv(new Poco::Net::TCPServerConnectionFactoryImpl<EchoConnection>(), serverSecureSocket);
srv.start();
Poco::Net::Context::Ptr ptrContext2 =
new Poco::Net::Context(Poco::Net::Context::TLS_CLIENT_USE,
"./cert4/myKey.pem",
"./cert4/myCert.pem",
"./cert4/myCert.pem",
Poco::Net::Context::VERIFY_ONCE);
Poco::SharedPtr<Poco::Net::InvalidCertificateHandler> ptrCert2 = new Poco::Net::AcceptCertificateHandler(true);
Poco::SharedPtr<Poco::Net::PrivateKeyPassphraseHandler>ptrPrivateKeyPassphraseHandler2(new Poco::Net::KeyConsoleHandler(false));
Poco::Net::SSLManager::instance().initializeClient(ptrPrivateKeyPassphraseHandler2, ptrCert2, ptrContext2);
Poco::Net::SocketAddress sa("127.0.0.1", 8085);
Poco::Net::SecureStreamSocket ss1(sa);
std::string data("TEST TEST");
int retSend = ss1.sendBytes(data.data(), (int) data.size());
if (retSend>0)
{
std::cout << "buffer -> : " << data.data() << std::endl;
char buffer[1024];
memset(buffer, '\0', 1024);
int retRecv = ss1.receiveBytes(buffer, sizeof(buffer));
if (retRecv > 0)
{
std::cout << "buffer <- : " << buffer << std::endl;
}
else
{
std::cout << "ERROR: recv " << retRecv << std::endl;
}
}
ss1.close();
}
catch (Poco::Exception& ex)
{
std::cout << "!! EXCEPTION "<< ex.displayText() << std::endl;
}
}
//[....]
class EchoConnection: public Poco::Net::TCPServerConnection
{
public:
EchoConnection(const Poco::Net::StreamSocket& s): Poco::Net::TCPServerConnection(s){}
void run()
{
Poco::Net::StreamSocket& ss = socket();
std::cout << "connection from client: " << ss.address() << std::endl;
try
{
// ...
}
catch (Poco::Exception& exc)
{
std::cerr << "--------------- EchoConnection: " << exc.displayText() << std::endl;
}
}
};
I would like the server to close the connection if the client certificate is not known to the server, but it happens
which, even with the context:
Poco::Net::Context::Ptr ptrContext2 =
new Poco::Net::Context(Poco::Net::Context::TLS_CLIENT_USE,
"",
"",
"./cert4/myCert.pem",
Poco::Net::Context::VERIFY_ONCE);
Thanks to anyone who can help me out.
void serverClientTest()
{
try {
Poco::Net::initializeSSL();
// Socket server
Poco::Net::Context::Ptr ptrContext =
new Poco::Net::Context(Poco::Net::Context::SERVER_USE,
"./server.key",
"./server.crt",
"./ca.pem",
Poco::Net::Context::VERIFY_STRICT,
9,
false,
"ALL:!ADH:!LOW:!EXP:!MD5:#STRENGTH");
Poco::SharedPtr<Poco::Net::InvalidCertificateHandler> ptrCert = new Poco::Net::AcceptCertificateHandler(true);
Poco::SharedPtr<Poco::Net::PrivateKeyPassphraseHandler> ptrPrivateKeyPassphraseHandler;
ptrPrivateKeyPassphraseHandler = new Poco::Net::KeyConsoleHandler(true);
Poco::Net::SSLManager::instance().initializeServer(ptrPrivateKeyPassphraseHandler, ptrCert, ptrContext);
Poco::Net::SocketAddress serverAddress("0.0.0.0", 8085);
Poco::Net::SecureServerSocket serverSecureSocket(serverAddress);
Poco::Net::TCPServer srv(new Poco::Net::TCPServerConnectionFactoryImpl<EchoConnection>(), serverSecureSocket);
srv.start();
Poco::Net::Context::Ptr ptrContext2 =
new Poco::Net::Context(Poco::Net::Context::CLIENT_USE,
"./client.key",
"./client.crt",
"./ca.pem",
Poco::Net::Context::VERIFY_STRICT,
9,
true,
"ALL:!ADH:!LOW:!EXP:!MD5:#STRENGTH");
Poco::SharedPtr<Poco::Net::InvalidCertificateHandler> ptrCert2 = new Poco::Net::AcceptCertificateHandler(true);
Poco::SharedPtr<Poco::Net::PrivateKeyPassphraseHandler>ptrPrivateKeyPassphraseHandler2(new Poco::Net::KeyConsoleHandler(false));
Poco::Net::SSLManager::instance().initializeClient(ptrPrivateKeyPassphraseHandler2, ptrCert2, ptrContext2);
Poco::Net::SocketAddress sa("127.0.0.1", 8085);
Poco::Net::SecureStreamSocket ss1(sa);
std::string data("TEST");
int retSend = ss1.sendBytes(data.data(), (int) data.size());
if (retSend>0)
{
char buffer[1024];
memset(buffer, '\0', 1024);
int retRecv = ss1.receiveBytes(buffer, sizeof(buffer));
if (retRecv > 0)
{
std::cout << "buffer <-: " << buffer << std::endl;
}
else
{
std::cout << "ERROR: " << retRecv << std::endl;
}
}
ss1.close();
}
catch (Poco::Exception& ex)
{
std::cout << ex.displayText() << std::endl;
}
}

Finding the smallest and largest numbers by single linked list and two classes

I got several numbers from user by single linked list and my program's task is finding the smallest and largest number in the linked list by two classes and print them on the sreen. But after a time, my program got closed and i didn't see anything. What went wrong?
#include<iostream>
using namespace std;
struct Node
{
double Number;
struct Node *Point;
} *End = nullptr;
typedef struct Node node;
namespace Min_Max
{
class Min
{
node *Result = End;
public: Min()
{
if(Result == nullptr)
{
cout << "You didn\'t enter anything!\a";
system("pause");
exit(EXIT_FAILURE);
}
node *Counter = Result->Point;
while(Counter != nullptr)
{
if(Counter->Number < Result->Number)
Result = Counter;
Result = Result->Point;
}
}
node* Show()
{
return Result;
}
};
class Max
{
private:
node *Result = End;
public:
Max()
{
if(Result == nullptr)
{
cout << "You didn\'t enter anything!\a";
system("pause");
exit(EXIT_FAILURE);
}
node *Counter = Result->Point;
while(Counter != nullptr)
{
if(Counter->Number > Result->Number)
Result = Counter;
Result = Result->Point;
}
}
node* Show()
{
return Result;
}
};
};
int main()
{
node *linker = nullptr;
register short int Counter = 1;
while(1)
{
linker = new node;
if(linker == nullptr)
{
cout << "An error occurred during allocating memory." << endl << endl;
system("pause");
return 0;
}
cout << "Number " << Counter << ": Enter your number: ";
cin >> linker->Number;
system("cls");
if(linker->Number == 0)
{
delete linker;
break;
}
linker->Point = End;
End = linker;
Counter++;
}
Min_Max::Min Min;
Min_Max::Max Max;
cout << "The smallest number is " << (Min.Show())->Number << endl;
cout << "The largest number is " << (Max.Show())->Number << endl;
return 0;
}
My C++ compiler is GCC-C++11 and my operating system is Windows 10.

Printing a singly linked list using classes

I am making a program for singly linked lists using multiple files and classes.
I have to have a Node.h, LinkedList.h, Node.cpp, LinkedList.cpp, and a main.cpp
I was having other problems but now my printList() function just prints "List()" instead of "List(node 1, node2, etc...)"
I think my insert might be the problem because my searchNode() doesn't work right either, it always says node not found.
Here is the code I have: (I can't change the Node.h and LinkedList.h files)
Node.h:
//
// Node.h
// Linked Lists
//
#ifndef Linked_Lists_Node_h
#define Linked_Lists_Node_h
class Node
{
public:
Node(int data);
int data;
Node *next;
};
#endif
LinkedList.h:
//
// LinkedList.h
// Linked Lists
//
#ifndef Linked_Lists_LinkedList_h
#define Linked_Lists_LinkedList_h
#include "Node.h"
class LinkedList
{
private:
Node *head;
public:
LinkedList();
void addNode(int data);
void removeNode(int data);
bool searchNode(int data);
void printList();
};
#endif
Node.cpp
//
// Node.cpp
// Linked Lists
//
#include <iostream>
#include <cstdlib>
#include "LinkedList.h"
#include "Node.h"
using namespace std;
Node::Node(int data) {};
LinkedList.cpp
//
// LinkedList.cpp
// Linked Lists
//
#include <iostream>
#include <cstdlib>
#include "LinkedList.h"
#include "Node.h"
using namespace std;
LinkedList::LinkedList()
{
head = NULL;
}
void LinkedList::addNode(int data)
{
Node *newNode;
newNode->data = data;
newNode->next = NULL;
Node *tmp = head;
if(tmp != NULL)
{
while(tmp->next != NULL)
{
tmp = tmp->next;
}
tmp->next = newNode;
}
cout << "Node added" << endl;
printList();
}
void LinkedList::removeNode(int data)
{
Node *tmp = head;
if(tmp == NULL)
{
cout << "No node removed" << endl;
return;
}
if(tmp->next == NULL)
{
delete tmp;
head = NULL;
}
else
{
Node *previous;
do
{
if(tmp->data == data)
{
break;
}
previous = tmp;
tmp = tmp->next;
}
while(tmp != NULL);
previous->next = tmp->next;
delete tmp;
}
cout << "Node removed" << endl;
printList();
}
bool LinkedList::searchNode(int data)
{
Node *tmp = head;
while(tmp != NULL)
{
if(tmp->data == data)
{
cout << "Node found" << endl;
printList();
return true;
}
tmp = tmp->next;
}
cout << "Node not found" << endl;
printList();
return false;
}
void LinkedList::printList()
{
Node *tmp = head;
if(tmp == NULL)
{
cout << "List()" << endl;
return;
}
if(tmp->next == NULL)
{
cout << "List(" << tmp->data << ")";
}
else
{
do
{
cout << "List(" << tmp->data;
cout << ", ";
tmp = tmp->next;
}
while (tmp != NULL);
cout << ")" << endl;
}
}
main.cpp
//
// main.cpp
// Linked Lists
//
#include <iostream>
#include <cstdlib>
#include "LinkedList.h"
#include "Node.h"
#include "LinkedList.cpp"
using namespace std;
int main ()
{
LinkedList list;
int data;
int choice;
while(1)
{
cout << " Select:" << endl;
cout << "1 to add a node" <<endl;
cout << "2 to remove a node" << endl;
cout << "3 to search for a node" << endl;
cout << "4 to exit" << endl;
cout << endl;
cin >> choice;
switch(choice)
{
case 1: //insertion
cout << "Enter node: ";
cin >> data;
list.addNode(data); //add a node
break;
case 2: //deletion
cout << "Enter node: ";
cin >> data;
list.removeNode(data); //remove a node
break;
case 3: //search
cout << "Enter node: ";
cin >> data;
list.searchNode(data); //search for a node
break;
case 4:
exit(0); //exit the program
break;
default: //default case
cout << "Please enter a valid choice (1 - 4)!" << endl;
break;
}
}
return 0;
}
If you could help me figure out my problem I would greatly appreciate it.
You're not adding any nodes. If head is NULL, your add Node becomes:
void LinkedList::addNode(int data)
{
Node *newNode;
newNode->data = data;
newNode->next = NULL;
Node *tmp = head;
if(tmp != NULL)
{
//this never gets executed
}
cout << "Node added" << endl;
printList();
}
You need to treat this case (first insertion):
void LinkedList::addNode(int data)
{
Node *newNode;
newNode->data = data;
newNode->next = NULL;
Node *tmp = head;
if(tmp != NULL)
{
while(tmp->next != NULL)
{
tmp = tmp->next;
}
tmp->next = newNode;
}
else
{
head = newNode;
}
cout << "Node added" << endl;
printList();
}
This will create the head if it doesn't exist already.
Are you using a debugger? It would be much easier (for you) if you did.