I have tried to implement queue using linked list. This is the code i wrote. When i try to use the disp() method i get a infinite loop running. I am not able to find the error in the logic. I fail to understand how the line while(temp!=NULL) and incrementing the temp never ends.
#include<iostream>
using namespace std;
struct node{
int x;
node *next;
};
class queue{
node *front,*rear;
public:
queue(){
front = NULL;
rear= NULL;
}
void enqueue(int x){
node *temp=new node;
temp->x=x;
temp->next=NULL;
if(rear==NULL){
rear=temp;
front = temp;
}
else{
rear->next=temp;
rear=temp;
}
}
int dequeue(){
node *temp =front;
if(temp!=NULL){
int x =temp->x;
temp=temp->next;
delete temp;
return x;
}
else{
return -1e7;
}
}
void disp(){
node *temp=front;
while(temp!=NULL){
cout<<temp->x<<" ";
temp=temp->next;
}
}
};
int main(){
int n;
cin>>n;
queue obj;
for(int i=0;i<n;i++){
int x;
cin>>x;
obj.enqueue(x);
}
int x,y;
cin>>x>>y;
obj.enqueue(x);
obj.enqueue(y);
obj.dequeue();
obj.disp();
}
Your dequeue is not properly dequeueing the first element:
node *temp =front;
if(temp!=NULL){
int x =temp->x;
temp=temp->next;
delete temp;
return x;
}
temp = temp->next makes temp point to the second node and in the next line you delete that second node, instead of the first one.
Make the second node the new front and delete the old front:
node *temp = front;
if(temp != NULL){
int x = temp->x;
front = temp->next; // <---
delete temp;
return x;
}
Related
I am able to create a new node of some values specified but while trying to insert a new node at a specific location in the created, my link gets broken somehow, where did I go wrong.
just in case I've genuinely removed the test case for the linked list being empty.
#include<iostream>
struct node //declaring node struct with data and pointer to the node itself
{
int data;
node* next;
};
node* headnode; //adding another variable headnode which points to null
void insert(int date ){
node* newnode= new node;
newnode->data=date;
newnode->next=headnode;
headnode= newnode;
}
void insertatlocation(int location){
std::cout<<"enter data\n";
int data;
std::cin>>data;
node* newnode = new node;
newnode->data=data;
node* prepointer = headnode;
for (int i=0;i<=location;i++){
prepointer=headnode;
headnode= headnode->next;
}
prepointer->next=newnode;
newnode->next=headnode;
}
void printnode(){
node* pointer= headnode;
while (pointer!=NULL)
{
std::cout<<pointer->data<<"->";
pointer= pointer->next;
}
std::cout<<"\n";
}
int main(){
node* headnode=NULL;
int x,loc;
int data;
std::cin>>x;
for(int i=0;i<x;i++){
std::cin>>data;
insert(data);
printnode();
}
std::cout<<"enter the location to add inbetween \n";
std::cin>>loc;
insertatlocation(loc);
for(int i=0;i<10;i++){
printnode();
}
}
can u use head->data as an integer variable, if the "data" is integer type in chained lists.
for example, I have a code where I have to transform a simply chained list of integer variables, into 2 lists, one with positive numbers, the other one with negative numbers. I cant find the error in my code...
using namespace std;
struct Node{
int data;
Node* next;
};
Node* head=new Node;
Node* pozitiv=NULL;
Node* negativ=NULL;
Node* newNode(int data)
{
Node* nodNou = new Node();
nodNou -> data = data;
nodNou -> next = NULL;
return nodNou;
}
void Push(Node* &top, int data)
{
Node* nodNou = newNode(data);
nodNou -> next = top;
top = nodNou;
}
void read(){
Node* aux=new Node;
int r,i,v[1000];
cout<<"insert the number of elements in list"<<endl;
cin>>r;
cin>>v[0];
head->data=v[0];
head->next=NULL;
for(i=1;i<r;i++){
cin>>v[i];
aux->data=v[i];
aux->next=head;
head=aux;
aux=new Node;
}
}
write(){
if(head==NULL) return 0;
else{
while(head!=NULL){
cout<<head->data<<"->";
head=head->next;
}
}
cout<<endl<<endl;
}
void stivezlutat(){
Node* aux;
pozitiv=new Node;
pozitiv->next=NULL;
negativ=new Node;
negativ->next=NULL;
while(head!=NULL){
if(head->data >= 0){
Push(pozitiv, head->data);
}
else{
Push(negativ, head->data);
}
head=head->next;
}
cout<<"pozitive list:"<<endl;
while(pozitiv != NULL){
cout<<pozitiv->data<<"->";
pozitiv=pozitiv->next;
}
cout<<endl;
cout<<"negative list:"<<endl;
while(negativ != NULL){
cout<<negativ->data<<"->";
negativ=negativ->next;
}
}
int main()
{
read();
write();
stivezlutat();
}```
Errors in your code:
Required #include directives don't present.
extra "```" exists at the end of code.
No return type is specified for write.
In the function write, return with value is used while no return statement presents at the end of the function, so no return type is valid for that.
The function write breaks the value of head, so the function stivezlutat will see empty list.
Extra nodes are assigned to pozitiv and negativ and they are printed in the funciton stivezlutat.
Fixed code (also indentation is fixed):
#include <iostream>
using namespace std;
struct Node{
int data;
Node* next;
};
Node* head=new Node;
Node* pozitiv=NULL;
Node* negativ=NULL;
Node* newNode(int data)
{
Node* nodNou = new Node();
nodNou -> data = data;
nodNou -> next = NULL;
return nodNou;
}
void Push(Node* &top, int data)
{
Node* nodNou = newNode(data);
nodNou -> next = top;
top = nodNou;
}
void read(){
Node* aux=new Node;
int r,i,v[1000];
cout<<"insert the number of elements in list"<<endl;
cin>>r;
cin>>v[0];
head->data=v[0];
head->next=NULL;
for(i=1;i<r;i++){
cin>>v[i];
aux->data=v[i];
aux->next=head;
head=aux;
aux=new Node;
}
}
void write(){
if(head==NULL) return;
else{
Node* cursor = head;
while(cursor!=NULL){
cout<<cursor->data<<"->";
cursor=cursor->next;
}
}
cout<<endl<<endl;
}
void stivezlutat(){
Node* aux;
pozitiv=NULL;
negativ=NULL;
while(head!=NULL){
if(head->data >= 0){
Push(pozitiv, head->data);
}
else{
Push(negativ, head->data);
}
head=head->next;
}
cout<<"pozitive list:"<<endl;
while(pozitiv != NULL){
cout<<pozitiv->data<<"->";
pozitiv=pozitiv->next;
}
cout<<endl;
cout<<"negative list:"<<endl;
while(negativ != NULL){
cout<<negativ->data<<"->";
negativ=negativ->next;
}
}
int main()
{
read();
write();
stivezlutat();
}
I am new to the data structures and I was trying to write a code which add nodes to the beginning of the linked list. After every time user enters a new node, the program is supposed to display an updates linked list, but my program is only displaying the current entered node.
The code is as follows: -
#include<iostream.h>
#include<conio.h>
struct Node
{
int data;
Node* next;
};
struct Node* head;
void Insert(int x)
{
Node* temp=new Node();
temp->data=x;
temp->next=NULL;
head=temp;
}
void Print()
{
struct Node* temp=head;
cout<<"List is: ";
while(temp!=NULL)
{
cout<<temp->data;
temp=temp->next;
}
cout<<"\n";
}
void main()
{
head=NULL;
clrscr();
cout<<"How many numbers?\n";
int n,x,i;
cin>>n;
for(i=0; i<n; i++)
{
cout<<"Enter the number \n";
cin>>x;
Insert(x);
Print();
}
getch();
}
Your Insert method is wrong. You need to assign head to next:
void Insert(int x)
{
Node* temp=new Node();
temp->data=x;
temp->next=head;
head=temp;
}
This will then chain your list correctly.
Your Insert function is wrong. You need to use something like this in order to add new items at the end of the list:
void InsertAtTheEnd(int x) {
Node* temp = new Node();
temp->data=x;
temp->next=NULL;
if (NULL == head) {
head = temp;
} else {
Node *tmp = head;
while (tmp->next != NULL) {
tmp = tmp->next;
}
tmp->next = temp;
}
}
And this will add them to the beggining:
void InsertAtTheBeginning(int x) {
Node* temp=new Node();
temp->data=x;
temp->next=head;
head=temp;
}
Check it out live
I want to know what is the problem in creat_list2 function..
I have a problem in this code and as i knew it's in the creat_list2 cause the program run Successfully
when i stop this function it ask the user to enter a numbers that will be stored in the list 1 then print them but the problem as i saw is in the second fuction that creat the second list...,I have to submit my assignment tomorrow so I wish if any body can help me to solve this problem.
#include<iostream>
using namespace std;
struct node{
int x;
node *next;
};
struct snode{
int y;
snode *next;
};
creat_list1(node *&head, node *&tail)
{
int num;
cout<<"enter number\n";
cin>>num;
while(num!=0)
{
node *np=new node;
np->x=num;
if(head==nullptr)
head=np;
else
tail->next=np;
tail=np;
tail->next=nullptr;
cout<<"enter number again\n";
cin>>num;
}
}
creat_list2(node *&head, snode *shead, snode *stail)
{
int sum=0;
while(head!=nullptr)
{
for(int i=0;i<head->x;i++)
sum+=i;
snode *np= new snode;
np->y=sum;
if(head==0)
shead=np;
else
stail->next=np;
stail=np;
stail->next=nullptr;
}
head=head->next;
}
void print_list1 (node *head)
{
while(head!=nullptr)
{
cout<<head->x<<"\t";
head=head->next;
}
}
void print_list2(snode *shead)
{
while(shead!=nullptr)
{
cout<<shead->y<<"\t";
shead=shead->next;
}
}
main()
{
node *head=nullptr, *tail=nullptr;
snode *shead=nullptr, *stail=nullptr;
creat_list1(head,tail);
creat_list2(head,shead,stail);`enter code here`
print_list1(head);
print_list2(shead);
}
There are couple of issues with yours code,
creat_list2(node *&head, snode *shead, snode *stail) if u updated
head like head=head->next; it will reflect at the main.
shead and stail are just a local pointer and any update to just a pointer like stail=np;will not have any changes at the `main
There seems to be typo if(head==0) shead=np; instead it should be
if(shead==0) shead=np; which is causing null pointer exception.
I tried to fix the error at creat_list2 but functionality is still ambiguous to me,
void creat_list2(const node *head, node *&shead, node *&stail) //updated in argument
{
int sum = 0;
while (head != nullptr)
{
//for (int i = 0; i < head->x; i++)
// sum += i;
node *np = new node;
np->x = sum;
if (shead == 0) //error : head instead shead
shead = np;
else
stail->next = np;
stail = np;
stail->next = nullptr;
head = head->next;
}
}
In the below code, the number stored inside the last element is only being displayed, not the rest of elements.
#include <stdio.h>
#include <iostream>
using namespace std;
struct node
{
int data;
node* next;
};
struct node * Start = NULL;
struct node * End = NULL;
void CreateList(int num)
{
struct node *temp = new struct node;
int data,i=0;
while(true)
{
cout<<"Enter data \n";
cin>>data;
temp->data = data;
temp->next = Start;
if(Start==NULL)
{
Start = End = temp;
}
else{
End->next = temp;
End = temp;
}
if(i==num-1)
break;
i++;
}
}
void Display()
{
struct node* temp1;
temp1 = Start;
do{
cout<<temp1->data<<endl;
temp1=temp1->next;
} while(temp1!=Start);
}
int main()
{
int num;
cout<<"How many elements you want to input?\n";
cin>>num;
CreateList(num);
Display();
return 0;
}
I have done it using structure not classes. I have used two non-dynamic pointers 'Start' and 'End'. The main problem is coming in Display() function. The Display() is printing the value of the last element.
Output:
(Click image to enlarge)
You are not creating the required number of nodes in CreateList. You are creating a node using
struct node *temp = new struct node;
and are reusing the same node in the loop.
Here's an updated version of the function.
void CreateList(int num)
{
int data,i=0;
while(true)
{
cout<<"Enter data \n";
cin>>data;
// Create a new node for every data
struct node *temp = new struct node;
temp->data = data;
temp->next = Start;
if(Start==NULL)
{
Start = End = temp;
}
else{
End->next = temp;
End = temp;
}
if(i==num-1)
break;
i++;
}
}
Suggestion for futher cleanup. In C++, you don't need to use struct node. Just node is sufficient.
node* Start = NULL;
node* End = NULL;
and
// Create a new node for every data
node *temp = new node;