C++程序设计 编写程序实现单链表逆置功能。
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C++程序设计 编写程序实现单链表逆置功能。相关的知识,希望对你有一定的参考价值。
一、设计任务:
将单向链表逆置,如 ABCD 变成 DCBA,只能搜索链表一次。
二、功能要求:
1、定义链表结点为结构体类型
struct linknode
int data;
struct linknode *next;
;
2、设计一个函数 createList()创建一个至少包含 5 个结点的单链表;
3、设计一个函数 reverse(node *head),将创建好的单链表进行逆置。
4、将逆置后的单链表结点显示到控制台窗口中。
struct linknode
int data;
struct linknode * next;
;
struct linknode *creat()
struct linknode *head;
struct linknode *p1,*p2;
n=0;
p1=p2=(struct linknode *)malloc(LEN);
scanf("%ld%d",&p1->num,&p1->score);/*%d和%d之间不应该有逗号*/
head=NULL;
while (p1->num!=0)
n=n+1;
if(n==1) head=p1;
else p2->next=p1;
p2=p1;
p1=(struct linknode*)malloc(LEN);
scanf("%ld%d",&p1->num,&p1->score);
p2->next=NULL;
return(head);
void reverse(node *head)
struct linknode *p,*sta;
printf("\\n倒序前:");
p=head;
for(i=0;i<=LEN;i++)
printf("%d ",p->data);
p=p->next;
/*************************/
p=NULL;
while(head->next != NULL)
sta=head;
head=head->next;
sta->next=p;
p=sta;
head->next=sta;
printf("\\n倒序后:");
p=head;
for(i=1;i<=LEN;i++)
printf("%d ",p->data);
p=p->next;
int main()
struct linknode *head;
head = creat();
reverse(linknode *head);
return 0;
/*************************/
//随便写的没编译过.本回答被提问者和网友采纳 参考技术B push到栈里面在pop出来追问
有没有完整的代码。。。
单链表类,链表逆置
1 #pragma once 2 #include"iostream" 3 #include <cstdlib> 4 #include <time.h> 5 using namespace std; 6 7 struct Node 8 { 9 int val; 10 Node *next; 11 Node(int nVal, Node *nNext = NULL) { 12 val = nVal; 13 next = nNext; 14 } 15 Node() {} 16 }; 17 18 class ListNode 19 { 20 private: 21 Node *head; //声明头节点 22 int counts; 23 24 public: 25 ListNode(); 26 ~ListNode(); 27 void CreateList(int n); 28 void CreateListByPC(int n,int randnum); 29 ListNode* Reverse(); 30 ListNode* ReverseBetween(int m, int n); 31 void ShowList(); 32 int ListLength(); 33 void InsertNode(int position, int value); 34 void DeleteNode(int position); 35 Node *GetNodeByPosition(int position); 36 };[/align] 37 [align=center] 38 [size=6][color=#48d1cc]源文件[/color][/size]
实现文件如下:
#include "ListNode.h" ListNode::ListNode() { head = new Node; head->next = NULL; head->val = 0; } ListNode::~ListNode() { Node * temp = head; //创建一个临时变量保存删除节点信息 while(head->next){ temp = head->next; delete head; head = temp; } delete head; cout << "该链表析构完成..." << endl; } void ListNode::CreateList(int n) { cout << "正在创建长度为" << n << "的链表......" << endl; Node *preNode = head; for (int i = 0; i < n; i++) { Node *newNode = new Node; cout << "请输入该节点的值: "; cin >> newNode->val; newNode->next = NULL; preNode->next = newNode; preNode = newNode; } } void ListNode::CreateListByPC(int n, int randnum) { cout << "正在创建长度为" << n << "的链表,其值范围为:" << randnum << endl; srand((unsigned int)time(NULL)); Node *preNode = head; for (int i = 0; i < n; i++) { Node *newNode = new Node; newNode->val = rand()%randnum; newNode->next = NULL; preNode->next = newNode; preNode = newNode; } } ListNode * ListNode::Reverse() { cout << "正在逆置链表..." << endl; Node *newHead = NULL; //临时变量,遍历新链表 Node *vNode = head->next; //取第一个节点开始遍历 while (vNode!=NULL) { Node *temp = vNode->next; //备份原链表的下一个节点信息 vNode->next = newHead; //当前节点的指针域修改 newHead = vNode; vNode = temp; } head->next = newHead; return NULL; } ListNode * ListNode::ReverseBetween(int m,int n) { if (m <= 0 || n >= this->counts) return false; Node *temp = head; //临时变量保存头节点 int changeLength = n - m + 1; //获取需要逆置的个数 while (--m) { temp = temp->next; } Node *preNode = temp; //找到m节点的前置节点 temp = temp->next; Node *changeTail = temp; //m节点,即需要改变的尾节点 Node *newHead = NULL; cout << "changeLength = " << changeLength << endl; int i = 0; while (changeLength--) { cout << "正在执行第" << ++i << "ci" << endl; Node *vNode = temp->next; //备份下一个节点的信息 temp->next = newHead; newHead = temp; temp = vNode; } preNode->next = newHead; changeTail->next = temp; return NULL; } void ListNode::ShowList() { Node *tempNode = head; while (tempNode->next) { cout << tempNode->next->val <<" "; tempNode = tempNode->next; } cout << endl << "该链表已显示完毕..." << endl; } int ListNode::ListLength() { Node *temp = head; for (; temp->next != NULL; temp = temp->next) counts++; return counts; } void ListNode::InsertNode(int position, int value) { Node *temp = this->GetNodeByPosition(position - 1); Node *newNode = new Node(value); newNode->next = temp->next; temp->next = newNode; } void ListNode::DeleteNode(int position) { if (position<1 || position>this->ListLength()) return; Node *temp = this->GetNodeByPosition(position - 1); Node *delNode = temp->next; temp->next = delNode->next; delete delNode; } Node * ListNode::GetNodeByPosition(int position) { if (position<1 || position>this->ListLength()) return flase; Node *temp = head; for (int i = 0; i < position; i++) temp = temp->next; return temp; }
以上是关于C++程序设计 编写程序实现单链表逆置功能。的主要内容,如果未能解决你的问题,请参考以下文章