ProDeveloperTutorial.com

教程和编程解决方案
菜单
  • Shell脚本
  • 系统设计
  • Linux系统编程
  • 4g LTE
  • 编码问题
  • C
  • C ++
  • DSA
  • GIT

从单个链接列表执行以下删除操作,C ++解决方案

前开发者教程 2019年2月24日
  1. 删除所有出现的给定键
  2. 删除所有大于给定键的节点

C ++解决方案

#include<iostream>
#include<vector>
#include<string>

using namespace std;

struct Node
{
	int data;
	Node *next;

	Node(int x)
	{
		data = x;
		next = NULL;
	}
};

void print_list(Node *head)
{
	Node *start = head;

	while(start)
	{
		cout<<start->data<<" -> ";
		start = start->next;
	}
	cout<<"\n";
}


void delete_all_occurrences_of_a_given_key(Node **head, int key)
{
    Node *temp = *head, *prev; 
  
    // If head node itself holds the key or multiple occurrences of key 
    while (temp != NULL && temp->data == key) 
    { 
        *head = temp->next;   // Changed head 
        free(temp);               // free old head 
        temp = *head;         // Change Temp 
    } 
  
    // Delete occurrences other than head 
    while (temp != NULL) 
    { 
        // Search for the key to be deleted, keep track of the 
        // previous node as we need to change 'prev->next' 
        while (temp != NULL && temp->data != key) 
        { 
            prev = temp; 
            temp = temp->next; 
        } 
  
        // If key was not present in linked list 
        if (temp == NULL) return; 
  
        // Unlink the node from linked list 
        prev->next = temp->next; 
  
        free(temp);  // Free memory 
  
        //Update Temp for next iteration of outer loop 
        temp = prev->next; 
    } 
}

void delete_all_elements_greater_than_given_key(Node** head, int key) 
{ 
    Node *temp = *head, *prev; 
  
    // If head node itself holds the value greater than 'key' 
    while (temp != NULL && temp->data > key) 
    { 
        *head = temp->next; // Changed head 
        free(temp); // free old head 
        temp = *head; // Change temp 
    } 
  
    // Delete occurrences other than head 
    while (temp != NULL) { 
  
        // Search for the node to be deleted,  
        // keep track of the previous node as we  
        // need to change 'prev->next' 
        while (temp != NULL && temp->data <= key) 
        { 
            prev = temp; 
            temp = temp->next; 
        } 
  
        // If required value node was not present 
        // in linked list 
        if (temp == NULL) 
            return; 
  
        // Unlink the node from linked list 
        prev->next = temp->next; 
  
        delete temp;
  
        // Update Temp for next iteration of  
        temp = prev->next; 
    } 
} 


int main()
{
	Node *head = new Node(2);
	head -> next = new Node(1);
	head -> next -> next = new Node(4);
	head -> next -> next -> next = new Node(3);
	head -> next -> next -> next  -> next = new Node(6);
	head -> next -> next -> next  -> next -> next = new Node(5);

	cout<<"The original list = "<<endl;
	print_list(head);

	int key = 3;

	delete_all_occurrences_of_a_given_key (&head, key );

	cout<<"The list after deleting "<< key <<" key is = "<<endl;
	print_list(head);

	delete_all_elements_greater_than_given_key (&head, key );
	cout<<"The list after deleting the elements greater than "<< key <<" key is = "<<endl;
	print_list(head);


	return 0;
}

该网站上可用的教程列表:

C编程20+章C ++编程80+章
100多个编码问题数据结构和算法85+章
系统设计20+章Shell脚本编写12章
4g LTE 60+章节最常见的编码问题
5G NR 50+章Linux系统编程20+章
分享
电子邮件
鸣叫
领英
Reddit
绊倒
Pinterest的
上一篇文章
下一篇

关于作者

前开发者教程

每天我们都会讨论竞争性编程问题,请加入我们的网站:   电报频道

ProDeveloperTutorial.com

教程和编程解决方案
版权© 2020 ProDeveloperTutorial.com
从以下课程获得热门课程: 教育性的

      <time id="YsI9t3x" class="YkgfNo4"><summary id="yKGpFb2"><input class="JmeGe8n"><section id="jR9ZuC9"></section></input></summary></time>
      <p></p>




      • <tfoot id="B6ek62z" class="Bv4JTkz"></tfoot>


          <big id="rkwUIwZ"></big>
          <source id="sasdOYe" class="stt4k0i"></source>