I'm tring to create a deque and list the elements in it. I've made two functions for this: create_queue and print_queue. I've sent the pointers (head and tail) as reference so I don't have to declare them globally.
#include<iostream>
using namespace std;
struct Nod {
int info;
Nod* next, *back;
};
void create_queue(Nod*& p, Nod*& u)
{
Nod *c = new Nod;
cout << "c->info: "; cin >> c->info;
if (!p)
{
p = c;
p->back = NULL;
p->next = NULL;
u = p;
}
else
{
u->next = c;
c->back = u;
u = c;
u->next = NULL;
}
}
void print_queue(Nod*& p, Nod*& u)
{
Nod *c = p;
while (c) {
cout << c->info << " ";
c = c->next;
}
}
int main()
{
int n, i = 1;
Nod *p, *u = new Nod;
cout << "Nr nod: "; cin >> n;
while (i <= n){
create_queue(p, u);
i++;
}
print_queue(p, u);
getchar();
}
From what I understand from the debugger, I'm not able to access p->info.
What is the problem?
To pass an object that can be modified to a function you don't need that ugly Nod*& (address of a reference!) type. Use a common pointer Nod* instead.
Related
#include <iostream>
#include <cmath>
using namespace std;
class node {
public:
int data;
node* next;
node(int d) {
data = d;
next = NULL;
}
};
void insertAtTail(node*& head, int d) {
node* temp = head;
node* n = new node(d);
n->next = NULL;
if (head == NULL) {
head = n;
} else {
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = n;
}
}
void buildList(node*& head, int t) {
int data;
cin >> data;
int q = 1;
while (q != t + 1) {
insertAtTail(head, data);
cin >> data;
q++;
}
return;
}
int main() {
node* head = NULL;
int t, q;
cin >> t;
buildList(head, t);
int u;
cin >> u;
cout << u << endl;
print(head);
}
After making an input function for the Linked List (buildList) , whenever I declare another variable (int u) and take input, it always takes a garbage value and does not take the inputted value. Please review it and tell me where am I wrong
Being a beginner I tried this single linked list program to accept and display first to last elements.I can't figure out what is wrong.After you run it the program stops responding after taking in the first element. I am not very familiar with the language and am new to pointer concept. This was an assignment work.
#include <iostream>
using namespace std;
struct node
{
int data;
node* next;
};
class alpha
{
public:
node* head;
node* last;
node* n;
node* p;
int x;
char ch;
void input()
{
cout << "Enter the element..";
cin >> x;
insert(x);
cout << "Do you want to add more?";
cin >> ch;
if (ch == 'y')
{
input();
}
else
{
display();
}
}
void insert(int x1)
{
n = new node;
n->data = x1;
if (head == NULL)
{
head = n;
last = n;
}
else
{
n->next = NULL;
last->next = n;
last = n;
}
}
void display()
{
p = head;
while (p != NULL)
{
cout << p->data;
p = p->next;
}
}
};
int main()
{
alpha o;
o.input();
return 0;
}
the big mistake already pointed out is the absence of an initialization by a constructor. Also I suggest to move some data member to private, and make some of them local.
#include <iostream>
using namespace std;
struct node
{
int data;
node* next;
};
class alpha
{
private:
node* head;
node* last;
public:
alpha() {
head = NULL;
last = NULL;
}
void input()
{
int x;
char ch;
cout << "Enter the element..";
cin >> x;
insert(x);
cout << "Do you want to add more?";
cin >> ch;
if (ch == 'y')
{
input();
}
else
{
display();
}
}
void insert(int x1)
{
node* n = new node;
n->data = x1;
if (head == NULL)
{
head = n;
last = n;
}
else
{
n->next = NULL;
last->next = n;
last = n;
}
}
void display()
{
node* p = head;
while (p != NULL)
{
cout << p->data;
p = p->next;
}
}
};
int main()
{
alpha o;
o.input();
return 0;
}
As someone suggested, please implement a destructor ~alpha(), in order to avoid leaks of node instances.
I'm trying to implement a deque with some functions: front(),
back(), push_front(), push_back(), pop_front(), pop_back(). If I have one element in the queue and I try to pop it I get an "read access violation" in print function, however I check if a first element exists in deque.
#include<iostream>
#include<cstdlib>
using namespace std;
struct Nod {
int info;
Nod* next, *back;
};
void create_queue(Nod*& p, Nod*& u)
{
Nod *c = new Nod;
cout << "c->info: "; cin >> c->info;
if (!p)
{
p = c;
p->back = NULL;
p->next = NULL;
u = p;
}
else
{
u->next = c;
c->back = u;
u = c;
u->next = NULL;
}
}
void print_queue(Nod* p, Nod* u)
{
if (p) {
Nod *c = p;
while (c) {
cout << c->info << " ";
c = c->next;
}
}
else
cout << "Deque is empty";
}
int front(Nod *p) {
return p->info;
}
int back(Nod *u) {
return u->info;
}
void push_front(Nod*& p, Nod*& u) {
Nod *c = new Nod;
cout << "Push front c->info "; cin >> c->info;
c->next = p;
p->back = c;
c->back = NULL;
p = c;
}
void push_back(Nod*& p, Nod*& u) {
Nod *c = new Nod;
cout << "Push back c->info "; cin >> c->info;
c->back = u;
u->next = c;
u = c;
u->next = NULL;
}
void pop_front(Nod*& p, Nod*& u) {
if (p) {
Nod *c = p;
if (p->next != NULL) {
p->next->back = NULL;
p = p->next;
}
delete c;
}
else
{
cout << "Can't pop, deque is empty";
}
}
void pop_back(Nod*& p, Nod*& u) {
if (u){
Nod *c = u;
if (u->back != NULL) {
u->back->next = NULL;
u = u->back;
}
delete c;
}
else
{
cout << "Can't pop, deque is empty";
}
}
int main()
{
int n, i = 1;
Nod *p, *u = new Nod;
p = NULL;
u = NULL;
cout << "Nr nod: "; cin >> n;
while (i <= n){
create_queue(p, u);
i++;
}
pop_front(p, u); //problems if there is only one element in deque
print_queue(p, u);
system("Pause");
}
When there's only one node, you delete it but it's never set to nullptr, thus you still access the memory there causing a runtime error. Here I've written a modified pop_front and pop_back. In particular pop_back now sets the next pointer of the tail's previous element to nullptr. If there is only one element in the deque (head == tail) we do a pop_front, which will set head to nullptr by assigning it to its next element.
void pop_front(Nod*& head, Nod* tail) {
if (head) {
Nod* nodePtr = head;
head = head->next;
delete nodePtr;
}
}
void pop_back(Nod*& head, Nod*& tail) {
if (head == tail) {
return pop_front(head, tail);
}
Nod* prev = tail->back;
prev->next = NULL;
delete tail;
tail = prev;
}
Hi. I have a string of digits like in the picture above. I want to print them in decreasing order using a minimum number of queues. The input also acts like a queue.
My idea is to take first digit from the right and create a queue. Then for each other digit check if it is bigger than the first digit in any queue. If it is, add it to that queue. If it is not, create a new queue and repeat the procces.
I have written the following code:
#include<iostream>
using namespace std;
struct Nod{
int info;
Nod *urm;
};
void push(Nod *&p, Nod *&u, int info){
if (p == NULL)
{
p = new Nod;
p->info = info;
u = p;
u->urm = NULL;
}
else
{
Nod *q = new Nod;
q->info = info;
q->urm = p;
p = q;
}
}
void pop(Nod *&p, Nod *&u)
{
if (p != NULL){
Nod *q = p;
if (q->urm != NULL){
while (q->urm->urm != NULL)
{
q = q->urm;
}
delete u;
u = q;
u->urm = NULL;
}
else
p = NULL;
}
else
cout << "Empty Queue";
}
void print(Nod *p)
{
cout << endl;
Nod *q = p;
while (q != NULL)
{
cout << q->info << " ";
q = q->urm;
}
cout << endl;
}
int main()
{
Nod *p, *u;
p = u = NULL;
Nod *p1[100];
Nod *u1[100];
char * s;
s = new char[100];
cin >> s;
push(p1[0], u1[0], s[(unsigned)strlen(s) - 1]-'0');
for (int i = (unsigned)strlen(s) - 2; i >= 0; i--){
int k = 0; int ok = 0;
while (p1[k] != NULL)
{
if (s[i]-'0' >= p1[k]->info)
{
push(p1[k], u1[k], s[i]-'0');
ok = 1;
break;
}
k++;
}
if (ok == 0)
push(p1[k], u1[k], s[i]-'0');
}
//test printing first 3 lines.
print(p1[0]);
print(p1[1]);
print(p1[2]);
system("Pause");
}
I have a crash at this line which I believe it's caused by a pointer error? I'm using Visual Studio 2013. I've tried using the debugger but still I can't figure out what's the problem. I also wanted to ask if you can point me some basic resources on how to use the debugger. Thank you.
if (s[i]-'0' >= p1[k]->info)
My problem is that I'm trying to insert a letter into a linked list based on users input at a specific position given by the int h...However every time I run the program only the second character in the list changes regardless of the number the user puts into the program.
Example:
./h
Name: koala
a<-l<-a<-o<-k
Change the position: 2
To the character: 3
a<-l<-3<-o<-k
Insert the Character: F
To the Postion: 3
a<-F<-l<-3<-o<-k
I want it to look like.
./h
Name: koala
a<-l<-a<-o<-k
Change the position: 2
To the character: 3
a<-l<-3<-o<-k
Insert the Character: F
To the Postion: 3
a<-l<-3<-F<-o<-k
I know my problem is in the insert_char() function in my lists.cpp but just can't figure out what i'm doing wrong...
List.h
#include <iostream>
using namespace std;
struct Node;
Node* new_list();
void insert_front(Node** plist,char x);
void insert_char(Node* plist, char x, int p);
void change_char(Node* plist, char x, int p);
void print_list(Node* list);
void delete_front(Node** plist);
void delete_list(Node** plist);
//void delete_char(Node* plist,int p);
struct Node {
char x;
Node *next;
};
main.cpp
struct Node {
char x;
Node *next;
};
int main(){
Node *list;
list = new_list(); //new empty list
cout<<"Name: ";
string name;
cin>> name;
for (int i =0; i < name.length(); i++)
insert_front(&list, name[i]);
//---------print list-------------------------
print_list(list);
cout <<"Change the position: ";
int z;
cin>> z;
cout<< "To the character: " ;
char x;
cin>> x;
change_char(list, x, z);
print_list(list);
cout <<"Insert the Character: ";
char y;
cin>> y;
cout<< "To the Postion: ";
int h;
cin>> h;
insert_char(list, y, h);
print_list(list);
return 0;
}
lists.cpp
Node* new_list()
{
Node* list = 0; //in C++ it is better to use 0 than NULL
return list;
}
void insert_front(Node** plist,char x){
Node* t;
t = new Node;
t->x = x;
t->next = *plist;
*plist = t;
return;
}
void change_char(Node* plist, char x, int p)
{
Node* s = plist;
for (int i=1; i<p && 0!=s;i++)
s = s->next;
if (0 != s)
s->x = x;
return;
}
void insert_char(Node* plist, char x, int p){
Node* s = plist;
Node* a = new Node();
for (int i=1; i<p && s; i++)
a->next=s->next;
s->next=a;
if (0 !=s)
a->x=x;
return;
}
//void delete_char(Node* plist,int p)
void print_list(Node* list){
Node* p;
p = list;
if(p == 0)
cout << "--- empty list ---" << endl;
while(p !=0){
cout << p->x<<"<-";
p = p->next;
}
cout << endl;
}
void delete_front(Node** plist){
Node* t;
if( *plist != 0){ // list is not empty
t = (*plist);
*plist = (*plist)->next;
delete t;
}
}
void delete_list(Node** plist){
while(*plist != 0) //while list not empty
delete_front(plist);
}
bool is_empty(Node* list){
return (list == 0);
}
The for loop in insert_char is just "inserting" the character over and over. I think you meant to advance s to the starting point of the insertion (as determined by h).
Update:
This part is wrong for a few reasons:
for (int i=1; i<p && s; i++)
a->next=s->next;
s->next=a;
Note that the indentation is misleading. Since you don't have braces, only the middle line is part of the loop. Effectively, you've written:
for (int i=1; i<p && s; i++) {
a->next=s->next;
}
s->next=a;
Personally, I always use braces on blocks, even if they consist of only one statement.
So you set a->next a bunch of times instead of advancing to the point in the list you want to go.
You need to advance to the position you want the new element in the loop, and then do the actual insertion.
// Advance s to index p.
for (int i = 1; i < p && s->next; i++) {
s = s->next;
}
// Insert a at s.
a->next = s->next;
s->next = a;