Created
April 28, 2016 16:42
-
-
Save py4object/742059018858f9885e2256bb7183d944 to your computer and use it in GitHub Desktop.
liknedlist
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
template <class Type> | |
struct node | |
{ | |
Type info; | |
int indexX; | |
int indexY; | |
node<Type> *link; | |
}; | |
template <class Type> | |
class linkedList | |
{ | |
public: | |
node<Type> *head; | |
linkedList(Type array[][3],int weidth,int height); | |
void print(); | |
}; | |
template <class Type> | |
linkedList<Type>::linkedList(Type array[][3],int weidth,int height){ | |
head=NULL; | |
node <Type>* newnode; | |
node <Type>*current=head;; | |
for(int i=0;i<height;i++){ | |
for(int j=0;j<weidth;j++){ | |
if(current==NULL){ | |
newnode=new node<Type>; | |
newnode->link=NULL; | |
newnode->info=array[i][j]; | |
newnode->indexX=i; | |
newnode->indexY=j; | |
head=newnode; | |
current=head; | |
}else{ | |
while(current->link!=NULL){ | |
current=current->link; | |
} | |
newnode=new node<Type>; | |
newnode->link=NULL; | |
newnode->info=array[i][j]; | |
newnode->indexY=j; | |
newnode->indexX=i; | |
current->link=newnode; | |
} | |
} | |
} | |
} | |
template <class Type> | |
void linkedList<Type>::print(){ | |
node<Type>*current; | |
for(current=head;current;current=current->link){ | |
std::cout<<"info"<<current->info<<" "<<current->indexY<<" "<<current->indexX<<std::endl; | |
} | |
} | |
int main(){ | |
int d[3][3]={ | |
{2,1,3}, | |
{5,2,7}, | |
{6,3,8} | |
}; | |
linkedList<int>list= linkedList<int>(d,3,3); | |
list.print(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment