現在双方向循環リストでrotateをする関数を書いてるのですが、-1である番兵ノードがどうしても出力されてしまい、番兵ノードを出力しない出力方法が思いつきません。
どなたかアドバイスを頂きたいです。
C
1#include <stdio.h> 2#include <stdlib.h> 3 4struct t_node { 5 struct t_node *next; 6 struct t_node *prev; 7 int data; 8}; 9struct t_node *first_node(int data) { 10 struct t_node *head; 11 head = malloc(sizeof(struct t_node *)); 12 head->next = head; 13 head->prev = head; 14 head->data = data; 15} 16void add_back(struct t_node *list, int data) { 17 struct t_node *node; 18 struct t_node *p = list; 19 node = malloc(sizeof(struct t_node *)); 20 if (node == NULL) 21 return; 22 while (p->next != list) { 23 p = p->next; 24 } 25 p->next->prev = node; 26 p->next = node; 27 node->prev = p; 28 node->data = data; 29 node->next = list; 30} 31void print(struct t_node *head) { 32 struct t_node *tmp; 33 if (head == NULL) 34 return; 35 tmp = head->next; 36 printf("tmp data is %d\n", tmp->data);//5 37 printf("head data is %d\n", head->data);//1 38 while (tmp != head) { 39 printf("%d ", tmp->data); 40 tmp = tmp->next; 41 } 42 printf("%d", tmp->data); 43 printf("\n"); 44} 45void swap(struct t_node *n1, struct t_node *n2) { 46 int tmp; 47 tmp = n1->data; 48 n1->data = n2->data; 49 n2->data = tmp; 50} 51void rotate(struct t_node *list_head) { 52 struct t_node *change_list_head_next; 53 struct t_node *head; 54 struct t_node *last; 55 first_node(0); 56 change_list_head_next = list_head->next; 57 swap(change_list_head_next, list_head); 58 head = list_head->next; 59 last = head->prev; 60 print(head->next); 61 return; 62} 63int main(void) { 64 struct t_node *list_head; 65 struct t_node *change_list_head_next; 66 list_head = NULL; 67 list_head = first_node(-1); 68 add_back(list_head, 3); 69 add_back(list_head, 1); 70 add_back(list_head, 5); 71 print(list_head); 72 rotate(list_head); 73}
出力結果
-1を消したい。
C
13 1 5 -1 25 3 -1 1
回答3件
あなたの回答
tips
プレビュー