当前位置: 首页 > news >正文

代码下载网站河北恒山建设集团网站

代码下载网站,河北恒山建设集团网站,网站百度云,网上写作文的网站n个节点的二叉树n1Given a linked list and an integer n, append the last n elements of the LL to front. Assume given n will be smaller than length of LL. 给定一个链表和一个整数n#xff0c;将LL的最后n个元素附加到前面。 假设给定的n将小于LL的长度。 Input form…n个节点的二叉树n1Given a linked list and an integer n, append the last n elements of the LL to front. Assume given n will be smaller than length of LL. 给定一个链表和一个整数n将LL的最后n个元素附加到前面。 假设给定的n将小于LL的长度。 Input format: Line 1: Linked list elements (separated by space and terminated by -1 输入格式第1行链接的列表元素(以空格分隔并以-1终止 Sample Input 1 : 1 2 3 4 5 -1 3 Sample Output 1 : 3 4 5 1 2 Description: 描述 The question asks us to append the last N nodes to front, i.e the new linked list should first start from those N nodes and then traverse the rest of the nodes through the head of the old linked list. 这个问题要求我们将最后的N个节点附加到前面即新的链表应首先从这N个节点开始然后再通过旧链表的头遍历其余节点。 Example: 例 For Linked List 1-2-3-4-5-6-NULL To append the last 2 nodes, the new linked list should be: 5-6-1-2-3-4-NULL Solution Explanation: 解决方案说明 To solve this problem, we take two pointers temp and t and point both of them to the head of the linked list. We take another variable i and equate it to – n. This i is used for finding out the head of the new linked list. Then we traverse the loop while temp ! NULL. In the loop we check that if(i0) i.e temp is now n nodes away from t, t t- next. We will update i and temp temp-next on each traversal. At last, we update temp- next head, head t - next and t- next NULL. 为了解决这个问题我们使用两个指针temp和t并将它们都指向链接列表的开头。 我们采用另一个变量i并将其等于– n 。 我用于查找新链表的标题。 然后我们在temp NULL时遍历循环。 在循环中我们检查if(i 0)即temp现在距离t距离n个节点 t t- next 。 我们将在每次遍历时更新i 和temp temp- next 。 最后我们更新temp- next head head t- next和t- next NULL 。 Algorithm: 算法 STEP 1: Declare the function appendNNode with parameters (Node* head, int n) 步骤1使用参数声明函数appendNNode (Node * headint n) STEP 2: Declare two variables Node * temp , t and point both of them to head. 步骤2声明两个变量Node * temp t并将它们都指向head。 STEP 3: Declare int i -n 步骤3声明int i -n STEP 4: Repeat Step 5 and 6, while(temp-next ! NULL) 步骤4重复步骤5和6 同时(temp- next NULL) STEP 5: if(i0) t t- next. 步骤5 if(i 0)t t- next 。 STEP 6: temp temp- next, i. 步骤6 temp temp-接下来i 。 STEP 7: temp-next head, head t-next, and t- next NULL 步骤7 temp- next head head t- next和t- next NULL STEP 8: return head 步骤8返回头 Steps: 脚步 At first: 1-2-3-4-5-6-NULL, t-1 and temp-1. After complete traversal: 1-2-3-4-5-6-NULL, t-4 and temp-6. So, temp-next head and head t-next i.e 5-6-1-2-3-4 --- (reconnecting to 5) Atlast, t- next NULL i.e 5-6-1-2-3-4-NULL .minHeight{ min-height: 250px; } @media (min-width: 1025px){ .minHeight{ min-height: 90px; } } .minHeight{ min-height: 250px; } @media (min-width: 1025px){ .minHeight{ min-height: 90px; } } Function: 功能 Node *appendNNodes(Node* head, int n){ // Two pointers, one for traversal and // other for finding the new head of LL Node *temp head, *t head; //index maintained for finding new head int i -n; while(temp-next!NULL){ //When temp went forward n nodes from t if(i0){ t t-next; } temp temp -next; i; } //Connecting the tail to head temp-next head; //Assigning the new node head t-next; //Deleting the previous connection t-next NULL; return head; } C Code: C 代码 #includebits/stdc.h using namespace std; struct Node{// linked list Node int data; Node * next; }; Node *newNode(int k){ //defining new node Node *temp (Node*)malloc(sizeof(Node)); temp-data k; temp-next NULL; return temp; } //Used to add new node at the end of the list Node *addNode(Node* head, int k){ if(head NULL){ head newNode(k); } else{ Node * temp head; Node * node newNode(k); while(temp-next! NULL){ temp temp-next; } temp- next node; } return head; } // Used to create new linked list and return head Node *createNewLL(){ int cont 1; int data; Node* head NULL; while(cont){ coutEnter the data of the Nodeendl; cindata; head addNode(head,data); coutDo you want to continue?(0/1)endl; cincont; } return head; } //To print the Linked List void *printLL(Node * head){ while(head! NULL){ couthead-data-; head head- next; } coutNULLendl; } //Function Node *appendNNodes(Node* head, int n){ // Two pointers, one for traversal and // other for finding the new head of LL Node *temp head, *t head; //index maintained for finding new head int i -n; while(temp-next!NULL){ //When temp went forward n nodes from t if(i0){ t t-next; } temp temp -next; i; } //Connecting the tail to head temp-next head; //Assigning the new node head t-next; //Deleting the previous connection t-next NULL; return head; } //Driver Main int main(){ Node * head createNewLL(); coutThe linked list isendl; printLL(head); int data; coutEnter the number of nodes you want to append.endl; cindata; head appendNNodes(head,data); coutThe new Linked List is endl; printLL(head); return 0; } .minHeight{ min-height: 250px; } @media (min-width: 1025px){ .minHeight{ min-height: 90px; } } .minHeight{ min-height: 250px; } @media (min-width: 1025px){ .minHeight{ min-height: 90px; } } Output 输出量 Enter the data of the Node 1 Do you want to continue?(0/1) 1 Enter the data of the Node 2 Do you want to continue?(0/1) 1 Enter the data of the Node 3 Do you want to continue?(0/1) 1 Enter the data of the Node 4 Do you want to continue?(0/1) 1 Enter the data of the Node 5 Do you want to continue?(0/1) 1 Enter the data of the Node 6 Do you want to continue?(0/1) 1 Enter the data of the Node 7 Do you want to continue?(0/1) 0 The linked list is 1-2-3-4-5-6-7-NULL Enter the number of nodes you want to append. 3 The new Linked List is 5-6-7-1-2-3-4-NULL 翻译自: https://www.includehelp.com/cpp-programs/append-last-n-nodes-to-first-in-the-linked-list.aspxn个节点的二叉树n1
http://www.zqtcl.cn/news/420486/

