Skip to main content

Linked List Template


//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"

Comments

Popular posts from this blog

Using 'ls' command as a replacement of 'find'

Advantage over find - Actually not probably in terms of speed, but... only point of this is that it's an observation, but... you can do like 'search only in' */*/*Programs* , /*/*Programs*/* , this may compensate the extra time took by using two commands 1. To search in current folder only (1 level... only this folder, no subfolder)     Type... ls | grep Search_Term 2. To search in current folder only (till level 2... this folder + subfolder)     Type... ls * | grep Search_Term #ls and ls * are NOT same! 3. To search in current folder only (till level 3... this folder + subfolder + subfolder)     Type... ls */* | grep Search_Term 4. To search in current folder only (till level 4... this folder + subfolder + subfolder + subfolder)     Type... ls */*/* | grep Search_Term ... and so on... FORMULA - To search till 'n' level (considering current folder as 1)... go till (n-1) stars If wanting to search whole disk... better type star...

Most Code will now be at my GitHub - github.com/adi-g15

Friends... i didn't update this blog for a month. But actually it was the period of time, i LEARNED and DID the most. The sole purpose of this blog for me, was just to put 'Interesting Observations' i used to get during finding or solving the errors, and some just clicked to try. Now, that i have actually been trying many languages and design, so most of the new code part will be on GitHub from now on... Thanks for reading... and I WILL keep posting more inferences here (I got dozens in this month due to the projects ;D ). And special thanks to DevHack1.0 team! GitHub link -> https://github.com/AdityaGupta150

LudO - The Game

/* Programmer - Aditya Gupta (Techy15) Language - C++ Program - LudO - The Game */ /* PLANS- Add functionality for name of player */ //IMP NOTE - See all notes, written in this form "//NOTE..." //NOTE- Mark 'R','G','B','Y' for gotis, ignore doubling for now //NOTE- Settings will have options: change default game play order(ie RBYG), change/give names to each colour //NOTE- Add code in case of attacks //NOTE- Add stops, and ways to show them distinct from others {may require graphics.h} #include<iostream> /* #include<fstream>    To be used in case save & resume to be used*/ #include<cstdio> #include<cstdlib> using namespace std; //Changes on 16th Oct //Show gotis as R1, R2... and ask user to enter string R1 or whatever // // Place within appropriate block ...     short diethrow(){     int fl=1; //holds 0 if decnum is 0     while(fl==0) { char *temp;     ...