#include<iostream>
#include<malloc.h>
#include<cstring>
#define INPUT_LEN 100
using namespace std;
//template<class T>
class student{
public:
int sid;
string name;
};
/*struct node start*/
class node {
public:
node(){}
node(void* dt, short len, node* nt):data(dt),dataLen(len),next(nt){}
void getData(void** dt,short* len){
*dt = data;
*len = dataLen;
}
void getNext(node* nt){
nt = next;
}
void* data;
short dataLen;
node* next;
};
/*struct node end*/
/******************class list start**********************************/
class list{
public:
list(){listHeader = NULL;}
int add(void* data, short len);
int add(void* data, short len,int index);
int deleteByIndex(int index);
node* getByIndex(int index);
int getSize();
bool isEmpty();
void setEmpty();
private:
node* listHeader;
};
/******************class list end***********************************/
/******************class list functions start*********************************************/
//getSize
int list::getSize(){
int size = 0;
node *p = 0;
p = listHeader;
while(p){
p = p->next;
size++;
}
return size;
}
//isEmpty
bool list::isEmpty(){
if(NULL == listHeader){
return true;
}
else{
return false;
}
}
//setEmpty
void list::setEmpty(){
for( int i = getSize() ; i > 0 ; --i){
deleteByIndex(i-1);
}
listHeader = NULL;
}
/*add node to tail*/
int list::add( void* data, short len ){
node *p = NULL;
int ret = 1;
node *nn = new node(data, len, NULL);
if(isEmpty()){
listHeader = nn;
return ret;
}
p = listHeader;
while( NULL != p->next ){
p = p->next;
}
p->next = nn;
p = NULL;
nn = NULL;
return ret;
}
//add node by index
int list::add(void* data, short len, int index){
node *p = listHeader;
node *tempNode;
int ret = 0;
int i = 0;
if(isEmpty() || index > getSize()) {
cout<<"index out of list size!"<<endl;
return -1;
}
node *nn = new node(data, len, NULL);
if(0 == index){
tempNode = listHeader;
listHeader = nn;
listHeader->next = tempNode;
}
else{
while(p && ++i < index){
p = p->next;
}
if(i == index){
tempNode = p->next;
p->next = nn;
p->next->next = tempNode;
ret = 1;
}
}
p = NULL;
nn = NULL;
tempNode = NULL;
return ret;
}
//get data by index
node* list::getByIndex(int index){
int i = 0;
node *p = listHeader;
node *ret = NULL;
if(isEmpty() || index >= getSize()) {
cout<<"index out of list size!"<<endl;
return ret;
}
while(NULL != p && i <= index){
if(i == index){
ret = p;
break;
}
p = p->next;
i++;
}
p = NULL;
return ret;
}
//delete by index
int list::deleteByIndex(int index){
int ret = -1;
int i = 0;
node *tempNode = NULL;
node *p = listHeader;
if(isEmpty() || index >= getSize()){
cout<<"index out of list size!"<<endl;
return -1;
}
if(1 == getSize()){
tempNode = listHeader;
listHeader = NULL;
}
else{
while(p->next && ++i < index){
p = p->next;
}
if(p->next){
tempNode = p->next;
p->next = p->next->next;
ret = 1;
}
}
if(NULL != tempNode){
free(tempNode->data);
free(tempNode);
tempNode = NULL;
}
p=NULL;
return ret;
}
/****************************class list functions end*********************************************/
//print list data
void printList(list lst){
char ch[INPUT_LEN]={0};
node *p = NULL;
for(int i = 0; i < lst.getSize(); i++){
p = lst.getByIndex(i);
memset(ch, 0, INPUT_LEN);
memcpy((char*)ch, (char*)p->data,p->dataLen);
cout<<ch<<endl;
}
}
//delete list
void deleteList(list* lst){
lst->setEmpty();
}
void run1(){
char* data = NULL;
short len = 0;
list list1;
char ch[INPUT_LEN] = {0};
int i = 0;
cout<<"Create list,input data:"<<endl;
while(i < 5)
{
memset(ch,0,INPUT_LEN);
scanf("%s", ch);
len = strlen(ch);
if(0 < len){
data = (char*)malloc(len);
memset(data,0,len);
memcpy(data, &ch, len);
if ( 4 == i ){
list1.add(data, len, i);
}
else{
list1.add(data, len);
}
}
else{
break;
}
i++;
}
printList(list1);
deleteList(&list1);
cout<<"after delete!"<<endl;
printList(list1);
}
void run2(){
list list1;
node* nd = NULL;
student* stu1;
student* stu2;
short len = sizeof(student);
for (int i = 0; i < 5; i++){
stu1 = new student();
stu1->sid = i;
stu1->name = "bandaotilan";
list1.add(stu1,len);
}
for (int i = 0; i < list1.getSize(); i++){
nd = list1.getByIndex(i);
stu2 = (student*)nd->data;
cout<<stu2->sid<<endl;
cout<<stu2->name<<endl;
}
}
//main function
int main(){
//run1();
run2();
}
#include<malloc.h>
#include<cstring>
#define INPUT_LEN 100
using namespace std;
//template<class T>
class student{
public:
int sid;
string name;
};
/*struct node start*/
class node {
public:
node(){}
node(void* dt, short len, node* nt):data(dt),dataLen(len),next(nt){}
void getData(void** dt,short* len){
*dt = data;
*len = dataLen;
}
void getNext(node* nt){
nt = next;
}
void* data;
short dataLen;
node* next;
};
/*struct node end*/
/******************class list start**********************************/
class list{
public:
list(){listHeader = NULL;}
int add(void* data, short len);
int add(void* data, short len,int index);
int deleteByIndex(int index);
node* getByIndex(int index);
int getSize();
bool isEmpty();
void setEmpty();
private:
node* listHeader;
};
/******************class list end***********************************/
/******************class list functions start*********************************************/
//getSize
int list::getSize(){
int size = 0;
node *p = 0;
p = listHeader;
while(p){
p = p->next;
size++;
}
return size;
}
//isEmpty
bool list::isEmpty(){
if(NULL == listHeader){
return true;
}
else{
return false;
}
}
//setEmpty
void list::setEmpty(){
for( int i = getSize() ; i > 0 ; --i){
deleteByIndex(i-1);
}
listHeader = NULL;
}
/*add node to tail*/
int list::add( void* data, short len ){
node *p = NULL;
int ret = 1;
node *nn = new node(data, len, NULL);
if(isEmpty()){
listHeader = nn;
return ret;
}
p = listHeader;
while( NULL != p->next ){
p = p->next;
}
p->next = nn;
p = NULL;
nn = NULL;
return ret;
}
//add node by index
int list::add(void* data, short len, int index){
node *p = listHeader;
node *tempNode;
int ret = 0;
int i = 0;
if(isEmpty() || index > getSize()) {
cout<<"index out of list size!"<<endl;
return -1;
}
node *nn = new node(data, len, NULL);
if(0 == index){
tempNode = listHeader;
listHeader = nn;
listHeader->next = tempNode;
}
else{
while(p && ++i < index){
p = p->next;
}
if(i == index){
tempNode = p->next;
p->next = nn;
p->next->next = tempNode;
ret = 1;
}
}
p = NULL;
nn = NULL;
tempNode = NULL;
return ret;
}
//get data by index
node* list::getByIndex(int index){
int i = 0;
node *p = listHeader;
node *ret = NULL;
if(isEmpty() || index >= getSize()) {
cout<<"index out of list size!"<<endl;
return ret;
}
while(NULL != p && i <= index){
if(i == index){
ret = p;
break;
}
p = p->next;
i++;
}
p = NULL;
return ret;
}
//delete by index
int list::deleteByIndex(int index){
int ret = -1;
int i = 0;
node *tempNode = NULL;
node *p = listHeader;
if(isEmpty() || index >= getSize()){
cout<<"index out of list size!"<<endl;
return -1;
}
if(1 == getSize()){
tempNode = listHeader;
listHeader = NULL;
}
else{
while(p->next && ++i < index){
p = p->next;
}
if(p->next){
tempNode = p->next;
p->next = p->next->next;
ret = 1;
}
}
if(NULL != tempNode){
free(tempNode->data);
free(tempNode);
tempNode = NULL;
}
p=NULL;
return ret;
}
/****************************class list functions end*********************************************/
//print list data
void printList(list lst){
char ch[INPUT_LEN]={0};
node *p = NULL;
for(int i = 0; i < lst.getSize(); i++){
p = lst.getByIndex(i);
memset(ch, 0, INPUT_LEN);
memcpy((char*)ch, (char*)p->data,p->dataLen);
cout<<ch<<endl;
}
}
//delete list
void deleteList(list* lst){
lst->setEmpty();
}
void run1(){
char* data = NULL;
short len = 0;
list list1;
char ch[INPUT_LEN] = {0};
int i = 0;
cout<<"Create list,input data:"<<endl;
while(i < 5)
{
memset(ch,0,INPUT_LEN);
scanf("%s", ch);
len = strlen(ch);
if(0 < len){
data = (char*)malloc(len);
memset(data,0,len);
memcpy(data, &ch, len);
if ( 4 == i ){
list1.add(data, len, i);
}
else{
list1.add(data, len);
}
}
else{
break;
}
i++;
}
printList(list1);
deleteList(&list1);
cout<<"after delete!"<<endl;
printList(list1);
}
void run2(){
list list1;
node* nd = NULL;
student* stu1;
student* stu2;
short len = sizeof(student);
for (int i = 0; i < 5; i++){
stu1 = new student();
stu1->sid = i;
stu1->name = "bandaotilan";
list1.add(stu1,len);
}
for (int i = 0; i < list1.getSize(); i++){
nd = list1.getByIndex(i);
stu2 = (student*)nd->data;
cout<<stu2->sid<<endl;
cout<<stu2->name<<endl;
}
}
//main function
int main(){
//run1();
run2();
}