Advertisement
Guest User

Untitled

a guest
Jul 27th, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.30 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3.  
  4. struct node {
  5.  int data;
  6.  unsigned long ptrOffset;
  7. };
  8.  
  9. struct xorList {
  10.     struct node* head;
  11.     struct node* tail;
  12. };
  13.  
  14. struct xorList* InitList(int data, int data2) {
  15.  struct node* head = malloc(sizeof(struct node));
  16.  struct node* tail = malloc(sizeof(struct node));
  17.  head->ptrOffset = (unsigned long)((unsigned long)NULL ^ (unsigned long)tail);
  18.  head->data = data;
  19.  tail->ptrOffset = (unsigned long)((unsigned long)head ^ (unsigned long)NULL);
  20.  tail->data = data2;
  21.  struct xorList* list = malloc(sizeof(struct xorList));
  22.  list->head = head;
  23.  list->tail = tail;
  24.  
  25.  return list;
  26. }
  27.  
  28. void Append(struct node* head, struct node* tail, int data) {
  29.  struct node* newNode = malloc(sizeof(struct node));
  30.  
  31.  newNode->ptrOffset = (unsigned long)((unsigned long) head ^ (unsigned long)tail);
  32.  newNode->data = data;
  33. }
  34.  
  35. void main(){
  36.  struct xorList* list = InitList(1, 2);
  37.  //Append(head, 2);
  38.  int count = 0;
  39.  struct node* current = list->head;
  40.  struct node* begin = list->head;
  41.  struct node* prev = NULL;
  42.  while (current != NULL) {
  43.   count++;
  44.   if (current == begin) {
  45.    current = (struct node*)((unsigned long)prev ^ current->ptrOffset);
  46.   }
  47.   else {
  48.    current = (struct node*)((unsigned long)prev ^ current->ptrOffset);
  49.   }
  50.  }
  51.  printf("%d", count);
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement