1.如何通过调整链而不是数据来交换两个相邻的元素?
// 单向链表
Node *p,*afterp;
p=beforep->next;
afterp=p->next;
p->next=afterp->next;
beforep->next=afterp;
afterp->next=p;
// 双向链表
Node *beforep,*afterp;
beforep=p->prev;
afterp=p->next;
p->next=afterp->next;
beforep->next=afterp;
afterp->next=p;
p->next->prev=p;
p->prev=afterp;
afterp->prev=beforep;
2.如何求出两个已排序的表L1和L2的交集和并集。
// 交集
template <typename Object>
list<Object> intersection( const list<Object> & L1,const list<Object> & L2)
{
list<Object> intersect;
typename list<Object>:: const_iterator iterL1 = L1.begin();
typename list<Object>:: const_iterator iterL2 = L2.begin();
while(iterL1 != L1.end() && iterL2 != L2.end())
{
if (*iterL1 == *iterL2)
{
intersect.push_back(*iterL1);
iterL1++;
iterL2++;
}
else if (*iterL1 < *iterL2)
iterL1++;
else
iterL2++;
}
return intersect;
}
// 并集
// Assumes both input lists are sorted
template <typename Object>
list<Object> listUnion( const list<Object> & L1,const list<Object> & L2)
{
list<Object> result;
typename list<Object>:: const_iterator iterL1 = L1.begin();
typename list<Object>:: const_iterator iterL2= L2.begin();
while(iterL1 != L1.end() && iterL2 != L2.end())
{
if (*iterL1 == *iterL2)
{
result.push_back(*iterL1);
iterL1++;
iterL2++;
}
else if (*iterL1 < *iterL2)
{
result.push_back(*iterL1);
iterL1++;
}
else
{
result.push_back(*iterL2);
iterL2++;
}
}
return result;
}
3.一个有表头结点,没有尾结点,还有一个指向表头结点的指针的单向链表,写一个类包括以下函数:
返回链表的大小,
打印链表,
检测值x是否在链表中,
如果x不在链表中则加入链表,
如果x在链表中则删除它。
template <typename Object>
struct Node
{
Object data;
Node *next;
Node (const Object & d = Object(),Node *n=NULL)
:data(d),next(n){}
};
template <typename Object>
class singleList {
public:
singleList(){init();}
~singleList()
{
eraseList(head);
}
singleList(const singleList & rhs)
{
eraseList(head);
init();
*this=rhs;
}
bool add(Object x)
{
if(contains(x))
{
return false;
}
else
{
Node<Object> *ptr =new Node<Object>(x);
ptr->next=head->next;
head->next=ptr;
theSize++;
}
return true;
}
bool remove(Object x)
{
if(!contains(x))
return false;
else
{
Node<Object>*ptr=head->next;
Node<Object>*trailer;
while(ptr->data!=x)
{
trailer=ptr;
ptr=ptr->next;
}
trailer->next=ptr->next;
delete ptr;
theSize--;
}
return true;
}
int size()
{
return theSize;
}
void print()
{
Node<Object> *ptr=head->next;
while(ptr!=NULL)
{
count<<ptr->data<<" ";
ptr=ptr->next;
}
count<<endl;
}
bool contains(const Object & x)
{
Node<Object> * ptr=head->next;
while(ptr!=NULL)
{
if(x==ptr->data)
return true;
else
ptr=ptr->next;
}
return false;
}
void init()
{
theSize=0;
head=new Node<Object>;
head->next=NULL;
}
void eraseList(Node<Object> * h)
{
Node<Object> *ptr=h;
Node<Object> *nextPtr;
while(ptr!=NULL)
{
nextPtr=ptr->next;
delete ptr;
ptr=nextPtr;
}
}
private:
Node<Object> *head;
int theSize;
};
const_iterator & operator-- ()
{
current=current->prev;
return *this;
}
const_iterator operator-- (int)
{
const_iterator old=*this;
--(*this);
return old;
}
函数 | 备注 |
---|---|
push(x) | 将项x插入到双端队列的前段 |
pop() | 从双端队列删除前段项并返回 |
inject(x) | 将项x插入到双端队列的尾端 |
eject() | 从双端队列中删除尾端项并将其返回 |
template <typename Object>
class deque
{
public:
deque(){ l();}
void push (Object obj) {l.push_front(obj);}
Object pop () {Object obj=l.front();pop_front();return obj;}
void inject (Object obj) {l.push_back(obj);}
Object eject() {pop_back(obj);}
private:
list<Object> l;
}
6.不包含表头结点和尾结点,用单向链表高效地实现栈类。
template<typename @H_656_403@Object>
struct node
{
node() {next=@H_656_403@NULL;}
node(@H_656_403@Object obj):data(obj){}
node(@H_656_403@Object obj,node * ptr):data(obj),next(ptr){}
@H_656_403@Object data;
node *next;
};
template<typename @H_656_403@Object>
class stack { public: stack(){head=@H_656_403@NULL;} ~stack(){while(head) pop();} void push(@H_656_403@Object obj) { node<object> *ptr=new node<@H_656_403@Object>(obj,head); head=ptr; } @H_656_403@Object top() { return (head->data); } void pop() { node<@H_656_403@Object> *ptr=head->next; delete head; head=ptr; } private: node<@H_656_403@Object> *head; }
7.不包含表头结点和尾结点,用单向链表高效地实现队列类。
template<typename Object>
class queue
{
public:
queue(){front=NULL;rear=NULL;}
~queue(){while(front)deque();}
void deque(Object obj)
{
node<Object> *ptr=new node<Object>(obj,NULL);
if(rear)
rear=rear->next=ptr;
else
front=rear=ptr;
}
Object deque()
{
Object temp=front->data;
node<Object> *ptr=front;
if(front->next==NULL)
front=rear=NULL;
else
front=front->next;
delete ptr;
return temp;
}
private:
node<Object> *front;
node<Object> *rear;
}
8.使用由vector作为基本的数据结构的循环数组高效地实现队列类。
template<typename Object>
class queue
{
public:
queue(int s):maxSize(s),front(0),rear(0){elements.resize(maxSize);}
queue(){maxSize=100;front=0;rear=0;elements.resize(maxSize);}
~queue(){while(front!=rear) deque();}
void enque(Object obj)
{
if(!full())
{
elements[rear]=obj;
rear=(rear+1)%maxSize;
}
}
Object deque()
{
Object temp;
if(!empty())
{
temp=elements[front];
front=(front+1)%maxSize;
return temp;
}
}
bool empty(){return front==rear;}
bool full(){return (rear+1)%maxSize==front;}
private:
int front,rear;
int maxSize;
vector<Object> elements;
}
感谢您的访问,希望对您有所帮助。
欢迎大家关注或收藏、评论或点赞。
为使本文得到斧正和提问,转载请注明出处:
http://blog.csdn.net/nomasp