Created
October 27, 2012 18:16
-
-
Save Riateche/3965572 to your computer and use it in GitHub Desktop.
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> | |
using namespace std; | |
class ListItem { | |
public: | |
int d; | |
ListItem *next; | |
}; | |
class MyClass { | |
public: | |
ListItem *first, *last; | |
MyClass(); | |
int len; //длина списка | |
void Add(int d); | |
void RemFirst(); | |
void RemLast(); | |
int GetItem(int id); | |
int Show(); | |
}; | |
MyClass::MyClass() { | |
first=NULL; | |
last=NULL; | |
len=0; | |
} | |
void MyClass::Add(int d) { | |
ListItem *newlink = new ListItem; | |
newlink->next=NULL; | |
newlink->d=d; | |
if(first==NULL) { | |
last=newlink; | |
first=newlink; | |
} else { | |
last->next=newlink; | |
last=newlink; | |
} | |
len++; | |
} | |
void MyClass::RemFirst() { | |
if(first->next=NULL) { | |
first=last=NULL; | |
len=0; | |
} else { | |
first=first->next; | |
len--; | |
} | |
} | |
void MyClass::RemLast() { | |
if(first->next=NULL) { | |
first=last=NULL; | |
len=0; | |
} else { | |
last=NULL; | |
} | |
} | |
int MyClass::GetItem(int id) { | |
ListItem* Item=first; | |
if(id>len) { | |
cout<<"ERR"<<endl; | |
} | |
for(int k=0;k<id;k++) { | |
Item=Item->next; | |
} | |
return Item->d; | |
} | |
int MyClass::Show() { | |
ListItem *current=first; | |
while(current!=NULL) { | |
cout<<current->d<<endl; | |
current = current->next; | |
} | |
} | |
int main() { | |
MyClass item; | |
item.Add(15); | |
item.Add(24); | |
item.Add(7); | |
item.Show(); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment