I was writing a code to perform operations on simple linked list. Everything is working fine but for some reason two if blocks( both similar ) fails to execute although their condition is true. The following is my code help me debug it. I am currently a newcomer in coding c++ is my first language.
Thank You
#include <iostream>
using namespace std;
//Operations on Linked List....
struct node //Data Structure
{
int data;
node *next;
} *start; //Initial Node
int main()
{
start = new node; //Setup
start->data=0;
start->next=NULL;
cout<<"Welcome to Linked List Review program..."; //Initial Text
node *ptr=start; //Geting List
char ch='Y'; int x;
while(ch=='Y'||ch=='y')
{
cout<<"\nEnter the data: "; cin>>x;
ptr->next = new node;
ptr = ptr->next;
ptr->data = x;
ptr->next = NULL;
cout<<"\nDo you want to enter more? "; cin>>ch;
}
ptr=start->next; while(ptr->next!=NULL) { cout<<ptr->data<<" "; ptr = ptr->next; } cout<<ptr->data<<endl; //Print Linked List
ptr=start->next; //Reset Ptr for further operation
int i=0; while(ptr!=NULL) { ptr = ptr->next; i++; } cout<<endl<<"\nNo. of elements are: "<<i<<endl; //Counting and printing the count of no. of elements
ptr=start->next; //Reset ptr for further operation
cout<<"\n1)Add Data\n2)Delete Data\nCHoose the action you want to perform: "; cin>>x; //User Interface menu
switch (x)
{
case 1: // Create
{
int j; cout<<"Enter the position you want to perform operation on: "; cin>>j;
if((j<0) && (j>i)) //Checking if user is a tester
{ cout<<"\nYou love annoy all developers you f*ing tester...";
break;
}
else
{ // Creating and connecting new node
node *p = new node; cout<<"\nEnter the data: "; cin>>p->data;
for(int k=0; k<j-1;k++) ptr=ptr->next;
node *temp = ptr->next; ptr->next = p; p->next = temp;
break;
}
}
case 2:
{ // Delete
int j; cout<<"Enter the position you want to perform operation on: "; cin>>j;
if((j<0) && (j>i)) //Checking if user is a tester
{ cout<<"\nYou love annoy all developers you f*ing tester...";
break;
else
{ // Deleting following node
for(int k=0; k<j-1;k++) ptr=ptr->next;
node *temp = ptr->next; ptr->next = (ptr->next)->next; delete temp;
break;
}
}
}
ptr=start->next; while(ptr->next!=NULL) { cout<<ptr->data<<" "; ptr = ptr->next; } cout<<ptr->data<<endl; // Printing Linked List
cout<<"\nThank You...\nDeleting Linked List..."; // Deleting Linked List
node *temp=NULL;
i=0; ptr=start; while(ptr->next!=NULL) { temp = ptr->next; delete ptr; ptr=temp; cout<<"\nDeleted "<<i++<<"th node"; }
delete start; return 0;
} // The End
Aucun commentaire:
Enregistrer un commentaire