secret

sasdassdadf
 avatar
unknown
c_cpp
4 years ago
4.8 kB
38
Indexable
// A complete working C++ program to
// demonstrate all insertion methods
#include <bits/stdc++.h>
using namespace std;
 
// A linked list node
class Node
{
    public:
    string data;
    Node* next;
    Node* prev;
};
 

void push(Node** head_ref, string new_data)
{
    /* 1. allocate node */
    Node* new_node = new Node();
 
    /* 2. put in the data */
    new_node->data = new_data;
 
    /* 3. Make next of new node as head
    and previous as NULL */
    new_node->next = (*head_ref);
    new_node->prev = NULL;
 
    /* 4. change prev of head node to new node */
    if ((*head_ref) != NULL)
        (*head_ref)->prev = new_node;
 
    /* 5. move the head to point to the new node */
    (*head_ref) = new_node;
}
 
void insertAfter(Node* prev_node, string new_data)
{
    /*1. check if the given prev_node is NULL */
    if (prev_node == NULL)
    {
        cout<<"the given previous node cannot be NULL";
        return;
    }
 
    /* 2. allocate new node */
    Node* new_node = new Node();
 
    /* 3. put in the data */
    new_node->data = new_data;
 
    /* 4. Make next of new node as next of prev_node */
    new_node->next = prev_node->next;
 
    /* 5. Make the next of prev_node as new_node */
    prev_node->next = new_node;
 
    /* 6. Make prev_node as previous of new_node */
    new_node->prev = prev_node;
 
    /* 7. Change previous of new_node's next node */
    if (new_node->next != NULL)
        new_node->next->prev = new_node;
}
 
void append(Node** head_ref, string new_data)
{
    /* 1. allocate node */
    Node* new_node = new Node();
 
    Node* last = *head_ref; /* used in step 5*/
 
    /* 2. put in the data */
    new_node->data = new_data;
 
    /* 3. This new node is going to be the last node, so
        make next of it as NULL*/
    new_node->next = NULL;
 
    /* 4. If the Linked List is empty, then make the new
        node as head */
    if (*head_ref == NULL)
    {
        new_node->prev = NULL;
        *head_ref = new_node;
        return;
    }
 
    /* 5. Else traverse till the last node */
    while (last->next != NULL)
        last = last->next;
 
    /* 6. Change the next of last node */
    last->next = new_node;
 
    /* 7. Make last node as previous of new node */
    new_node->prev = last;
 
    return;
}
 
void deleteNode(Node** head_ref, string key)
{
     
    // Store head node
    Node* temp = *head_ref;
    Node* prev = NULL;
     
    // If head node itself holds
    // the key to be deleted
    if (temp != NULL && temp->data == key)
    {
        *head_ref = temp->next; // Changed head
        delete temp;            // free old head
        return;
    }
 
    // Else Search for the key to be deleted,
    // keep track of the previous node as we
    // need to change 'prev->next' */
      else
    {
    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 memory
    delete temp;
    }
}
 
bool search(Node* head, string ngalan) 
{ 
    Node* current = head; // Initialize current 
    while (current != NULL) 
    { 
        if (current->data == ngalan) 
        {
            cout << "Found "; 
            current = current->next;
            return 1;
        }
        else
        {
            cout << "Not found "; 
            return 1;
        }
            
    } 
    
} 

void printList(Node* node)
{
    Node* last;
    cout<<"\nAng naa sa sulod sa list kay silang: \n";
    while (node != NULL)
    {
        cout<<" "<<node->data<<" ";
        last = node;
        node = node->next;
    }
 }
 
/* Driver program to test above functions*/
int main()
{
    int number;
    string ngalan;
    /* Start with the empty list */
    Node* head = NULL;
          for (int i = 0; i < 5; i++) 
        {

            cout << "Unsa imong ngalan: ";
            cin >> ngalan;
            // Insert 7 at the beginning. So
            // linked list becomes 7->6->NULL
            push(&head, ngalan);
 
            printList(head);
            cout << "\n";
        }
        for (int i = 0; i < 2; i++) 
        {

            cout << "Kinsay e delete? ";
            cin >> ngalan;
            // Insert 7 at the beginning. So
            // linked list becomes 7->6->NULL
            deleteNode(&head, ngalan);
 
            printList(head);
            cout << "\n";
        }
        for (int i = 0; i < 2; i++) 
        {

            cout << "Kinsay e search? ";
            cin >> ngalan;
            // Insert 7 at the beginning. So
            // linked list becomes 7->6->NULL
            search(head, ngalan);
 
            printList(head);
            cout << "\n";
        }



}
Editor is loading...