/*
#include <stdio.h>
char a[201] = {0};
int stack[201] = {0};
int t = -1;
int counter = 0;
void push(){
int p = a[6] - '0';
t++;
if(a[7] != 32){
for(int i = 7; a[i] != 32; i++){
int q = a[i] - '0';
p = 10 * p + q;
}
}
stack[t] = p;
counter++;
return;
}
void top(){
if(counter == 0){
printf("-1\n");
return;
}
printf("%d\n", stack[t]);
return;
}
void pop(){
if(t == -1){
return;
}
stack[t] = 0;
t--;
counter--;
return;
}
void size(){
printf("%d\n", counter);
return;
}
void empty(){
if(counter == 0){
printf("true\n");
return;
}
printf("false\n");
return;
}
int main(){
int N;
scanf("%d\n", &N);
for(int i = 0; i < N; i++){
gets(a);
if(a[0]=='p'){
if(a[1]=='u'){
push();
}
else pop();
}
else if(a[0]=='t') top();
else if(a[0]=='s') size();
else empty();
}
return 0;
}
*/
/*
#include <stdio.h>
int front = 0;
int rear = 0;
int stack[5] = {0};
void push(int p){
front++;
if(front > 5 && rear == 0){
printf("Stack is full (Front: 5, Rear: 0)\n\n");
front--;
return;
}
if(front > 5 && rear > 0){
front--;
for (int i = 0; i + rear < 5; i++){
stack[i] = stack[i + rear];
}
for (int i = rear + 1; i < 5; i++){
stack[i] = 0;
}
front -= rear;
rear = 0;
stack[front++] = p;
printf("Stack is revised (Front: %d, Rear: 0)\n\n", front);
return;
}
printf("Front: %d, Rear: %d\n\n", front, rear);
stack[front-1] = p;
return;
}
void pop(){
if(front == 0 && rear == 0){
printf("Stack is empty(Front: 0, Rear: 0)\n\n");
return;
}
stack[rear++] = 0;
if(front == rear){
printf("Stack is emptied(Front: %d -> 0, Rear: %d -> 0)\n\n", front, rear);
front = 0;
rear = 0;
return;
}
printf("Front: %d, Rear: %d\n\n", front, rear);
return;
}
void view(){
for(int i=4;i>-1;i--){
printf("%d 번째 칸: %d\n", i, stack[i]);
}
printf("Front: %d, Rear: %d\n\n", front, rear);
return;
}
int main(){
int type;
int push_input;
printf("type 1: push()\ntype 2: pop()\ntype 3: view()\n");
for(;;){
scanf("%d", &type);
if(type == 1){
printf("[PUSH]\n");
printf("값을 입력하세요: ");
scanf("%d", &push_input);
push(push_input);
}
if(type == 2){
printf("[POP]\n");
pop();
}
if(type == 3){
printf("[VIEW]\n");
view();
}
}
}
*/
/*
#include <stdio.h>
int front = 0;
int rear = 0;
int stack[5] = {0};
void push(int p){
if(front - rear == 4 || front - rear == -1){
front++;
printf("Stack is full (Front: %d, Rear: %d)\n\n", front, rear);
front--;
return;
}
if(front > 4) {
front = 0;
stack[0] = p;
front++;
printf("Front: %d, Rear: %d\n\n", front, rear);
return;
}
front++;
printf("Front: %d, Rear: %d\n\n", front, rear);
stack[front-1] = p;
return;
}
void pop(){
if(front == rear){
printf("Stack is empty(Front: %d, Rear: %d)\n\n", front, rear);
return;
}
rear++;
if(rear == 5){
stack[rear-1] = 0;
rear = 0;
printf("Front: %d, Rear: %d\n\n", front, rear);
return;
}
stack[rear-1] = 0;
printf("Front: %d, Rear: %d\n\n", front, rear);
return;
}
void view(){
for(int i=4;i>-1;i--){
printf("%d 번째 칸: %d\n", i, stack[i]);
}
printf("Front: %d, Rear: %d\n\n", front, rear);
return;
}
int main(){
int type;
int push_input;
printf("type 1: push()\ntype 2: pop()\ntype 3: view()\n");
for(;;){
scanf("%d", &type);
if(type == 1){
printf("[PUSH]\n");
printf("값을 입력하세요: ");
scanf("%d", &push_input);
push(push_input);
}
if(type == 2){
printf("[POP]\n");
pop();
}
if(type == 3){
printf("[VIEW]\n");
view();
}
}
}
*/
/*
#include <stdio.h>
int front = 0;
int rear = 0;
int f = 0;
int stack[5] = {0};
void push(int p){
if(f == 5){
printf("Stack is full\n");
printf("Front: %d, Rear: %d\n\n", front, rear);
return;
}
if (front == 5){
front = 1;
stack[0] = p;
f++;
printf("Front: %d, Rear: %d\n\n", front, rear);
return;
}
stack[front++] = p;
f++;
printf("Front: %d, Rear: %d\n\n", front, rear);
return;
}
void pop(){
if(f == 0){
printf("Stack is empty(Front: %d, Rear: %d)\n\n", front, rear);
return;
}
if(rear == 5){
rear = 1;
stack[0] = 0;
f--;
printf("Front: %d, Rear: %d\n\n", front, rear);
return;
}
stack[rear++] = 0;
f--;
printf("Front: %d, Rear: %d\n\n", front, rear);
return;
}
void view(){
for(int i=4;i>-1;i--){
printf("%d 번째 칸: %d\n", i, stack[i]);
}
printf("Front: %d, Rear: %d\n\n", front, rear);
return;
}
int main(){
int type;
int push_input;
printf("type 1: push()\ntype 2: pop()\ntype 3: view()\n");
for(;;){
scanf("%d", &type);
if(type == 1){
printf("[PUSH]\n");
printf("값을 입력하세요: ");
scanf("%d", &push_input);
push(push_input);
}
if(type == 2){
printf("[POP]\n");
pop();
}
if(type == 3){
printf("[VIEW]\n");
view();
}
}
}
//노드 추가 (.)
Node myNode = NULL;
myNode = (Node)malloc(sizeof(Node));
myNode.data = 10;
// 이후 노드 추가
newNode = (Node*)malloc(sizeof(Node));
newNode->data = 100;
newNode->next = NULL;
cur = head;
while(cur->next!=NULL)
{
cur = cur->next;
}
cur->next = newNode;
view(head);
*/
//Linked List
#include <stdio.h>
#include <stdlib.h>
typedef struct _node{
int data;
struct _node * next;
}Node;
int main()
{
Node *head = NULL;
Node *tail = NULL;
Node *cur = NULL;
Node *newNode = NULL;
int newData;
ListInit();
//최초 노드 추가 ( -> )
while(1)
{
scanf("%d", &newData);
if(newData < 1) {
break;
}
newNode = (Node*)malloc(sizeof(Node));
newNode->data = newData;
newNode->next = NULL;
if (head == NULL)
head = newNode;
else
tail->next = newNode;
tail = newNode;
}
printf("\n입력 받은 데이터의 전체 출력!\n");
if(head == NULL){
printf("저장된 값이 존재하지 않습니다. \n");
}
else{
cur = head;
printf("Linked List : ");
printf("%d -> ", cur->data);
while(cur->next != NULL){
cur = cur->next;
printf("%d -> ", cur->data);
}
}
printf("\n\n");
if(head == NULL){
return 0;
}
else{
Node * delNode = head;
Node * delNextNode = head -> next;
printf("%d 삭제됨 \n", head->data);
free(delNode);
while(delNextNode != NULL){
delNode = delNextNode;
delNextNode = delNextNode->next;
printf("%d 삭제됨 \n", delNode->data);
free(delNode);
}
}
return 0;
}
#include <stdio.h>
#include <stdlib.h>
typedef struct _node{
int data;
struct _node * next;
}Node;
Node * head = NULL;
Node * tail = NULL;
Node * cur = NULL;
Node * newN = NULL;
int newD;
void printNode(Node * newN){
if(head == NULL){
printf("입력된 데이터가 없음\n");
return;
}
else{
cur = head;
printf("Linked List : %d -> ", cur->data);
while(cur->next!=NULL){
cur = cur->next;
printf("%d -> ", cur->data);
}
}
printf("\n\n");
return;
}
void deleteNode(Node * newN){
if(head == NULL){
printf("입력된 데이터 없음\n");
return;
}
else{
int deD;
Node * deN = NULL;
printf("삭제할 값 입력: ");
scanf("%d", &deD);
cur = head;
if(cur->data == deD){
deN = cur;
cur = cur->next;
head = cur;
free(deN);
printNode(newN);
return;
}
while(cur->next->data != deD){
cur = cur->next;
if(cur->next == NULL){
printf("일치하는 값이 없습니다.\n");
return 0;
}
}
deN = cur->next;
printf("\n%d 삭제됨 \n", cur->next->data);
cur->next = cur->next->next;
free(deN);
printNode(newN);
return;
}
}
void searchNode(Node * newN){
if(head == NULL){
printf("입력된 값 없음\n");
return;
}
else{
int seD;
int k = 1;
printf("검색할 값 입력: ");
scanf("%d", &seD);
cur = head;
while(cur->data != seD){
cur = cur->next;
k++;
if(cur == NULL){
printf("일치하는 데이터가 없습니다.\n");
return 0;
}
}
printf("\n데이터(%d)는 %d 번째 노드에 있습니다. \n", seD, k);
return;
}
}
void insertNode(Node * newN){
int inD, inno, k = 0;
Node * inN = NULL;
cur = head;
while(cur->data != NULL){
cur = cur->next;
k++;
}
printf("입력할 값(1보다 작은 값 입력 시 종료): ");
scanf("%d", &inD);
printf("\n입력할 값의 위치: ");
scanf("%d", &inno);
cur = head;
if(inN < 1){
printf("1보다 작은 값을 입력할 수 없음. 입력 종료\n\n");
printNode(newN);
return;
}
if(inno > k){
printf("적합하지 않은 위치. 입력 종료\n\n");
printNode(newN);
return;
}
if(head == NULL && inno == 1){
newN->data = inD;
newN->next = NULL;
printNode(newN);
return;
}
if(inno == 1){
}
for(i = 1; i < inno; i++){
cur = cur->next;
}
inN->data = inD;
cur
printNode(newN);
return;
}
int main(){
//노드 생성
printf("\n초기 노드 값 입력 (값이 1보다 작으면 작성 종료) : \n");
while(1){
scanf("%d", &newD);
if(newD < 1){
break;
}
newN = (Node*)malloc(sizeof(Node));
newN->data = newD;
newN->next = NULL;
if(head==NULL){
head = newN;
}
else{
tail->next = newN;
}
tail = newN;
}
printNode(newN);
while(1){
int tyno;
printf("\n\ntype 1 : View All Data\ntype 2 : Search Data Location\ntype 3 : Insert Data\ntype 4 : Delete Data\ntype 0 : Stop\n\nWrite down type # :");
scanf("%d", &tyno);
printf("\n\n");
if(tyno == 0){
break;
}
if(tyno == 1){
printNode(newN);
}
if(tyno == 2){
searchNode(newN);
}
if(tyno == 3){
insertNode(newN);
printf("3\n");
}
if(tyno == 4){
deleteNode(newN);
}
}
return 0;
}