Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- typedef struct listitem
- {
- struct listitem *next;
- struct listitem *prev;
- int data;
- } LISTITEM;
- int main()
- {
- LISTITEM *temp, head;
- head.next = (LISTITEM*)&head; // contains the address of head
- head.prev = (LISTITEM*)&head; // also contains the address of head
- head.data = -1;
- // populate the list
- for(int i = 0; i < 3; i++)
- {
- temp = malloc(sizeof(LISTITEM));
- temp->data = i;
- temp->next = head.next;
- head.next = temp;
- temp->prev = &head;
- temp->next->prev = temp;
- }
- // now lets see what we have going forward
- temp = head.next;
- while(temp != &head)
- {
- printf("forward list item: current is %p; next is %p; prev is %p; data is %d\n", temp, temp->next, temp->prev, temp->data);
- temp = temp->next;
- }
- // and going backwards
- temp = head.prev;
- while(temp != &head)
- {
- printf("forward list item: current is %p; next is %p; prev is %p; data is %d\n", temp, temp->next, temp->prev, temp->data);
- temp = temp->prev;
- }
- return 0;
- }
Add Comment
Please, Sign In to add comment