c++链表实现链表的翻转,即倒序输出 重新定义一个链表,将原来连表中的数据倒着插进去。
发布网友
发布时间:2022-04-25 18:00
我来回答
共2个回答
热心网友
时间:2023-10-23 04:19
谁这么教你链表的,真是误人子弟
链表链表当然要有表,你这种办法定义链表看着多累?
以下代码仅供参考,main函数仅用来示意链表的用法,请自己重写
#include"iostream"
#include"iomanip"
using namespace std;
template<typename T>class List;
template<typename T>class Node
{
private:
T info; //data field
Node<T> *link; //pointer field,type is node class
public:
Node(); //constructor that creates head node
Node(const T &data); //constructor that creates normal node
void InsertAfter(Node<T> *p); //insert a node after the current node
//delete the node after the current node,and return the node in order to backup
Node<T> *RemoveAfter();
friend class List<T>; //friend class
};
template<typename T>Node<T>::Node()
{
link=NULL;
}
template<typename T>Node<T>::Node(const T &data)
{
info=data;
link=NULL;
}
template<typename T>void Node<T>::InsertAfter(Node<T> *p)
{
p->link=link;
link=p;
}
template<typename T>Node<T> *Node<T>::RemoveAfter()
{
Node<T> *q=link;
if(link==NULL) //at the end of linked list
{
q=NULL;
}
else
{
link=q->link;
}
return q;
}
template<typename T>class List
{
private:
Node<T> *head,*tail; //the head pointer and tail pointer of the linked list
public:
List(); //constructor
~List(); //destructor
//empty the linked list,only leave head pointer and tail pointer
void MakeEmpty();
Node<T> *Find(T data); //find the data in data field,return the node of the data
int Length(); //calculate the length of the linked list
void PrintList(); //print the linked list
void InsertFront(Node<T> *p); //insert the node from header
void InsertRear(Node<T> *p); //insert the node from near
void InsertOrder(Node<T> *p); //ascendind insert
Node<T> *CreateNode(T data); //create a node with data,return the node
Node<T> *DeleteNode(Node<T> *p);//delete the node,return the pointer of node
};
template<typename T>List<T>::List()
{
head=tail=new Node<T>();
}
template<typename T>List<T>::~List()
{
MakeEmpty();
delete head;
}
template<typename T>void List<T>::MakeEmpty()
{
Node<T> *p;
while(head->link!=NULL) //not the end of linked list
{
p=head->link;
head->link=p->link;
delete p;
}
tail=head;
}
template<typename T>Node<T> *List<T>::Find(T data)
{
Node<T> *p;
p=head->link;
while(p!=NULL&&p->info!=data)
{
p=p->link;
}
return p;
}
template<typename T>int List<T>::Length()
{
int count=0;
Node<T> *p;
p=head->link;
while(p!=NULL)
{
p=p->link;
count++;
}
return count;
}
template<typename T>void List<T>::PrintList()
{
Node<T> *p;
p=head->link;
while(p!=NULL)
{
cout<<setw(5)<<p->info;
p=p->link;
}
cout<<endl;
}
template<typename T>void List<T>::InsertFront(Node<T> *p)
{
p->link=head->link;
head->link=p;
if(tail==head)
{
tail=p;
}
}
template<typename T>void List<T>::InsertRear(Node<T> *p)
{
p->link=tail->link;
tail->link=p;
tail=p;
}
template<typename T>void List<T>::InsertOrder(Node<T> *p)
{
Node<T> *tempp,*tempq; //tempq is the former one of tempp
tempp=head->link;
tempq=head;
while(tempp!=NULL&&(tempp->info<p->info))
{
tempq=tempp;
tempp=tempp->link;
}
tempq->InsertAfter(p);
if(tail==tempq)
{
tail=tempq->link;
}
}
template<typename T>Node<T> *List<T>::CreateNode(T data)
{
Node<T> *temp;
temp=new Node<T>(data);
return temp;
}
template<typename T>Node<T> *List<T>::DeleteNode(Node<T> *p)
{
Node<T> *q;
q=head;
while(q->link!=NULL&&q->link!=p)
{
q=q->link;
}
if(q->link==tail)
{
tail=q;
}
return q->RemoveAfter();
}
int main()
{
cout<<"This program is mainly used to show the usage of linked list!"<<endl;
Node<int> *p;
List<int> list1,list2;
int a[16],i,t;
cout<<"Please enter 16 integers:"<<endl;
for(i=0;i<16;i++)
{
cin>>a[i];
}
for(i=0;i<16;i++)
{
p=list1.CreateNode(a[i]);
list1.InsertFront(p);
p=list2.CreateNode(a[i]);
list2.InsertRear(p);
}
list1.PrintList();
cout<<"List1's length is: "<<list1.Length()<<endl;
list2.PrintList();
cout<<"List2's length is: "<<list2.Length()<<endl;
cout<<"Please enter an integer which needs to be deleted: "<<endl;
cin>>t;
p=list1.Find(t);
if(p!=NULL)
{
cout<<"Found!"<<endl;
while(p!=NULL)
{
p=list1.DeleteNode(p);
delete p;
p=list1.Find(t);
}
cout<<"After delete :"<<t<<endl;
list1.PrintList();
cout<<"List1's length is: "<<list1.Length()<<endl;
}
else
{
cout<<"Not found!"<<endl;
}
list1.MakeEmpty();
for(i=0;i<16;i++)
{
p=list1.CreateNode(a[i]);
list1.InsertOrder(p);
}
list1.PrintList();
cout<<"List1's length is: "<<list1.Length()<<endl;
system("pause");
return 0;
}
热心网友
时间:2023-10-23 04:19
这个不会耶……~