C++操作单链表ListNode
通过C++进行单链表的创建、打印以及利用栈实现逆序打印。
单链表的创建和打印
#include
using namespace std;
//定义结构体
struct ListNode{
int val;
ListNode* next;
};
class operateList
{
public:
/*创建单链表*/
void createList(ListNode *head)
{
int i;
ListNode* phead=head; //不破坏头指针
for(i=1;i<10;i++){
ListNode* node=new ListNode;
node->val=i;
node->next=NULL;
phead->next=node;
phead=node;
}
cout<
}
/*打印链表*/
void printList(ListNode* head)
{
ListNode* phead=head;
while(phead->next!=NULL)
{
cout<val<
phead=phead->next;
}
cout<val;