Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Q
Quiz-3
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
1
Merge Requests
1
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
정용석
Quiz-3
Commits
11e1e033
Commit
11e1e033
authored
Feb 03, 2025
by
정용석
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
링크드리스트, 계산기 구현
parent
1b943dfb
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
550 additions
and
0 deletions
+550
-0
calculator.cpp
+156
-0
doubly_linked_list.cpp
+213
-0
singly_linked_list.c
+181
-0
No files found.
calculator.cpp
0 → 100644
View file @
11e1e033
#include <iostream>
#include <stack>
#include <cstdlib>
class
Calculator
{
public
:
std
::
string
infix_to_postfix
(
std
::
string
&
infix
);
int
run_calculate
(
std
::
string
&
input
);
};
std
::
string
Calculator
::
infix_to_postfix
(
std
::
string
&
infix
){
std
::
stack
<
std
::
string
>
sign
;
std
::
string
postfix
;
if
(
infix
[
0
]
==
'+'
||
infix
[
0
]
==
'-'
){
std
::
cout
<<
"parse error"
;
return
"err"
;
}
for
(
int
i
=
0
;
i
<
infix
.
size
();
i
++
){
if
(
infix
[
i
]
==
' '
){
continue
;
}
if
(
infix
[
i
]
==
'('
){
sign
.
push
(
std
::
string
(
1
,
infix
[
i
]));
}
else
if
(
infix
[
i
]
>=
'0'
&&
infix
[
i
]
<=
'9'
){
postfix
=
postfix
+
infix
[
i
];
if
(
infix
[
i
+
1
]
!=
'\0'
&&
infix
[
i
+
1
]
>=
'0'
&&
infix
[
i
+
1
]
<=
'9'
){
continue
;
}
else
{
postfix
=
postfix
+
" "
;
}
}
else
if
(
infix
[
i
]
==
')'
){
if
(
sign
.
empty
()){
std
::
cout
<<
"parse error"
;
return
"err"
;
}
while
(
sign
.
top
()
!=
"("
){
if
(
sign
.
empty
()){
std
::
cout
<<
"parse error"
;
return
"err"
;
}
postfix
=
postfix
+
sign
.
top
()
+
" "
;
sign
.
pop
();
}
sign
.
pop
();
}
else
{
// infix[i] == '+' || infix[i] == '-' || infix[i] == '*' || infix[i] == '/'
if
(
sign
.
empty
()){
sign
.
push
(
std
::
string
(
1
,
infix
[
i
]));
continue
;
}
if
(
infix
[
i
]
==
'*'
||
infix
[
i
]
==
'/'
){
if
(
sign
.
top
()
==
"*"
||
sign
.
top
()
==
"/"
){
postfix
=
postfix
+
sign
.
top
()
+
" "
;
sign
.
pop
();
sign
.
push
(
std
::
string
(
1
,
infix
[
i
]));
}
else
{
sign
.
push
(
std
::
string
(
1
,
infix
[
i
]));
}
}
else
if
(
infix
[
i
]
==
'+'
||
infix
[
i
]
==
'-'
){
if
(
sign
.
top
()
==
"*"
||
sign
.
top
()
==
"/"
||
sign
.
top
()
==
"+"
||
sign
.
top
()
==
"-"
){
postfix
=
postfix
+
sign
.
top
()
+
" "
;
sign
.
pop
();
sign
.
push
(
std
::
string
(
1
,
infix
[
i
]));
}
else
{
sign
.
push
(
std
::
string
(
1
,
infix
[
i
]));
}
}
}
}
while
(
!
sign
.
empty
()){
postfix
=
postfix
+
sign
.
top
()
+
" "
;
sign
.
pop
();
}
return
postfix
;
}
int
Calculator
::
run_calculate
(
std
::
string
&
infix
){
std
::
string
input
=
infix_to_postfix
(
infix
);
// std::cout << input << std::endl;
if
(
input
==
"err"
){
std
::
cout
<<
" 계산결과가 0으로 출력됩니다."
<<
std
::
endl
;
return
0
;
// 에러 발생시 계산결과 0
}
int
result
;
std
::
stack
<
int
>
numbers
;
for
(
int
i
=
0
;
i
<
input
.
size
();
i
++
){
if
(
input
[
i
]
==
' '
){
continue
;
}
if
(
input
[
i
]
>=
'0'
&&
input
[
i
]
<=
'9'
){
int
temp
=
0
;
while
(
input
[
i
]
>=
'0'
&&
input
[
i
]
<=
'9'
){
temp
=
10
*
temp
+
static_cast
<
int
>
(
input
[
i
])
-
'0'
;
i
++
;
}
numbers
.
push
(
temp
);
}
if
((
input
[
i
]
==
'+'
||
input
[
i
]
==
'-'
||
input
[
i
]
==
'*'
||
input
[
i
]
==
'/'
)
&&
numbers
.
size
()
<
2
){
std
::
cout
<<
"parse error 계산결과가 0으로 출력됩니다."
<<
std
::
endl
;
return
0
;
// 에러 발생시 계산결과 0
}
if
(
input
[
i
]
==
'+'
){
int
num2
=
numbers
.
top
();
numbers
.
pop
();
int
num1
=
numbers
.
top
();
numbers
.
pop
();
numbers
.
push
(
num1
+
num2
);
}
else
if
(
input
[
i
]
==
'-'
){
int
num2
=
numbers
.
top
();
numbers
.
pop
();
int
num1
=
numbers
.
top
();
numbers
.
pop
();
numbers
.
push
(
num1
-
num2
);
}
else
if
(
input
[
i
]
==
'*'
){
int
num2
=
numbers
.
top
();
numbers
.
pop
();
int
num1
=
numbers
.
top
();
numbers
.
pop
();
numbers
.
push
(
num1
*
num2
);
}
else
if
(
input
[
i
]
==
'/'
){
int
num2
=
numbers
.
top
();
numbers
.
pop
();
int
num1
=
numbers
.
top
();
numbers
.
pop
();
numbers
.
push
(
num1
/
num2
);
}
}
result
=
numbers
.
top
();
return
result
;
}
int
main
(){
Calculator
cal
;
std
::
string
t1
[]
=
{
"((2+ 13*5) / 2) * 3"
,
"21 +3"
,
"2 +3 * 5"
,
"2-4*(1+12)"
,
"+3+2"
,
"1 + -3"
,
};
int
test1
[
6
];
for
(
int
i
=
0
;
i
<
6
;
i
++
){
std
::
cout
<<
t1
[
i
]
<<
std
::
endl
;
test1
[
i
]
=
cal
.
run_calculate
(
t1
[
i
]);
std
::
cout
<<
test1
[
i
]
<<
std
::
endl
;
}
return
0
;
}
\ No newline at end of file
doubly_linked_list.cpp
0 → 100644
View file @
11e1e033
#ifndef DOUBLY_LINKED_LIST_CPP
#define DOUBLY_LINKED_LIST_CPP
#include "doubly_linked_list.h"
template
<
typename
T
>
T
DoublyLinkedList
<
T
>::
front
()
const
{
if
(
!
head
){
// head 가 NULL 이면 예외처리
throw
std
::
__throw_out_of_range
(
"리스트가 비어있습니다."
);
}
return
head
->
data
;
}
template
<
typename
T
>
T
DoublyLinkedList
<
T
>::
back
()
const
{
if
(
!
tail
){
throw
std
::
__throw_out_of_range
(
"리스트가 비어있습니다."
);
}
return
tail
->
data
;
}
template
<
typename
T
>
size_t
DoublyLinkedList
<
T
>::
size
()
const
{
return
node_size
;
}
template
<
typename
T
>
void
DoublyLinkedList
<
T
>::
push_front
(
T
value
){
Node
<
T
>*
newNode
=
new
Node
<
T
>
(
value
);
if
(
head
){
newNode
->
next
=
head
;
head
->
prev
=
newNode
;
}
else
{
// 비어있었다면
tail
=
newNode
;
}
head
=
newNode
;
node_size
++
;
}
template
<
typename
T
>
void
DoublyLinkedList
<
T
>::
push_back
(
T
value
){
Node
<
T
>*
newNode
=
new
Node
<
T
>
(
value
);
if
(
tail
){
newNode
->
prev
=
tail
;
tail
->
next
=
newNode
;
}
else
{
head
=
newNode
;
}
tail
=
newNode
;
node_size
++
;
}
template
<
typename
T
>
void
DoublyLinkedList
<
T
>::
pop_front
(){
if
(
!
head
){
throw
std
::
out_of_range
(
"리스트가 비어있습니다."
);
}
Node
<
T
>*
temp
=
head
;
head
=
head
->
next
;
if
(
head
){
head
->
prev
=
NULL
;
}
else
{
tail
=
NULL
;
}
node_size
--
;
delete
temp
;
}
template
<
typename
T
>
void
DoublyLinkedList
<
T
>::
pop_back
(){
if
(
!
head
){
throw
std
::
out_of_range
(
"리스트가 비어있습니다."
);
}
Node
<
T
>*
temp
=
tail
;
tail
=
tail
->
prev
;
if
(
tail
){
tail
->
next
=
NULL
;
}
else
{
tail
=
NULL
;
}
node_size
--
;
delete
temp
;
}
template
<
typename
T
>
void
DoublyLinkedList
<
T
>::
insert
(
T
value
,
size_t
position
){
if
(
position
==
0
){
push_front
(
value
);
}
else
if
(
position
==
node_size
){
push_back
(
value
);
}
else
if
(
position
>
0
&&
position
<
node_size
){
Node
<
T
>*
newNode
=
new
Node
<
T
>
(
value
);
Node
<
T
>*
temp
=
head
;
Node
<
T
>*
temp_prev
=
temp
;
for
(
int
i
=
0
;
i
<
position
;
i
++
){
temp_prev
=
temp
;
temp
=
temp
->
next
;
}
temp_prev
->
next
=
newNode
;
newNode
->
prev
=
temp_prev
;
newNode
->
next
=
temp
;
temp
->
prev
=
newNode
;
}
else
{
throw
std
::
out_of_range
(
"잘못된 위치에 insert하려 합니다."
);
return
;
}
node_size
++
;
}
template
<
typename
T
>
void
DoublyLinkedList
<
T
>::
remove
(
T
value
){
if
(
node_size
==
0
){
std
::
cout
<<
"리스트가 비어있습니다."
<<
std
::
endl
;
return
;
}
// 값이 head에 있을 때
if
(
head
->
data
==
value
){
Node
<
T
>*
temp
=
head
;
head
=
head
->
next
;
if
(
head
){
head
->
prev
=
nullptr
;
}
else
{
tail
=
nullptr
;
}
delete
temp
;
node_size
--
;
return
;
}
// 값이 head에 없을 때
Node
<
T
>*
current
=
head
;
while
(
current
!=
NULL
){
if
(
current
->
data
==
value
){
Node
<
T
>*
prevNode
=
current
->
prev
;
Node
<
T
>*
nextNode
=
current
->
next
;
if
(
prevNode
){
prevNode
->
next
=
nextNode
;
}
if
(
nextNode
){
nextNode
->
prev
=
prevNode
;
}
else
{
tail
=
prevNode
;
}
delete
current
;
node_size
--
;
return
;
}
current
=
current
->
next
;
}
std
::
cout
<<
"값 "
<<
value
<<
"을(를) 찾을 수 없습니다.
\n
"
;
}
template
<
typename
T
>
void
DoublyLinkedList
<
T
>::
clear
(){
while
(
head
!=
NULL
){
pop_front
();
}
}
template
<
typename
T
>
bool
DoublyLinkedList
<
T
>::
empty
()
const
{
if
(
node_size
==
0
){
return
true
;
}
else
{
return
false
;
}
}
template
<
typename
T
>
void
DoublyLinkedList
<
T
>::
print
()
const
{
Node
<
T
>*
temp
=
head
;
if
(
temp
!=
NULL
){
std
::
cout
<<
"NULL <-> "
;
}
while
(
temp
!=
NULL
){
std
::
cout
<<
temp
->
data
<<
" <-> "
;
temp
=
temp
->
next
;
}
std
::
cout
<<
"NULL"
<<
std
::
endl
;
}
int
main
(){
DoublyLinkedList
<
int
>
dll1
;
dll1
.
push_back
(
3
);
dll1
.
push_front
(
2
);
dll1
.
push_back
(
5
);
dll1
.
push_front
(
1
);
dll1
.
print
();
// 출력결과
// NULL <-> 1 <-> 2 <-> 3 <-> 5 <-> NULL
dll1
.
insert
(
4
,
3
);
dll1
.
print
();
// 출력결과
// NULL <-> 1 <-> 2 <-> 3 <-> 4 <-> 5 <-> NULL
dll1
.
remove
(
4
);
dll1
.
print
();
dll1
.
remove
(
4
);
// 출력결과
// NULL <-> 1 <-> 2 <-> 3 <-> 5 <-> NULL
// 값 4을(를) 찾을 수 없습니다.
dll1
.
clear
();
dll1
.
print
();
// 출력결과
// NULL
return
0
;
}
#endif
\ No newline at end of file
singly_linked_list.c
0 → 100644
View file @
11e1e033
#include <stdio.h>
#include <stdlib.h>
typedef
struct
Node
{
int
data
;
struct
Node
*
next
;
}
Node
;
Node
*
createNode
(
int
data
){
Node
*
head
=
(
Node
*
)
malloc
(
sizeof
(
Node
));
if
(
!
head
){
printf
(
"노드 생성 오류
\n
"
);
exit
(
1
);
}
head
->
data
=
data
;
head
->
next
=
NULL
;
return
head
;
}
void
appendNode
(
Node
**
head
,
int
data
){
Node
*
newNode
=
createNode
(
data
);
if
(
*
head
==
NULL
){
// 빈 LL 였을 경우
*
head
=
newNode
;
return
;
}
Node
*
temp
=
*
head
;
while
(
temp
->
next
!=
NULL
){
// 마지막 위치 찾기
temp
=
temp
->
next
;
}
temp
->
next
=
newNode
;
}
void
insertNode
(
Node
**
head
,
int
data
,
int
position
){
Node
*
newNode
=
createNode
(
data
);
if
(
position
==
0
){
newNode
->
next
=
*
head
;
*
head
=
newNode
;
return
;
}
Node
*
temp
=
*
head
;
for
(
int
i
=
0
;
i
<
position
-
1
&&
temp
!=
NULL
;
i
++
){
temp
=
temp
->
next
;
}
if
(
temp
==
NULL
){
printf
(
"insertNode의 위치가 범위를 넘어갑니다.
\n
"
);
free
(
newNode
);
return
;
}
newNode
->
next
=
temp
->
next
;
temp
->
next
=
newNode
;
}
int
size
(
Node
*
head
){
int
count
=
0
;
if
(
head
==
NULL
){
// 빈 LL 였을 경우
printf
(
"빈 노드입니다.
\n
"
);
return
count
;
}
Node
*
temp
=
head
;
while
(
temp
->
next
!=
NULL
){
// 마지막 위치 찾기
temp
=
temp
->
next
;
count
++
;
}
count
++
;
return
count
;
}
// data값을 갖는 첫 번째 노드를 제거
void
deleteNode
(
Node
**
head
,
int
data
){
if
(
*
head
==
NULL
){
printf
(
"빈 노드입니다.
\n
"
);
return
;
}
Node
*
temp
=
*
head
;
if
(
temp
->
data
==
data
){
*
head
=
temp
->
next
;
free
(
temp
);
return
;
}
Node
*
prev
=
NULL
;
while
(
temp
->
next
!=
NULL
&&
temp
->
data
!=
data
){
prev
=
temp
;
temp
=
temp
->
next
;
}
if
(
temp
==
NULL
){
printf
(
"삭제할 Data가 없습니다.
\n
"
);
}
prev
->
next
=
temp
->
next
;
free
(
temp
);
return
;
}
void
printList
(
Node
*
head
){
Node
*
temp
=
head
;
while
(
temp
!=
NULL
){
printf
(
"%d -> "
,
temp
->
data
);
temp
=
temp
->
next
;
}
printf
(
"NULL
\n
"
);
}
Node
*
searchNode
(
Node
*
head
,
int
data
){
if
(
head
==
NULL
){
printf
(
"빈 노드입니다.
\n
"
);
}
Node
*
temp
=
head
;
while
(
temp
!=
NULL
){
if
(
temp
->
data
==
data
){
return
temp
;
}
temp
=
temp
->
next
;
}
return
NULL
;
}
void
freeList
(
Node
**
head
){
Node
*
temp
;
while
(
*
head
!=
NULL
){
temp
=
*
head
;
*
head
=
(
*
head
)
->
next
;
free
(
temp
);
}
}
int
main
(
void
){
Node
*
node1
=
createNode
(
1
);
appendNode
(
&
node1
,
2
);
appendNode
(
&
node1
,
4
);
appendNode
(
&
node1
,
5
);
printf
(
"size : %d
\n
"
,
size
(
node1
));
printList
(
node1
);
// 출력결과
// size : 4
// 1 -> 2 -> 4 -> 5 -> NULL
insertNode
(
&
node1
,
3
,
2
);
printf
(
"size : %d
\n
"
,
size
(
node1
));
printList
(
node1
);
// 출력결과
// size : 5
// 1 -> 2 -> 3 -> 4 -> 5 -> NULL
int
searchValue1
=
4
;
Node
*
search1
=
searchNode
(
node1
,
searchValue1
);
if
(
search1
!=
NULL
){
printf
(
"%d 값이 리스트에 존재합니다.
\n
"
,
searchValue1
);
}
else
{
printf
(
"%d 값이 리스트에 존재하지 않습니다.
\n
"
,
searchValue1
);
}
// 출력결과
// 4 값이 리스트에 존재합니다.
deleteNode
(
&
node1
,
searchValue1
);
printf
(
"%d 삭제
\n
"
,
searchValue1
);
printList
(
node1
);
// 출력결과
// 4 삭제
// 1 -> 2 -> 3 -> 5 -> NULL
search1
=
searchNode
(
node1
,
searchValue1
);
if
(
search1
!=
NULL
){
printf
(
"%d 값이 리스트에 존재합니다.
\n
"
,
searchValue1
);
}
else
{
printf
(
"%d 값이 리스트에 존재하지 않습니다.
\n
"
,
searchValue1
);
}
// 출력결과
// 4 값이 리스트에 존재하지 않습니다.
freeList
(
&
node1
);
printf
(
"리스트 제거
\n
"
);
printList
(
node1
);
// 출력결과
// 리스트 제거
// NULL
return
0
;
}
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment