#include "LinkedList.h"
#include<string.h>
using namespace std;
LinkedList::LinkedList(int memory){
this->memory = memory;
first= new struct Stu;
first->next = first;
first->pre = first;
}
LinkedList::~LinkedList(){
}
int LinkedList::insert(LkItem item){
//Full Check! :: Error Message (-1).
if(isFull()) return -1;
//Distrubute Memory to Stu struct.
Stu *inStu = new struct Stu;
inStu->item = item;
//Search and Insert.
if(direction(item.name) == 1){
search(item.name,-1);
inStu->next = location;
inStu->pre = location->pre;
location->pre->next = inStu;
location->pre = inStu;
}
else{
search(item.name,1);
inStu->pre = location;
inStu ->next = location->next;
location->next->pre = inStu;
location->next = inStu;
}
--memory;
return 1;
}
int LinkedList::get(string name,LkItem& item){
//If Search-Funtion returns 0 value, it means already found.
if(search(name,0)==0){
item = location->item;
return 1;
}
return 0;
}
int LinkedList::remove(string name,LkItem& item){
int result = 0;
//If Search-Funtion returns 0 value, it means already found.
if(search(name,0)==0){
item = location->item;
Stu *temp = location;
location = location->next;
location->pre = temp->pre;
temp->pre->next = location;
delete temp;
result = 1;
}
return result;
}
int LinkedList::direction(string insert_Name){
if(insert_Name[0]<='L' || insert_Name[0]<='l' && insert_Name[0]>='A' || insert_Name[0]>='a')
return 1;
else
return -1;
}
int LinkedList::search(string insert_Name,int value){
goFirst();
string current_Name;
int insert_drection = -2;
// insert_drection value:
// 0 ----- insert_Name == current_Name ( can use in Fuction get() and remove() )
// 1 ----- insert_Name insert backward
// -1 ----- insert_Name insert forward
do{
if(direction(insert_Name)== 1)
location = location->next;
else
location = location->pre;
current_Name = location->item.name;
if(strcmp(insert_Name.c_str(),current_Name.c_str()) == 0)
insert_drection = 0;
else if(strcmpi(insert_Name.c_str(),current_Name.c_str()) > 0)
insert_drection = 1;
else if(strcmpi(insert_Name.c_str(),current_Name.c_str()) < 0)
insert_drection = -1;
if(insert_drection == value)
break;
}while(!(location == first));
return insert_drection;
}
int LinkedList::next(LkItem& item){
if(isEmpty()||location->next == first)
return 0;
location = location->next;
item = location->item;
return 1;
}
int LinkedList::back(LkItem &item){
if(isEmpty()||location->pre == first)
return 0;
location = location->pre;
item = location->item;
return 1;
}
void LinkedList::makeEmpty(){
goFirst();
while(!(location->next == first)){
next(location->item);
Stu *temp;
temp = location;
location = location->next;
location->pre = temp->pre;
temp->pre->next = location;
delete temp;
}
}
bool LinkedList::isFull(){
if(memory <= 0)
return true;
return false;
}
bool LinkedList::isEmpty(){
if(first->pre == first)
return true;
return false;
}
void LinkedList::goFirst(){
location = first;
}
C++ 排序双向循环链表代码
需积分: 17 112 浏览量
2008-12-10
00:42:33
上传
评论 1
收藏 207KB RAR 举报
miniobear
- 粉丝: 0
- 资源: 2