//This is just my own template for linked list (Stack and queus are just limitations which i dont want)
//JUST A TEMPLATE CONTAINING MANY FUNCTIONS
//Programmer - Aditya Gupta (now posting as TECHY3)
#include<iostream>
struct Node{ //single data nodes
int dat;
Node* next;
}; //CAUTION-> DONT DECLARE 'TEMPORARY NODE' HERE, SINCE SOMEWHERE IN SOME LOOP IT MAY BE DELETED, AS WE DO IN POP
class List{
Node *top,*rear;
public:
void Push(int dat); //pushes to end (last/rear)
void Push(int dat, int loc); /*receives location also, for eg. if location passed as 5,
then first use noofelements() to check whether it has at least 4 elements,
then continues... to put given data at location 5, ie. initial 5th data becomes 6th*/
void tPush(int dat); //to push at top position *doesnt call push(int,int) since it will take more time
int isempty(); //gives 1 if empty(ie true), and gives 0 if not empty (ie false)
int search(int dat); //returns location (1st element of queue is '1'st element)
// 0 means not found
int noofelements();
void Pop(int dat); //first use search() to get location, then calls Pop(int,int) to delete it
void Pop(int dat,int loc);
void lPop(int loc); //in case we dont know the data but want to remove so and so location..
//actually disp() then Pop(int,int) use kar sakte hai, but some users are too lazy to do that!
void disp();
List(){ top=rear=NULL;
}
~List(){ //Q. WILL DESTRUCTOR REQUIRE DELETE STATEMENTS? (LIKE DELETE TOP; ETC. ?)
}
};
void List::Push(int dat){
Node *ptr;
ptr->next=rear;
ptr->data=dat;
rear->next=ptr;
}
void List::Pop(){
Node *temp=top; Node *temp2; //used temp2 since it will change only in the loop, 'before' temp changes, the loop stops at last element, so temp2 will still be the 2nd last element
while(temp->next!=rear) //loop stops when temp is representing last element, and temp2 representing the 2nd last
{
temp2=temp;
temp=temp->next;
}
rear=temp2;
temp2->next=rear;
temp->next=NULL;
delete temp; //last element deleted
}
void List::tPop(){
Node *temp;
temp=top;
top=temp->next;
temp->next=NULL;
delete temp;
}
int List::isempty(){ //can directly use noofelements() bcz in case of empty loop will not run, hence no time difference
if(top=rear) return 1;
else return 0;
}
int List::noofelements(){
Node *temp=top; int count=0;
while(temp!=rear){ count++; temp=temp->next;
}; ++count; //compensating for not counting last element
delete temp;
return count;
}
int List::search(int dat){
Node *temp=top; int ret=0,count=0;
while(temp!=rear){ count++; if(temp->data==dat) ret=count; temp=temp->next;};
if(temp->data==dat) ret=count+1; //compensating for last element, since the loop cant check for that
delete temp;
return ret;
}
void List::disp(){
Node *temp=top; cout<<endl;
while(temp!=rear){ cout<<temp->data<<" ";
}; cout<<temp->data; //compensating for last element, since the loop cant check for that
delete temp;
}
-------------x-----------------x--------------------x---------------------x--------------------------
-------------x-----------------x--------------------x---------------------x--------------------------
//THREE DATA NODES (for "C++ wala SQL")
struct Node{
int dat1,dat2,dat3;
Node *next;
}; //CAUTION-> DONT DECLARE 'TEMPORARY NODE' HERE, SINCE SOMEWHERE IN SOME LOOP IT MAY BE DELETED, AS WE DO IN POP
class List{ //almost same, just made it complatible to work with three data nodes
Node *top,*rear;
public:
void Push(int a1,int a2,int a3); //pushes to end (last/rear)
void Push(int a1,int a2, int a3, int loc); //receives row number (THIS IS NOT IN SQL! :p)
void tPush(int a1, int loc); //to push at top position *doesnt call push(int,int) since it will take more time
int isempty();
int search(int a1, int a2, int a3); //returns location (1st element of queue is '1'st element)
int search(int a1, int a2); //in case we remember any two
int search(int a1); //in case you remember only one
int rsearch(int a1, int a2=-1, int a3=-1); //random search, in case the user doesnt know which column to search
//This isnt of much use i think though(it will not search input with value -1)
int noofelements();
void Pop(); //first calls disp() and then takes row to be deleted as input, then calls Pop(int)
void Pop(int loc);
void disp();
List(){ top=rear=NULL;
}
~List(){ //Q. WILL DESTRUCTOR REQUIRE DELETE STATEMENTS? (LIKE DELETE TOP; ETC. ?)
}
};
-------------x-----------------x--------------------x---------------------x--------------------------
-------------x-----------------x--------------------x---------------------x--------------------------
//TABLE (3 coloums) DITTO SAME AS ABOVE JUST THE COMMENTS ARE DIFFERENT
class Table{ //almost same, just made it complatible to work with three data nodes
Node *top,*rear;
public:
void Push(int a1,int a2,int a3); //pushes to end (last/rear)
void Push(int a1,int a2, int a3, int loc); //receives row number (THIS IS NOT IN SQL! :p)
void tPush(int a1, int loc); //puts at 1st row
int isempty();
int search(int a1, int a2, int a3); //returns location (1st row of table is '1'st row)
int search(int a1, int a2); //in case we remember any two
int search(int a1); //in case you remember only one
int rsearch(int a1, int a2=-1, int a3=-1); //random search, in case the user doesnt know which column to search
//This isnt of much use i think though(it will not search input with value -1)
int noofrow();
void Pop(); //first calls disp() and then takes row to be deleted as input, then calls Pop(int)
void Pop(int loc);
void disp(char*, char*, char*); //receives column name as argument
Table(){ top=rear=NULL;
}
~Table(){ //Q. WILL DESTRUCTOR REQUIRE DELETE STATEMENTS? (LIKE DELETE TOP; ETC. ?)
}
}SQL;
//to copy paste in programs using this template : "//NOTE: I HAVE REMOVED ALL COMMENTS, REFER TO THE LINKED LIST TEMPLATE FOR DETAILED COMMENTS"
//JUST A TEMPLATE CONTAINING MANY FUNCTIONS
//Programmer - Aditya Gupta (now posting as TECHY3)
#include<iostream>
struct Node{ //single data nodes
int dat;
Node* next;
}; //CAUTION-> DONT DECLARE 'TEMPORARY NODE' HERE, SINCE SOMEWHERE IN SOME LOOP IT MAY BE DELETED, AS WE DO IN POP
class List{
Node *top,*rear;
public:
void Push(int dat); //pushes to end (last/rear)
void Push(int dat, int loc); /*receives location also, for eg. if location passed as 5,
then first use noofelements() to check whether it has at least 4 elements,
then continues... to put given data at location 5, ie. initial 5th data becomes 6th*/
void tPush(int dat); //to push at top position *doesnt call push(int,int) since it will take more time
int isempty(); //gives 1 if empty(ie true), and gives 0 if not empty (ie false)
int search(int dat); //returns location (1st element of queue is '1'st element)
// 0 means not found
int noofelements();
void Pop(int dat); //first use search() to get location, then calls Pop(int,int) to delete it
void Pop(int dat,int loc);
void lPop(int loc); //in case we dont know the data but want to remove so and so location..
//actually disp() then Pop(int,int) use kar sakte hai, but some users are too lazy to do that!
void disp();
List(){ top=rear=NULL;
}
~List(){ //Q. WILL DESTRUCTOR REQUIRE DELETE STATEMENTS? (LIKE DELETE TOP; ETC. ?)
}
};
void List::Push(int dat){
Node *ptr;
ptr->next=rear;
ptr->data=dat;
rear->next=ptr;
}
void List::Pop(){
Node *temp=top; Node *temp2; //used temp2 since it will change only in the loop, 'before' temp changes, the loop stops at last element, so temp2 will still be the 2nd last element
while(temp->next!=rear) //loop stops when temp is representing last element, and temp2 representing the 2nd last
{
temp2=temp;
temp=temp->next;
}
rear=temp2;
temp2->next=rear;
temp->next=NULL;
delete temp; //last element deleted
}
void List::tPop(){
Node *temp;
temp=top;
top=temp->next;
temp->next=NULL;
delete temp;
}
int List::isempty(){ //can directly use noofelements() bcz in case of empty loop will not run, hence no time difference
if(top=rear) return 1;
else return 0;
}
int List::noofelements(){
Node *temp=top; int count=0;
while(temp!=rear){ count++; temp=temp->next;
}; ++count; //compensating for not counting last element
delete temp;
return count;
}
int List::search(int dat){
Node *temp=top; int ret=0,count=0;
while(temp!=rear){ count++; if(temp->data==dat) ret=count; temp=temp->next;};
if(temp->data==dat) ret=count+1; //compensating for last element, since the loop cant check for that
delete temp;
return ret;
}
void List::disp(){
Node *temp=top; cout<<endl;
while(temp!=rear){ cout<<temp->data<<" ";
}; cout<<temp->data; //compensating for last element, since the loop cant check for that
delete temp;
}
-------------x-----------------x--------------------x---------------------x--------------------------
-------------x-----------------x--------------------x---------------------x--------------------------
//THREE DATA NODES (for "C++ wala SQL")
struct Node{
int dat1,dat2,dat3;
Node *next;
}; //CAUTION-> DONT DECLARE 'TEMPORARY NODE' HERE, SINCE SOMEWHERE IN SOME LOOP IT MAY BE DELETED, AS WE DO IN POP
class List{ //almost same, just made it complatible to work with three data nodes
Node *top,*rear;
public:
void Push(int a1,int a2,int a3); //pushes to end (last/rear)
void Push(int a1,int a2, int a3, int loc); //receives row number (THIS IS NOT IN SQL! :p)
void tPush(int a1, int loc); //to push at top position *doesnt call push(int,int) since it will take more time
int isempty();
int search(int a1, int a2, int a3); //returns location (1st element of queue is '1'st element)
int search(int a1, int a2); //in case we remember any two
int search(int a1); //in case you remember only one
int rsearch(int a1, int a2=-1, int a3=-1); //random search, in case the user doesnt know which column to search
//This isnt of much use i think though(it will not search input with value -1)
int noofelements();
void Pop(); //first calls disp() and then takes row to be deleted as input, then calls Pop(int)
void Pop(int loc);
void disp();
List(){ top=rear=NULL;
}
~List(){ //Q. WILL DESTRUCTOR REQUIRE DELETE STATEMENTS? (LIKE DELETE TOP; ETC. ?)
}
};
-------------x-----------------x--------------------x---------------------x--------------------------
-------------x-----------------x--------------------x---------------------x--------------------------
//TABLE (3 coloums) DITTO SAME AS ABOVE JUST THE COMMENTS ARE DIFFERENT
class Table{ //almost same, just made it complatible to work with three data nodes
Node *top,*rear;
public:
void Push(int a1,int a2,int a3); //pushes to end (last/rear)
void Push(int a1,int a2, int a3, int loc); //receives row number (THIS IS NOT IN SQL! :p)
void tPush(int a1, int loc); //puts at 1st row
int isempty();
int search(int a1, int a2, int a3); //returns location (1st row of table is '1'st row)
int search(int a1, int a2); //in case we remember any two
int search(int a1); //in case you remember only one
int rsearch(int a1, int a2=-1, int a3=-1); //random search, in case the user doesnt know which column to search
//This isnt of much use i think though(it will not search input with value -1)
int noofrow();
void Pop(); //first calls disp() and then takes row to be deleted as input, then calls Pop(int)
void Pop(int loc);
void disp(char*, char*, char*); //receives column name as argument
Table(){ top=rear=NULL;
}
~Table(){ //Q. WILL DESTRUCTOR REQUIRE DELETE STATEMENTS? (LIKE DELETE TOP; ETC. ?)
}
}SQL;
//to copy paste in programs using this template : "//NOTE: I HAVE REMOVED ALL COMMENTS, REFER TO THE LINKED LIST TEMPLATE FOR DETAILED COMMENTS"
Comments
Post a Comment
You are free to leave any kind of suggestion, or improvement. It doesn't require you to sign in either.