/**/
////
用c ++ 中的list容器就比较容易了。
#include < iostream >
#include < list >
#include < algorithm >
using namespace std;
int main()
... {
list<int> li;
for (int i = 0; i < 10; ++i)
...{
li.push_back(i);
}
copy (li.begin(), li.end(),
ostream_iterator<int>(cout, " "));
li.reverse();
cout << endl;
copy (li.begin(), li.end(),
ostream_iterator<int>(cout, " "));
return 0;
}
/**/ /////
template < class T >
void ListRev(node < T >** List)
... {
node* pre = NULL;
node* pos = NULL;
node* cur = *List;
while(cur != NULL)
...{
pos = cur->next;
cur->next = pre;
pre = cur;
cur = pos;
}
*node = pre;
}
/**/ /////
// test.cpp : 定义控制台应用程序的入口点。
//
#include " stdafx.h "
#include < iostream >
#include < ctime >
using namespace std;
// int _tmain(int argc, _TCHAR* argv[])
struct Node
... {
public:
Node():_val(0),_Next(NULL)
...{
}
Node(int i):_val(i),_Next(NULL)
...{
}
Node(int i, Node *p):_val(i),_Next(p)
...{
}
public:
int _val;
Node * _Next;
~Node()
...{
if(_Next)
...{
delete _Next;
}
}
} ;
typedef Node * LinkNode;
LinkNode CreateLink( int len, int MAX_BOUND = 100 )
... {
srand((unsigned)time(NULL));
LinkNode tmp,head;
head = new Node(-1);
tmp = head;
for (int i=0; i<len; i++)
...{
tmp = tmp->_Next= new Node(rand()%MAX_BOUND);
}
tmp->_Next =NULL;
return head;
}
void display( LinkNode head)
... {
LinkNode p=head;
while(p!= NULL)
...{
cout << p->_val<< endl;
p=p->_Next;
}
}
LinkNode reverselink(LinkNode head)
... {
//LinkNode head =head1;
LinkNode p = NULL;
LinkNode q =NULL;
while(head!= NULL)
...{
q=head->_Next;
head->_Next= p;
p= head;
head = q;
}
return p;
}
int main( int argc, char * argv[])
... {
LinkNode head = CreateLink(10);
display(head);
LinkNode head1= reverselink(head);
display(head1);
delete head;
head1 = NULL;
system("PAUSE");
return 0;
}
/**/ /////
大体思路是这样的:
1 、用另外一个临时头指针ph指向头节点来接管这个链表,把原来的头指针head设为空,这样head领头的链表就是个空链表了。
2 、循环:逐个把临时链表中的节点摘下来,安放到head领头的链表的最前面。
void reverse()
... {
//point to the old list
node* ph = head;//point to the first node in old list
node *pt;//a temp pointer
head = NULL;//read for new list
while(ph!=NULL)
...{//for each node in old list
//point to the second node in old list
pt = ph->next;
//move old head node to new list
ph->next = head;
head = ph;
//in old list,the second node becomes the first
ph = pt;
}
}
/**/ /////
H是一个单链表的头指针,使用指针P,Q使链表反转,写出程序
1 按照题目要求。(只能使用两个指针)
2 代码正确的同时考虑特殊情况。比如H为NULL
3 代码尽量简洁。批改面试题目的人不会有耐心看完冗长的代码的。越短的代码越有吸引力。
4 当然必要的注视也是必须的。
Q = NULL; // Q用来保存H的上一个节点。
while (H != NULL)
... {
P = H.next; // P保存H的下一个节点。
H.next = Q; // 把H和上一个节点链接起来。
Q = H; // 保存当前节点到Q
H = P; // 恢复H,便移动到了下一个节点。
}
H = Q;
/**/ /////
用c ++ 中的list容器就比较容易了。
#include < iostream >
#include < list >
#include < algorithm >
using namespace std;
int main()
... {
list<int> li;
for (int i = 0; i < 10; ++i)
...{
li.push_back(i);
}
copy (li.begin(), li.end(),
ostream_iterator<int>(cout, " "));
li.reverse();
cout << endl;
copy (li.begin(), li.end(),
ostream_iterator<int>(cout, " "));
return 0;
}
/**/ /////
template < class T >
void ListRev(node < T >** List)
... {
node* pre = NULL;
node* pos = NULL;
node* cur = *List;
while(cur != NULL)
...{
pos = cur->next;
cur->next = pre;
pre = cur;
cur = pos;
}
*node = pre;
}
/**/ /////
// test.cpp : 定义控制台应用程序的入口点。
//
#include " stdafx.h "
#include < iostream >
#include < ctime >
using namespace std;
// int _tmain(int argc, _TCHAR* argv[])
struct Node
... {
public:
Node():_val(0),_Next(NULL)
...{
}
Node(int i):_val(i),_Next(NULL)
...{
}
Node(int i, Node *p):_val(i),_Next(p)
...{
}
public:
int _val;
Node * _Next;
~Node()
...{
if(_Next)
...{
delete _Next;
}
}
} ;
typedef Node * LinkNode;
LinkNode CreateLink( int len, int MAX_BOUND = 100 )
... {
srand((unsigned)time(NULL));
LinkNode tmp,head;
head = new Node(-1);
tmp = head;
for (int i=0; i<len; i++)
...{
tmp = tmp->_Next= new Node(rand()%MAX_BOUND);
}
tmp->_Next =NULL;
return head;
}
void display( LinkNode head)
... {
LinkNode p=head;
while(p!= NULL)
...{
cout << p->_val<< endl;
p=p->_Next;
}
}
LinkNode reverselink(LinkNode head)
... {
//LinkNode head =head1;
LinkNode p = NULL;
LinkNode q =NULL;
while(head!= NULL)
...{
q=head->_Next;
head->_Next= p;
p= head;
head = q;
}
return p;
}
int main( int argc, char * argv[])
... {
LinkNode head = CreateLink(10);
display(head);
LinkNode head1= reverselink(head);
display(head1);
delete head;
head1 = NULL;
system("PAUSE");
return 0;
}
/**/ /////
大体思路是这样的:
1 、用另外一个临时头指针ph指向头节点来接管这个链表,把原来的头指针head设为空,这样head领头的链表就是个空链表了。
2 、循环:逐个把临时链表中的节点摘下来,安放到head领头的链表的最前面。
void reverse()
... {
//point to the old list
node* ph = head;//point to the first node in old list
node *pt;//a temp pointer
head = NULL;//read for new list
while(ph!=NULL)
...{//for each node in old list
//point to the second node in old list
pt = ph->next;
//move old head node to new list
ph->next = head;
head = ph;
//in old list,the second node becomes the first
ph = pt;
}
}
/**/ /////
H是一个单链表的头指针,使用指针P,Q使链表反转,写出程序
1 按照题目要求。(只能使用两个指针)
2 代码正确的同时考虑特殊情况。比如H为NULL
3 代码尽量简洁。批改面试题目的人不会有耐心看完冗长的代码的。越短的代码越有吸引力。
4 当然必要的注视也是必须的。
Q = NULL; // Q用来保存H的上一个节点。
while (H != NULL)
... {
P = H.next; // P保存H的下一个节点。
H.next = Q; // 把H和上一个节点链接起来。
Q = H; // 保存当前节点到Q
H = P; // 恢复H,便移动到了下一个节点。
}
H = Q;
/**/ /////