C++/공부 정리
연결리스트 탐색, 연결리스트로 구현한 큐(Queue)와 스택(Stack)
sondiaa
2021. 10. 22. 16:08
addNode로 추가하고 search로 요소를 탐색
#include <iostream>
using namespace std;
struct Node
{
int n;
Node *next;
};
Node *head, *last;
void addNode(int num)
{
if(head == NULL)
{
head = new Node();
head->n = num;
last = head;
return;
}
last->next = new Node();
last = last->next;
last->n = num;
}
void search()
{
Node *p = head;
while(p != NULL)
{
cout << p->n << ' ' ;
p = p->next;
}
}
int main()
{
addNode(1);
addNode(2);
addNode(3);
addNode(4);
search();
return 0;
}
A ~ Z까지 연결 후 탐색하기
#include <iostream>
using namespace std;
struct Node
{
char x;
Node *next;
};
Node *head, *tail;
void addNode(char alp)
{
if(head == NULL)
{
tail = head = new Node({alp, NULL});
return;
}
tail = tail->next = new Node({alp, NULL});
}
int main()
{
for(int i=0; i<26; i++)
{
addNode(65+i);
}
Node *p = head;
while(p != NULL)
{
cout << p->x << ' ';
p = p->next;
}
return 0;
}
숫자 입력 받고 해당 숫자 탐색
#include <iostream>
using namespace std;
struct Node
{
int x;
Node *next;
};
Node *head, *tail;
void addNode(int num)
{
if(head == NULL)
{
tail = head = new Node({num, NULL});
return;
}
tail = tail->next = new Node({num, NULL});
}
int search(int search_num)
{
Node *p = head;
int flag = 0, cnt = 0;
while(p != NULL)
{
if(p->x == search_num)
{
flag = 1;
cnt++;
}
p = p->next;
}
if(flag == 1)
cout << "존재";
else
cout << "존재 X";
return cnt;
}
int main()
{
addNode(3);
addNode(7);
addNode(9);
addNode(2);
addNode(4);
addNode(2);
addNode(2);
int count_num, num;
cin >> num;
count_num = search(num);
cout << count_num;
return 0;
}
char 배열 복사(string 사용하지 않은 경우)
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
ios::sync_with_stdio(false);
cin.tie(NULL);
char name1[10] = "smile";
char name2[10];
strcpy(name2, name1);
cout << name2;
return 0;
}
짝수 길이의 문자열만 연결리스트에 저장하고 탐색
#include <iostream>
#include <cstring>
using namespace std;
char word[5][7] = {"BTS", "BBQ", "BOBOBO", "BBCB", "BBBI"};
struct Node
{
char word[7];
Node *next;
};
Node *head, *tail;
void addNode(char *word1)
{
if(head == NULL)
{
head = new Node();
strcpy(head->word, word1);
tail = head;
return;
}
tail->next = new Node();
tail = tail->next;
strcpy(tail->word, word1);
}
int main()
{
for(int i=0; i<5; i++)
{
if(strlen(word[i]) % 2 == 0)
{
addNode(word[i]);
}
}
for(Node *p = head; p != NULL; p= p->next)
{
cout << p->word << ' ';
if(p->next == NULL) break;
}
return 0;
}
연결리스트로 큐 구현하기
#include <iostream>
using namespace std;
struct Node
{
int a;
Node *next;
};
Node *head, *tail;
void push(int num)
{
if(head == NULL)
{
tail = head = new Node({num, NULL});
return;
}
tail = tail->next = new Node({num, NULL});
}
int front()
{
return head->a;
}
void pop()
{
Node *temp = head->next;
delete head;
head = temp;
}
int main()
{
push(5);
push(3);
push(2);
cout << front();
pop();
cout << front();
pop();
push(7);
cout << front();
return 0;
}
값을 head에 삽입하기
#include <iostream>
using namespace std;
struct Node
{
int a;
Node *next;
};
Node *head;
void addNode(int num)
{
Node *temp;
temp = new Node({num, NULL});
temp->next = head;
head = temp;
}
int front()
{
return head->a;
}
void pop()
{
Node *temp = head->next;
delete head;
head = temp;
}
int main()
{
addNode(3);
addNode(7);
addNode(2);
cout << front();
pop();
cout << front();
pop();
addNode(7);
cout << front();
return 0;
}
연결리스트로 스택 구현
#include <iostream>
using namespace std;
struct Node
{
int a;
Node *next;
};
Node *head;
void addNode(int num)
{
head = new Node({num, head});
}
int top()
{
return head->a;
}
void pop()
{
Node * temp = head->next;
delete head;
head = temp;
}
int main()
{
addNode(1);
addNode(2);
cout << top();
addNode(3);
cout << top();
pop();
cout << top();
pop();
cout << top();
return 0;
}