티스토리 뷰

DLL데이터검색

#include <stdio.h>
#include <stdlib.h>
struct node {
	int data;
	struct node *next;
	struct node *prev;
};
struct node* head = 0;

void addToDLL(int n)
{
	struct node* cur;
	cur = (struct node*)malloc(sizeof(struct node));
	cur->data = n;
	cur->next = 0;
	cur->prev = 0;

	if (head == 0) {
		head = cur;
		return;
	}
	{ //끝을 찾는다
		struct node* temp = head;
		while (temp->next != 0) {
			temp = temp->next;
		}
		temp->next = cur;
		cur->prev = temp;
	}
}


void findFB(int n)
{
	struct node* cur = head;

	if (head == 0) {
		printf("-999");
		return;
	}
	while (1) { //검색
		if (cur == 0) {
			if (head == 0) { //검색을못했을땐 break dll이없을땐 return
				return;
			}
			printf("-999");
			return;
		}
		if (cur->data == n) {
			break;
		}
		cur = cur->next;
	}

	if (cur->prev == 0) {
		printf("-999 ");

	}
	else {
		printf("%d ", cur->prev->data);
	}


	if (cur->next == 0) {
		printf("-999 ");

	}
	else {
		printf("%d ", cur->next->data);
	}
	return;

}


void showDLL()
{
	struct node* cur = head;
	if (head == 0) {
		return;
	}
	while (cur != 0) {
		printf("%d ", cur->data);
		cur = cur->next;
	}
	return;
}

int main(void)
{
	int num, data1, data2, i;
	
	scanf("%d", &num);
	for (i = 0; i < num; i++) {
		scanf("%d", &data1);
		addToDLL(data1);
	}
	scanf("%d", &data1);
	findFB(data1);

}
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31