Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- struct list {
- list* prev;
- list* next;
- }
- struct element {
- int value;
- struct list* link;
- }
- void list_insert(list* list, list* element) {
- element->prev = list;
- element->next = list->next;
- list->next = element;
- element->next->prev = element;
- }
- // Initialize empty list
- struct list mylist;
- mylist.next = &mylist;
- mylist.prev = &mylist;
- // Initialize element
- struct element e;
- e.value = 42;
- // Add element to list
- list_insert(&mylist, &e);
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- // element within list
- typedef struct element {
- int value; // data value
- struct element *next; // pointer to next element in list
- struct element *prev; // pointer to previous element in list
- } element_t;
- // list of elements
- typedef struct list {
- element_t *head; // pointer to first element in list
- element_t *tail; // pointer to last element in list
- } list_t;
- // new_element -- get new element
- element_t *
- new_element(int value)
- {
- element_t *newptr;
- newptr = calloc(1,sizeof(element_t));
- if (newptr == NULL) {
- printf("new_element: malloc failuren");
- exit(1);
- }
- newptr->value = value;
- return newptr;
- }
- // list_insert -- insert before list head
- void
- list_insert(list_t *list,element_t *newptr)
- {
- element_t *head;
- head = list->head;
- newptr->prev = NULL;
- newptr->next = head;
- // insert at head of empty list
- if (head == NULL)
- list->tail = newptr;
- // insert at head of non-empty list
- else {
- newptr->next = head;
- head->prev = newptr;
- }
- // make new element the head of the list
- list->head = newptr;
- }
- // list_insert_unique -- insert before list head
- void
- list_insert_unique(list_t *list,element_t *newptr)
- {
- element_t *curptr;
- // search for pre-existing element with the same value
- for (curptr = list->head; curptr != NULL; curptr = curptr->next) {
- if (curptr->value == newptr->value)
- break;
- }
- // only insert if we did _not_ find one
- if (curptr == NULL)
- list_insert(list,newptr);
- else
- free(newptr);
- }
- // list_print -- print the list
- void
- list_print(list_t *list)
- {
- element_t *curptr;
- printf("list_print:");
- // search for pre-existing element with the same value
- for (curptr = list->head; curptr != NULL; curptr = curptr->next)
- printf(" %d",curptr->value);
- printf("n");
- }
- // list_print -- print the list in reverse order
- void
- list_rprint(list_t *list)
- {
- element_t *curptr;
- printf("list_rprint:");
- // search for pre-existing element with the same value
- for (curptr = list->tail; curptr != NULL; curptr = curptr->prev)
- printf(" %d",curptr->value);
- printf("n");
- }
- // main -- main program
- int
- main(void)
- {
- list_t mylist;
- element_t *e;
- // initialize list
- mylist.head = NULL;
- mylist.tail = NULL;
- // Add unique element to list
- e = new_element(42);
- list_insert_unique(&mylist,e);
- e = new_element(23);
- list_insert_unique(&mylist,e);
- e = new_element(17);
- list_insert_unique(&mylist,e);
- // try to insert a duplicate
- e = new_element(17);
- list_insert_unique(&mylist,e);
- // try to insert a duplicate
- e = new_element(42);
- list_insert_unique(&mylist,e);
- // print list in order [checks the head/next pointers]
- list_print(&mylist);
- // print list in reverse order [checks the tail/prev pointers]
- list_rprint(&mylist);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement