Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #define HEAD list->head
- #define TAIL list->tail
- #define CURR list->curr
- #define SIZE list->size
- // DONE
- typedef struct Node
- {
- int data;
- // add *next and *prev here
- struct Node* next;
- struct Node* prev;
- } Node; // DONE
- // DONE
- typedef struct LinkedList
- {
- // add Node* head, tail, current_position and other necessary fields here
- Node* head;
- Node* tail;
- Node* curr;
- int size;
- } LinkedList;
- // DONE
- void init_linkedlist(LinkedList *list)
- {
- //implement init_linkedlist
- HEAD = NULL;
- TAIL = NULL;
- CURR = NULL;
- SIZE = 0;
- // initialize head, tail with null
- }
- // DONE
- void clear(LinkedList *list)
- {
- //Implement clear
- Node* temp1 = HEAD;
- while(temp1){
- Node* temp2 = temp1->next;
- free(temp1);
- temp1 = temp2;
- }
- HEAD = NULL;
- TAIL = NULL;
- CURR = NULL;
- SIZE = 0;
- // traverse the list and free each node
- // set head and tail to null
- }
- // DONE
- int get_size(LinkedList *list)
- {
- //Implement get_size
- return SIZE;
- }
- // DONE
- void append(LinkedList *list, int value)
- {
- //Implement append
- Node* temp = (Node*) malloc(sizeof(Node));
- temp->data = value;
- temp->next = NULL;
- SIZE++;
- if(HEAD == NULL){
- temp->prev = NULL;
- CURR = temp;
- HEAD = temp;
- TAIL = temp;
- return;
- }
- temp->prev = TAIL;
- TAIL->next = temp;
- TAIL = temp;
- // create a new node and set its value
- // consider the case when the list is empty and when it isnt
- }
- // DONE
- void insert(LinkedList *list, int value)
- {
- //Implement insert
- Node* temp = (Node*) malloc(sizeof(Node));
- temp->data = value;
- SIZE++;
- if(HEAD == NULL){
- HEAD = temp;
- TAIL = temp;
- CURR = temp;
- temp->next = NULL;
- temp->prev = NULL;
- return;
- }
- temp->next = CURR;
- if(CURR == HEAD){
- temp->prev = NULL;
- HEAD = temp;
- }
- else temp->prev = CURR->prev;
- CURR->prev = temp;
- CURR = temp;
- // create a new node and set its value
- // place it at the current position (check order of operations)
- // consider the case when the list is empty and when it isnt
- }
- //DONE
- int remove_at_current(LinkedList *list)
- {
- //Implement remove_at_current
- Node* temp = CURR;
- int value = temp->data;
- if(HEAD == TAIL) HEAD = NULL, TAIL = NULL, CURR = NULL;
- else if(CURR == HEAD){
- HEAD = CURR->next;
- HEAD->prev = NULL;
- CURR = HEAD;
- }
- else if(CURR == TAIL){
- TAIL = CURR->prev;
- TAIL->next = NULL;
- CURR = TAIL;
- }
- else{
- CURR->prev->next = CURR->next;
- CURR->next->prev = CURR->prev;
- CURR = CURR->next;
- }
- free(temp);
- SIZE--;
- // consider the case when current node is at the begining or at the end
- return value;
- }
- //DONE
- int find(LinkedList *list, int value)
- {
- //Implement find
- Node* temp = HEAD;
- int position = 0;
- while(temp && temp->data != value){
- temp = temp->next;
- position++;
- }
- // traverse the list and return the position of the value
- if(temp) return position;
- else return -1;
- }
- //DONE
- void move_to_start(LinkedList *list)
- {
- //Implement move_to_start
- CURR = HEAD;
- }
- //DONE
- void move_to_end(LinkedList *list)
- {
- //Implement move_to_end
- CURR = TAIL;
- }
- //DONE
- void prev(LinkedList *list)
- {
- //Implement prev
- if(CURR != HEAD) CURR = CURR->prev;
- }
- //DONE
- void next(LinkedList *list)
- {
- //Implement next
- if(CURR != TAIL) CURR = CURR->next;
- }
- void move_to_position(LinkedList *list, int position)
- {
- //Implement move_to_position
- Node* temp = HEAD;
- int i = 0;
- while(i != position){
- temp = temp->next;
- i++;
- }
- CURR = temp;
- // traverse the list and stop at the given position
- }
- //DONE
- int get_current_position(LinkedList *list)
- {
- //Implement get_current_position
- if(!CURR) return -1;
- Node* temp = HEAD;
- int position = 0;
- while(temp != CURR) temp = temp->next, position++;
- // traverse the list and stop when you are at the current position
- // return the position (integer)
- return position;
- }
- //DONE
- int get_current_element(LinkedList *list)
- {
- //Implement get_current_element
- if(!CURR) return -1;
- int value = CURR->data;
- // return the value at the current position
- return value;
- }
- //DONE
- void print_list(LinkedList *list)
- {
- //Print list elements
- printf("< ");
- Node* temp = HEAD;
- for(int i = 0; temp; i++){
- if(temp == CURR) printf("|");
- if(temp != TAIL) printf("%d ",temp->data);
- else printf("%d",temp->data);
- temp = temp->next;
- }
- printf(" >\n");
- }
- //DONE
- void free_list(LinkedList *list)
- {
- //Implement free_list
- clear(list);
- // free each node in the list
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement