单向链表长整数加法 java_单链表实现大整数加法

#include "ListNode.h"

template

class SingleList{

public:

SingleList(){head = new ListNode;}

~SingleList(){

makeEmpty();

delete head;

}

public:

void makeEmpty();

int length();

ListNode* find(Type item , int n)const;

ListNode* find(int n)const;

bool insert(Type item, int n =0);

Type remove(int n);

bool removeAll(Type item);

Type get(int n);

void ReverseSinglyLinkedList();

void print();

private:

ListNode *head;

};

template

void SingleList::makeEmpty(){

ListNode * pdel = NULL;

while(head->m_next!=NULL){

pdel = head->m_next;

head->m_next = pdel->m_next;

delete pdel;

}

}

template

int SingleList::length(){

int count = 0 ;

ListNode *p = head->m_next;

while (p!=NULL){

p = p->m_next;

count++;

}

return count;

}

template

ListNode*SingleList::find(int n)const{

if(n<0){cout << "out of the boundary"<< endl

;return NULL;}

ListNode* p = head;

for(int i =0 ; i<=n&&p ;i++){

p = p->m_next;

}

if(p==NULL){

cout << "can't find the element"<

return NULL;

}

return p;

}

template

ListNode*  SingleList::find(Type item , int

n)const{

if(n<1){

cout << "the n is illegal" << endl;

return NULL;

}

ListNode *p = head;

int count = 0;

while(count!=n && p){

p=p->m_next;

if(p->m_data==item)

count++;

}

if(p==NULL){

cout << "can't find the element"<

return NULL;

}

return p;

}

template

bool SingleList::insert(Type item, int n=0){

ListNode *p = head;

if(n<0){

cout << "the n is illegal"<

return false;

}

ListNode* pnode = new ListNode(item);

ListNode* pmove = head;

if(pnode==NULL){

cout << "Application is error "<< endl;

return false;

}

for(int i = 0 ; i

pmove = pmove->m_next;

}

if(pmove==NULL){

cout << "the n is illegal " << endl;

return false;

}

pnode->m_next = pmove->m_next;

pmove->m_next = pnode;

return true;

}

template

Type SingleList::remove(int n){

if(n<0){

cout << "can't find th element"<

exit(0);

}

ListNode* pmove=head ,*pdel;

for(int i = 0 ; i

pmove = pmove->m_next;

}

if(pmove->m_next==NULL){

cout << "can't find the element" <

exit(0);

}

pdel = pmove->m_next;

pmove->m_next = pdel->m_next;

Type item = pdel->m_data;

delete pdel;

return item;

}

template

bool SingleList::removeAll(Type item){

ListNode *pmove = head,*pdel = pmove->m_next;

while(pdel!=NULL){

if(pdel->m_data==item){

pmove->m_next = pdel->m_next;

delete pdel;

pdel = pmove->m_next;

continue;

}

pmove=pmove->m_next;

pdel = pdel->m_next;

pmove = pmove->m_next;

}

return true;

}

template

Type SingleList::get(int n){

if(n<0){

cout << "can't find the element "<

exit(0);

}

ListNode *pmove =head->m_next;

for(int i =0 ; i

pmove = pmove->m_next;

if(NULL == pmove){

cout << "can't find the element "<

exit(0);

}

}

return  pmove->m_data;

}

template

void SingleList::print(){

ListNode *pmove = head->m_next;

cout << "head";

while (pmove!=NULL){

cout << "->" << pmove->m_data;

pmove = pmove->m_next;

}

cout << endl;

}

template

void SingleList::ReverseSinglyLinkedList(){

if(head->m_next==NULL)return;

ListNode *pmove = head->m_next;

ListNode *temp ;

while(pmove->m_next!=NULL){

temp = pmove->m_next;

pmove->m_next = temp->m_next;

temp->m_next = head->m_next;

head->m_next = temp;

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值