value=data;u->next=NULL;if(NULL==head){//头结点也存储数据head=u;cur=u;}else{cur->next=u;cur=u;//当前指针改为u}length++;}intLi" />
文档库 最新最全的文档下载
当前位置:文档库 › 链表.cpp

链表.cpp

#include
#include "Link.h"

void Link::add(int data){
struct myLink *u = new struct myLink;
u->value = data;
u->next = NULL;
if (NULL == head){//头结点也存储数据
head = u;
cur = u;
}else {
cur->next = u;
cur = u; //当前指针改为u
}
length++;
}
int Link::getData(int pos){
if (length >= pos && length > 0){//链表非空并且无越界
int count = 0;//计数
while (pos != count){
count++;
if (1 == count)
cur = head;//头指针成为当前指针
else
cur = cur->next;//指针后移
}
return cur->value;
}
return -1; //其它返回-1
}
int Link::remove(int pos){
if (length >= pos && length > 0){//链表非空并且无越界
int count = 1;//计数
while (pos != count){//得到第pos位置的前一个指针
count++;
if (2 == count)
cur = head;//头指针成为当前指针
else
cur = cur->next;//指针后移
}
struct myLink *temp;
if (1 == count){
//如果remove第一个数据,就应该特殊处理下,因为头结点也存储了data
temp = head;
head = temp->next;
delete temp;
}else {
temp = cur->next;
cur->next = temp->next;
delete temp;
}
length--; //不要忘了将length--
return 0;
}
return -1;//其它返回-1
}
int Link::insert(int pos, int data){
if (length >= pos && length > 0){//链表非空并且无越界
int count = 1;//计数
while (pos != count){//找到当前位置的前一个指针
count++;
if (2 == count)
cur = head;//头指针成为当前指针
else
cur = cur->next;//指针后移
}
struct myLink *u = new struct myLink;
u->value = data;
if (1 == count){
u->next = head;
head = u;//当前结点成为头结点
}else {
u->next = cur->next;//新加结点的指向当前结点的下一个结点
cur->next = u; //当前结点指向新加结点
}
length++; //不要忘了
return 0;
}
return -1;//其它返回-1
}
void Link::sort(){
if (length > 1){
int count = 0;
while (length - 1 != count){
int i = count + 1;
struct myLink *temp = head;
cur = head; //当前结点指向头结点
while (length != i ){
if (cur->value > cur->next->value){
cur->value += cur->next->value;
cur->next->value = cur->value - cur->next->value;
cur->value -= cur->next->value;
}
cur = cur->next;
i++;
}
temp = temp->next;
count++;
}
}
}
void Link::reverse(){//通过改变指针域来转置,效率更高点
if (length > 0){
int count = 0;
int *p = new int[length];//不要写成new int(length),最后俺找了2个多小时才找出来这里出错了
//new int(length)没有申请length个int的内存,
cur = head; //当前结点指向头结点
while (length != count){
p[count] = (int)cur;//将指针存储在p[i]中
count++;
cur = cur->next;
}
count = 0;
struct myLink *temp = head; //辅助指针
while (l

ength != count){
count++;
cur = (struct myLink *)p[length - count];//反转
if (1 == count){
head = cur;//最后一个结点成为头结点
temp = head;
}
else {
temp->next = cur; //当前结点成为上一个结点的后继结点
temp = temp->next; //temp后移
}
}
cur->next = NULL;
delete []p;
}
}
int Link::modify(int pos, int data){
if (length >= pos && length > 0){//链表非空并且无越界
int count = 0;//计数
while (pos != count){
count++;
if (1 == count)
cur = head;//头指针成为当前指针
else
cur = cur->next;//指针后移
}
cur->value = data;
return 0;
}
return -1;//其它返回-1
}
void Link::display(){
if (length > 0){
std::cout<<"data in the Link is:"<int count = 0;
cur = head; //当前结点指向头结点
while (length != count){
count++;
std::cout<<"the "<value<cur = cur->next;
}
}
}
Link::~Link(){
if (NULL != head){
cur = head; //从头结点开始delete
while (0 != length){
head = cur->next; //头结点后移
delete cur;
cur = head;
length--;
}
}
}

相关文档
相关文档 最新文档