📌  相关文章
📜  XOR链接列表:删除链接列表的最后一个节点

📅  最后修改于: 2021-04-17 14:50:52             🧑  作者: Mango

给定一个XOR链表,任务是删除XOR链表末尾的节点。

例子:

方法:解决此问题的想法是遍历XOR链表,直到到达最后一个节点并更新其前一个节点的地址。请按照以下步骤解决问题:

  • 如果“异或”链接列表为空,则打印“列表为空”。
  • 遍历XOR链接列表,直到到达链接列表的最后一个节点。
  • 更新其上一个节点的地址。
  • 从内存中删除最后一个节点。
  • 如果删除最后一个节点后列表变为空,则打印“列表为空” 。否则,打印链接列表的其余节点。

下面是上述方法的实现:

C
// C program for the above approach
  
#include 
#include 
#include 
  
// Structure of a node
// in XOR linked list
struct Node {
  
    // Stores data value
    // of a node
    int data;
  
    // Stores XOR of previous
    // pointer and next pointer
    struct Node* nxp;
};
  
// Function to find the XOR of two nodes
struct Node* XOR(struct Node* a, struct Node* b)
{
    return (struct Node*)((uintptr_t)(a)
                          ^ (uintptr_t)(b));
}
  
// Function to insert a node with
// given value at given position
struct Node* insert(struct Node** head, int value)
{
    // If XOR linked list is empty
    if (*head == NULL) {
  
        // Initialize a new Node
        struct Node* node
            = (struct Node*)malloc(
                sizeof(struct Node));
  
        // Stores data value in
        // the node
        node->data = value;
  
        // Stores XOR of previous
        // and next pointer
        node->nxp = XOR(NULL, NULL);
  
        // Update pointer of head node
        *head = node;
    }
  
    // If the XOR linked list
    // is not empty
    else {
  
        // Stores the address
        // of current node
        struct Node* curr = *head;
  
        // Stores the address
        // of previous node
        struct Node* prev = NULL;
  
        // Initialize a new Node
        struct Node* node
            = (struct Node*)malloc(
                sizeof(struct Node));
  
        // Update curr node address
        curr->nxp = XOR(node,
                        XOR(NULL, curr->nxp));
  
        // Update new node address
        node->nxp = XOR(NULL, curr);
  
        // Update head
        *head = node;
  
        // Update data value of
        // current node
        node->data = value;
    }
    return *head;
}
  
// Function to print elements of
// the XOR Linked List
void printList(struct Node** head)
{
    // Stores XOR pointer
    // in current node
    struct Node* curr = *head;
  
    // Stores XOR pointer of
    // in previous Node
    struct Node* prev = NULL;
  
    // Stores XOR pointer of
    // in next node
    struct Node* next;
  
    // Traverse XOR linked list
    while (curr != NULL) {
  
        // Print current node
        printf("%d ", curr->data);
  
        // Forward traversal
        next = XOR(prev, curr->nxp);
  
        // Update prev
        prev = curr;
  
        // Update curr
        curr = next;
    }
}
  
// Function to delete the last node
// present in the XOR Linked List
struct Node* delEnd(struct Node** head)
{
    // Base condition
    if (*head == NULL)
        printf("List is empty");
    else {
  
        // Stores XOR pointer
        // in current node
        struct Node* curr = *head;
  
        // Stores XOR pointer of
        // in previous Node
        struct Node* prev = NULL;
  
        // Stores XOR pointer of
        // in next node
        struct Node* next;
  
        // Traverse XOR linked list
        while (XOR(curr->nxp, prev) != NULL) {
  
            // Forward traversal
            next = XOR(prev, curr->nxp);
  
            // Update prev
            prev = curr;
  
            // Update curr
            curr = next;
        }
        // If the Linked List contains more than 1 node
        if (prev != NULL)
            prev->nxp = XOR(XOR(prev->nxp, curr), NULL);
  
        // Otherwise
        else
            *head = NULL;
  
        // Delete the last node from memory
        free(curr);
    }
  
    // Returns head of new linked list
    return *head;
}
  
// Driver Code
int main()
{
  
    /* Create the XOR Linked List
    head-->40<-->30<-->20<-->10 */
    struct Node* head = NULL;
    insert(&head, 10);
    insert(&head, 20);
    insert(&head, 30);
    insert(&head, 40);
  
    delEnd(&head);
  
    // If the list had a single node
    if (head == NULL)
        printf("List is empty");
    else
        // Print the list after deletion
        printList(&head);
  
    return (0);
}


输出:
40 30 20

时间复杂度: O(N)
辅助空间: O(1)