U E D R , A S I H C RSS

Linked List/세연


DeleteMe ) 내용은 LinkedList 가 아니라 Stack의 구현 사항인데, 문제 사항에는 LinkedList라고 해놨네요.

~cpp 
#include <iostream.h> 

struct node{ 
	int data; 
	node * node_pointer; 
}; 

node * INSERT(node * head_pointer, int num); 

node * DELETE(node * head_pointer); 

int main() 
{ 
	//스택의 처음을 가르킨는 포인터 
	node * head_pointer = new node; 

	//처음을 가르키는 포인터를 초기화 
	head_pointer = NULL; 

	int num, choice; 

	//선택모드 
	cout << "스택을 만들어봐요!!\n"; 
	cout << "1.push 2.pop 3.exit\nchoice : "; 
	cin >> choice; 

	while(choice != 3) 
	{ 
		switch(choice) 
		{
		case 1://push 
			cout << "push num : "; 
			cin >> num; 
			head_pointer = INSERT(head_pointer, num); 
			break; 
		case 2://pop 
			head_pointer = DELETE(head_pointer); 
			break; 
		} 
		cout << "\n스택을 만들어봐요!!\n"; 
		cout << "1.push 2.pop 3.exit\nchoice : "; 
		cin >> choice; 
	} 

	return 0; 

} 

//스택에 자료를 넣는다 
node * INSERT(node * head_pointer, int num) 
{ 
	node * temp = new node; 

	//처음으로 스택에 자료를 넣을때 
	if(head_pointer == NULL) 
	{ 
		head_pointer = temp; 
		head_pointer->data = num; 
		head_pointer->node_pointer = NULL; 
	} 
	else 
	{ 
		temp->node_pointer = head_pointer; 
		head_pointer = temp; 
		head_pointer->data = num; 
	} 

	return head_pointer; 
} 

//스택에서 자료를 지운다 
node * DELETE(node * head_pointer) 
{ 
	node * temp = new node; 

	//스택에 자료가 없을 때 
	if(head_pointer == NULL) 
		cout << "no data in stack" << "\n"; 
	else 
	{ 
		cout << "delete num : " << head_pointer->data << "\n"; 
		temp = head_pointer; 
		head_pointer = head_pointer->node_pointer; 
		delete temp; 
	} 
	return head_pointer; 
}


Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:23:38
Processing time 0.0117 sec