相关文章:

  • 设计素材网站线上网站数据报表
  • 做一个小型网站多少钱wordpress 手机商城模板
  • 谷歌网站收录提交金山网站建设关键词排名
  • 域名备案中网站可以开通个人网站开发多少钱
  • 西安维护网站广州公司网站设计制作
  • 荆门做网站网络推广公司多久能回本
  • 搜索网站存在的关键字室内设计公司排名榜
  • 响应式网页开发昆明网站排名优化公司哪家好
  • 如东建设局网站线上购物平台
  • 重庆网站推广营销淘宝的网站怎么做的好
  • 重庆企业建站模板珠海企业官网设计制作
  • 网页作图软件东莞优化哪家好
  • 专业的商城网站开发深圳网站界面设计
  • 做网站需要自备服务器吗专业生产车间设计图纸网站
  • 用vs2010做网站教程昆明模板建站定制网站
  • dedecms网站模板下载做网站价格需要多少钱
  • 昆明餐饮网站建设建电影网站教程
  • 怎么做服装网站wordpress 主题 三栏
  • 个人可否建立网站全包装修
  • 哈尔滨网站建设贴吧网站建设推广好做吗
  • 南宁网站建设排名制作网站的公司做网站去哪里找
  • 网站开发外贸材料信息价查询网站
  • 推荐几个好的seo网站程序模板WordPress博客建站系统
  • 手机网站建设推广方案ppt模板wordpress文章阅读统计
  • 自己可以接单做网站吗建设项目所在地公共媒体网站
  • 哈尔滨网站制作哪儿好薇学校网站首页代码html
  • 网站建设与设计 毕业设计企业自助网站建设
  • ip库网站源码佛山网站开发公司
  • 婚庆网站怎么设计模板电子商务系统规划方案
  • 东莞中企动力做网站wordpress结合tornado