top of page

게시판 게시물

ksw0633
2019년 10월 05일
In 소스 코드 제출
/* #include <stdio.h> #include <string.h> #include <windows.h> #include <math.h> #define MAX 1000 int numstack[1000]; int stack[1000]; char charstack[1000]; char equation[1000]; int numtop=0; int chartop=0; int top=0; void convert(); void calculator(); void convert() { int i; for(i=0;i<strlen(equation);i++) { //printf("------------------------------------------\n"); if(equation[i]>=48&&equation[i]<=57) { int temp=0; while(equation[i]>=48&&equation[i]<=57) { stack[top++]=equation[i]-'0'; i++; } int cnt=0; while(top>0) { temp+=(stack[top-1])*pow(10.0, cnt); top--; } numstack[numtop++]=temp; } if(equation[i]<48||equation[i]>57) { if(equation[i]==')'||equation[i]=='}'||equation[i]==']') calculator(); else charstack[chartop++]=equation[i]; } } calculator(); } void calculator()//괄호 계산은 1번 , 그냥 계산은 0번 { while(chartop>0) { int x, y; int temp; x=numstack[numtop-2]; y=numstack[numtop-1]; if(charstack[chartop-1]=='('||charstack[chartop-1]=='{'||charstack[chartop-1]=='[') { chartop--; return; } if(charstack[chartop-1]=='+') { temp=x+y; numtop=numtop-2; numstack[numtop++]=temp; } else if(charstack[chartop-1]=='-') { temp=x-y; numtop=numtop-2; numstack[numtop++]=temp; } else if(charstack[chartop-1]=='*') { temp=x*y; numtop=numtop-2; numstack[numtop++]=temp; } else if(charstack[chartop-1]=='/') { temp=x/y; numtop=numtop-2; numstack[numtop++]=temp; } chartop--; } } int main() { scanf("%s", equation); // printf("%s\n", equation); convert(); printf("%d\n", numstack[numtop-1]); return 0; } */ /* #include <stdio.h> #include <stdlib.h> #include <windows.h> #include <string.h> typedef struct node { int data; node *rlink; node *llink; }node; node* queue[1000]; int front=0; int rear=0; int heap[]; node *start; node *current; node *before; //int nodes=1; void MAXheap(); void deletemax(); void menu(); void print(); void MAXheap() { printf("---------------------------------------------------\n"); int num; printf("please enter a integer you want to insert.\n"); scanf("%d", &num); while(num<=0) { printf("please enter a integer.\n"); scanf("%d", &num); } if(start->data==0) { start->data=num; printf("insert complete.\n"); printf("---------------------------------------------------\n"); return; } node *newnode; newnode=(node*)malloc(sizeof(node)); //newnode->index=nodes++; newnode->llink=NULL; newnode->rlink=NULL; newnode->data=num; if(current->llink==NULL) current->llink=newnode; else current->rlink=newnode; //비교 if(current->data<newnode->data) { int temp; temp=current->data; current->data=newnode->data; newnode->data=temp; } before=current; current=newnode; printf("insert complete.\n"); printf("---------------------------------------------------\n"); } void deletemax() { printf("---------------------------------------------------\n"); if(start->data==0) { printf("the heap is empty\n"); return; } else if(start->llink==NULL) { printf("delete complete\n"); start->data=0; return; } node *temp; temp=start; start->data=current->data; free(current); current=before; while(1) { if(temp->llink==NULL||(temp->llink->data>=temp->data&&(temp->rlink->data>=temp->data||temp->rlink==NULL))) break; else { int t; if(temp->llink->data<temp->data) { t=temp->llink->data; temp->llink->data=temp->data; temp->data=t; } else { t=temp->rlink->data; temp->rlink->data=temp->data; temp->data=t; } } } printf("delete complete.\n"); printf("---------------------------------------------------\n"); } void menu() { printf("---------------------------------------------------\n"); printf("Welcome to heap management\n"); int m=0; start = (node*)malloc(sizeof(node)); //start->index=nodes++; start->rlink=NULL; start->llink=NULL; start->data=0; current=start; while(m!=4) { printf("press 1 to insert, 2 to delete the max node,3 to print, 4 to quit.\n"); scanf("%d", &m); if(m==1) MAXheap(); else if(m==2) deletemax(); else if(m==3) print(); else if(m==4) { printf("BYE."); return; } else printf("Please enter a number between 1 and 3.\n"); printf("---------------------------------------------------\n"); } } void print() { if(start->data==0) { printf("the heap is empty.\n"); printf("---------------------------------------------------\n"); return; } queue[rear++]=start; node *temp; while(1) { int i; for(i=front;i<rear;i++) { printf("%d ", queue[i]->data); if(queue[i]==current) { printf("\n"); front=0; rear=0; printf("---------------------------------------------------\n"); return; } } printf("\n"); int t=rear; for(i=front;i<t;i++) { if(queue[i]->llink!=NULL) queue[rear++]=queue[i]->llink; if(queue[i]->rlink!=NULL) queue[rear++]=queue[i]->rlink; } front=t; } } int main() { menu(); return 0; } */ /* #include<stdio.h> #include<string.h> #include<malloc.h> #include<windows.h> void addheap(); void deleteheap(); void print(); void menu(); int n=1; int heap[10000]; void addheap() { printf("--------------------------------------------------------------\n"); if(n>=10000) { printf("the heap is full.\n"); printf("--------------------------------------------------------------\n"); return; } int num; printf("please enter an integer.\n"); scanf("%d", &num); while(num<=0) { printf("please enter an integer.\n"); scanf("%d", &num); } heap[n++]=num; //printf("num=%d\n", num); // for(int k=0;k<n;k++) //printf("%d ", heap[k]); // printf("\n"); int i; i=(n-1)/2; int j; j=(n-1); //printf("%d %d %d %d %d\n",n, i, j, heap[i], heap[j]); while(heap[i]<heap[j]&&i>0) { //printf("debug\n"); int temp; temp=heap[i]; heap[i]=heap[j]; heap[j]=temp; j=i; i=i/2; } printf("insert complete.\n"); printf("--------------------------------------------------------------\n"); } void deleteheap() { printf("--------------------------------------------------------------\n"); if(n==1) { printf("the heap is empty.\n"); printf("--------------------------------------------------------------\n"); return; } heap[1]=heap[n-1]; n--; int i=1; while(1) { if(i*2>=n) break; else if(i*2+1>=n) break; if(heap[i]>heap[i*2]&&heap[i]>heap[i*2+1]) break; int temp; if(heap[i]<heap[i*2]) { temp=heap[i]; heap[i]=heap[i*2]; heap[i*2]=temp; i=i*2; } else if(heap[i]<heap[i*2+1]) { temp=heap[i]; heap[i]=heap[i*2+1]; heap[i*2+1]=temp; i=i*2+1; } } printf("delete complete.\n"); printf("--------------------------------------------------------------\n"); } void print() { printf("--------------------------------------------------------------\n"); if(n==1) { printf("the heap is empty.\n"); printf("--------------------------------------------------------------\n"); return; } int a=1; int b=1; int c; int i; printf("%d\n", heap[1]); while(a<n) { c=a+b*2; if(a+b*2>=n) c=n-1; for(i=a+1;i<=c;i++) printf("%d ", heap[i]); printf("\n"); a=a+b*2; b++; } printf("--------------------------------------------------------------\n"); } void menu() { int num=0; printf("--------------------------------------------------------------\n"); printf("welcome to heap management.\n"); while(num!=4) { printf("enter 1 to insert, 2 to delete the max node, 3 to print, 4 to quit.\n"); scanf("%d", &num); if(num==1) addheap(); else if(num==2) deleteheap(); else if(num==3) print(); else if(num==4) { printf("BYE.\n"); printf("--------------------------------------------------------------\n"); return; } else printf("please enter a number between 1 to 4.\n"); printf("--------------------------------------------------------------\n"); } } int main() { menu(); return 0; } */ /* #include <stdio.h> int map[200][200]; int visit[200][200]; int bfscheck[200][200]; int rain; int n; int max; int min=101; int bfscnt=1; void bfs(int x, int y, int danger); int findsafe(); void bfs(int x, int y, int danger) { //printf("debug\n"); /*if(danger==2) { printf("x=%d y=%d bfscnt=%d\n", x, y, bfscnt); bfscheck[x][y]=bfscnt; }*/ /* visit[x][y]=danger; if(visit[x][y-1]!=danger&&map[x][y-1]>danger&&y-1>=0) bfs(x, y-1, danger); if(visit[x][y+1]!=danger&&map[x][y+1]>danger&&y+1<n) bfs(x, y+1, danger); if(visit[x-1][y]!=danger&&map[x-1][y]>danger&&x-1>=0) bfs(x-1, y, danger); if(visit[x+1][y]!=danger&&map[x+1][y]>danger&&x+1<n) bfs(x+1, y, danger); }*/ /* int findsafe() { int i,j; int cnt=min; int safe=0; while(cnt<=max) { int safecnt=0; for(i=0;i<n;i++) { for(j=0;j<n;j++) { if(visit[i][j]==cnt); else { if(map[i][j]>cnt) { bfs(i, j, cnt); if(cnt==2) bfscnt++; safecnt++; } else visit[i][j]=cnt; } } } if(safecnt>safe) safe=safecnt; /* if(safecnt==2) printf("%d\n", cnt);*/ /* cnt++; } return safe; } int main() { int i, j; scanf("%d", &n); for(i=0;i<n;i++) { for(j=0;j<n;j++) { scanf("%d", &map[i][j]); if(map[i][j]<min) min=map[i][j]; if(map[i][j]>max) max=map[i][j]; } } int ans = findsafe(); if(ans==0) ans=1; printf("%d", ans); return 0; } */ /* #include <stdio.h> int graph[10010][10010];//x, y, 가중치 int n; int m; int main() { int i, j, a, b, c; scanf("%d %d", &n, &m); for(i=0;i<m;i++) { scanf("%d %d %d", &a, &b, &c); graph[a][b]=c; } for(i=0;i<9;i++) { printf("%d\n", arr[i]); } return 0; } */ /* #include <stdio.h> int graph[10010][5];//0=가중치, 1=x, 2=y int visit[10010]; int n; int m; int cnt; int partition(int arr[10010][5],int left, int right) { int pivot=arr[left][0]; int high, low; low = left+1; high=right; while(low<=high) { while(arr[low][0]>pivot&&low<=high) low++; while(arr[high][0]<pivot&&high>=low) high--; if(low<high) { int temp; temp=arr[low][0]; arr[low][0]=arr[high][0]; arr[high][0]=temp; temp=arr[low][1]; arr[low][1]=arr[high][1]; arr[high][1]=temp; temp=arr[low][2]; arr[low][2]=arr[high][2]; arr[high][2]=temp; } } int temp; temp=arr[left][0]; arr[left][0]=arr[high][0]; arr[high][0]=temp; temp=arr[left][1]; arr[left][1]=arr[high][1]; arr[high][1]=temp; temp=arr[left][2]; arr[left][2]=arr[high][2]; arr[high][2]=temp; return high; } void qsort(int arr[10010][5], int left, int right) { if(left<right) { int q = partition(arr, left, right); qsort(arr, left, q-1); qsort(arr, q+1, right); } } int main() { int i, j, a, b, c; scanf("%d %d", &n, &m); for(i=0;i<m;i++) { scanf("%d %d %d", &a, &b, &c); graph[cnt][0]=c; graph[cnt][1]=a; graph[cnt++][2]=b; visit[a]++; visit[b]++; } qsort(graph, 0, m-1); int erasecnt=0; int sum=0; //for(i=1;i<=n;i++) // printf("%d ", visit[i]); // printf("\n"); for(i=0;i<m;i++) { //printf("%d %d %d\n", graph[i][0], graph[i][1], graph[i][2]); if(m-erasecnt==n-1) { if(graph[i][0]%2==1) sum+=graph[i][0]; } else { if(visit[graph[i][1]]>1&&visit[graph[i][2]]>1) { visit[graph[i][1]]--; visit[graph[i][2]]--; erasecnt++; } } } printf("%d", sum); return 0; } */ #include <stdio.h> #include <time.h> void randomseed() { } int main() { }
0
0
3
ksw0633
2019년 10월 02일
In 소스 코드 제출
/* #include <stdio.h> #include <string.h> #include <windows.h> #include <math.h> #define MAX 1000 int numstack[1000]; int stack[1000]; char charstack[1000]; char equation[1000]; int numtop=0; int chartop=0; int top=0; void convert(); void calculator(); void convert() { int i; for(i=0;i<strlen(equation);i++) { //printf("------------------------------------------\n"); if(equation[i]>=48&&equation[i]<=57) { int temp=0; while(equation[i]>=48&&equation[i]<=57) { stack[top++]=equation[i]-'0'; i++; } int cnt=0; while(top>0) { temp+=(stack[top-1])*pow(10.0, cnt); top--; } numstack[numtop++]=temp; } if(equation[i]<48||equation[i]>57) { if(equation[i]==')'||equation[i]=='}'||equation[i]==']') calculator(); else charstack[chartop++]=equation[i]; } } calculator(); } void calculator()//괄호 계산은 1번 , 그냥 계산은 0번 { while(chartop>0) { int x, y; int temp; x=numstack[numtop-2]; y=numstack[numtop-1]; if(charstack[chartop-1]=='('||charstack[chartop-1]=='{'||charstack[chartop-1]=='[') { chartop--; return; } if(charstack[chartop-1]=='+') { temp=x+y; numtop=numtop-2; numstack[numtop++]=temp; } else if(charstack[chartop-1]=='-') { temp=x-y; numtop=numtop-2; numstack[numtop++]=temp; } else if(charstack[chartop-1]=='*') { temp=x*y; numtop=numtop-2; numstack[numtop++]=temp; } else if(charstack[chartop-1]=='/') { temp=x/y; numtop=numtop-2; numstack[numtop++]=temp; } chartop--; } } int main() { scanf("%s", equation); // printf("%s\n", equation); convert(); printf("%d\n", numstack[numtop-1]); return 0; } */ /* #include <stdio.h> #include <stdlib.h> #include <windows.h> #include <string.h> typedef struct node { int data; node *rlink; node *llink; }node; node* queue[1000]; int front=0; int rear=0; int heap[]; node *start; node *current; node *before; //int nodes=1; void MAXheap(); void deletemax(); void menu(); void print(); void MAXheap() { printf("---------------------------------------------------\n"); int num; printf("please enter a integer you want to insert.\n"); scanf("%d", &num); while(num<=0) { printf("please enter a integer.\n"); scanf("%d", &num); } if(start->data==0) { start->data=num; printf("insert complete.\n"); printf("---------------------------------------------------\n"); return; } node *newnode; newnode=(node*)malloc(sizeof(node)); //newnode->index=nodes++; newnode->llink=NULL; newnode->rlink=NULL; newnode->data=num; if(current->llink==NULL) current->llink=newnode; else current->rlink=newnode; //비교 if(current->data<newnode->data) { int temp; temp=current->data; current->data=newnode->data; newnode->data=temp; } before=current; current=newnode; printf("insert complete.\n"); printf("---------------------------------------------------\n"); } void deletemax() { printf("---------------------------------------------------\n"); if(start->data==0) { printf("the heap is empty\n"); return; } else if(start->llink==NULL) { printf("delete complete\n"); start->data=0; return; } node *temp; temp=start; start->data=current->data; free(current); current=before; while(1) { if(temp->llink==NULL||(temp->llink->data>=temp->data&&(temp->rlink->data>=temp->data||temp->rlink==NULL))) break; else { int t; if(temp->llink->data<temp->data) { t=temp->llink->data; temp->llink->data=temp->data; temp->data=t; } else { t=temp->rlink->data; temp->rlink->data=temp->data; temp->data=t; } } } printf("delete complete.\n"); printf("---------------------------------------------------\n"); } void menu() { printf("---------------------------------------------------\n"); printf("Welcome to heap management\n"); int m=0; start = (node*)malloc(sizeof(node)); //start->index=nodes++; start->rlink=NULL; start->llink=NULL; start->data=0; current=start; while(m!=4) { printf("press 1 to insert, 2 to delete the max node,3 to print, 4 to quit.\n"); scanf("%d", &m); if(m==1) MAXheap(); else if(m==2) deletemax(); else if(m==3) print(); else if(m==4) { printf("BYE."); return; } else printf("Please enter a number between 1 and 3.\n"); printf("---------------------------------------------------\n"); } } void print() { if(start->data==0) { printf("the heap is empty.\n"); printf("---------------------------------------------------\n"); return; } queue[rear++]=start; node *temp; while(1) { int i; for(i=front;i<rear;i++) { printf("%d ", queue[i]->data); if(queue[i]==current) { printf("\n"); front=0; rear=0; printf("---------------------------------------------------\n"); return; } } printf("\n"); int t=rear; for(i=front;i<t;i++) { if(queue[i]->llink!=NULL) queue[rear++]=queue[i]->llink; if(queue[i]->rlink!=NULL) queue[rear++]=queue[i]->rlink; } front=t; } } int main() { menu(); return 0; } */ /* #include<stdio.h> #include<string.h> #include<malloc.h> #include<windows.h> void addheap(); void deleteheap(); void print(); void menu(); int n=1; int heap[10000]; void addheap() { printf("--------------------------------------------------------------\n"); if(n>=10000) { printf("the heap is full.\n"); printf("--------------------------------------------------------------\n"); return; } int num; printf("please enter an integer.\n"); scanf("%d", &num); while(num<=0) { printf("please enter an integer.\n"); scanf("%d", &num); } heap[n++]=num; //printf("num=%d\n", num); // for(int k=0;k<n;k++) //printf("%d ", heap[k]); // printf("\n"); int i; i=(n-1)/2; int j; j=(n-1); //printf("%d %d %d %d %d\n",n, i, j, heap[i], heap[j]); while(heap[i]<heap[j]&&i>0) { //printf("debug\n"); int temp; temp=heap[i]; heap[i]=heap[j]; heap[j]=temp; j=i; i=i/2; } printf("insert complete.\n"); printf("--------------------------------------------------------------\n"); } void deleteheap() { printf("--------------------------------------------------------------\n"); if(n==1) { printf("the heap is empty.\n"); printf("--------------------------------------------------------------\n"); return; } heap[1]=heap[n-1]; n--; int i=1; while(1) { if(i*2>=n) break; else if(i*2+1>=n) break; if(heap[i]>heap[i*2]&&heap[i]>heap[i*2+1]) break; int temp; if(heap[i]<heap[i*2]) { temp=heap[i]; heap[i]=heap[i*2]; heap[i*2]=temp; i=i*2; } else if(heap[i]<heap[i*2+1]) { temp=heap[i]; heap[i]=heap[i*2+1]; heap[i*2+1]=temp; i=i*2+1; } } printf("delete complete.\n"); printf("--------------------------------------------------------------\n"); } void print() { printf("--------------------------------------------------------------\n"); if(n==1) { printf("the heap is empty.\n"); printf("--------------------------------------------------------------\n"); return; } int a=1; int b=1; int c; int i; printf("%d\n", heap[1]); while(a<n) { c=a+b*2; if(a+b*2>=n) c=n-1; for(i=a+1;i<=c;i++) printf("%d ", heap[i]); printf("\n"); a=a+b*2; b++; } printf("--------------------------------------------------------------\n"); } void menu() { int num=0; printf("--------------------------------------------------------------\n"); printf("welcome to heap management.\n"); while(num!=4) { printf("enter 1 to insert, 2 to delete the max node, 3 to print, 4 to quit.\n"); scanf("%d", &num); if(num==1) addheap(); else if(num==2) deleteheap(); else if(num==3) print(); else if(num==4) { printf("BYE.\n"); printf("--------------------------------------------------------------\n"); return; } else printf("please enter a number between 1 to 4.\n"); printf("--------------------------------------------------------------\n"); } } int main() { menu(); return 0; } */ #include <stdio.h> int map[200][200]; int visit[200][200]; int bfscheck[200][200]; int rain; int n; int max; int min=101; int bfscnt=1; void bfs(int x, int y, int danger); int findsafe(); void bfs(int x, int y, int danger) { //printf("debug\n"); /*if(danger==2) { printf("x=%d y=%d bfscnt=%d\n", x, y, bfscnt); bfscheck[x][y]=bfscnt; }*/ visit[x][y]=danger; if(visit[x][y-1]!=danger&&map[x][y-1]>danger&&y-1>=0) bfs(x, y-1, danger); if(visit[x][y+1]!=danger&&map[x][y+1]>danger&&y+1<n) bfs(x, y+1, danger); if(visit[x-1][y]!=danger&&map[x-1][y]>danger&&x-1>=0) bfs(x-1, y, danger); if(visit[x+1][y]!=danger&&map[x+1][y]>danger&&x+1<n) bfs(x+1, y, danger); } int findsafe() { int i,j; int cnt=min; int safe=0; while(cnt<=max) { int safecnt=0; for(i=0;i<n;i++) { for(j=0;j<n;j++) { if(visit[i][j]==cnt); else { if(map[i][j]>cnt) { bfs(i, j, cnt); if(cnt==2) bfscnt++; safecnt++; } else visit[i][j]=cnt; } } } if(safecnt>safe) safe=safecnt; /* if(safecnt==2) printf("%d\n", cnt);*/ cnt++; } return safe; } int main() { int i, j; scanf("%d", &n); for(i=0;i<n;i++) { for(j=0;j<n;j++) { scanf("%d", &map[i][j]); if(map[i][j]<min) min=map[i][j]; if(map[i][j]>max) max=map[i][j]; } } int ans = findsafe(); if(ans==0) ans=1; printf("%d", ans); return 0; }
0
0
1
ksw0633
2019년 10월 01일
In 소스 코드 제출
/* #include <stdio.h> #include <string.h> #include <windows.h> #include <math.h> #define MAX 1000 int numstack[1000]; int stack[1000]; char charstack[1000]; char equation[1000]; int numtop=0; int chartop=0; int top=0; void convert(); void calculator(); void convert() { int i; for(i=0;i<strlen(equation);i++) { //printf("------------------------------------------\n"); if(equation[i]>=48&&equation[i]<=57) { int temp=0; while(equation[i]>=48&&equation[i]<=57) { stack[top++]=equation[i]-'0'; i++; } int cnt=0; while(top>0) { temp+=(stack[top-1])*pow(10.0, cnt); top--; } numstack[numtop++]=temp; } if(equation[i]<48||equation[i]>57) { if(equation[i]==')'||equation[i]=='}'||equation[i]==']') calculator(); else charstack[chartop++]=equation[i]; } } calculator(); } void calculator()//괄호 계산은 1번 , 그냥 계산은 0번 { while(chartop>0) { int x, y; int temp; x=numstack[numtop-2]; y=numstack[numtop-1]; if(charstack[chartop-1]=='('||charstack[chartop-1]=='{'||charstack[chartop-1]=='[') { chartop--; return; } if(charstack[chartop-1]=='+') { temp=x+y; numtop=numtop-2; numstack[numtop++]=temp; } else if(charstack[chartop-1]=='-') { temp=x-y; numtop=numtop-2; numstack[numtop++]=temp; } else if(charstack[chartop-1]=='*') { temp=x*y; numtop=numtop-2; numstack[numtop++]=temp; } else if(charstack[chartop-1]=='/') { temp=x/y; numtop=numtop-2; numstack[numtop++]=temp; } chartop--; } } int main() { scanf("%s", equation); // printf("%s\n", equation); convert(); printf("%d\n", numstack[numtop-1]); return 0; } */ #include <stdio.h> #include <stdlib.h> #include <windows.h> #include <string.h> typedef struct node { int data; node *rlink; node *llink; }node; node* queue[1000]; int front=0; int rear=0; node *start; node *current; node *before; //int nodes=1; void MAXheap(); void deletemax(); void menu(); void print(); void MAXheap() { printf("---------------------------------------------------\n"); int num; printf("please enter a integer you want to insert.\n"); scanf("%d", &num); while(num<=0) { printf("please enter a integer.\n"); scanf("%d", &num); } if(start->data==0) { start->data=num; printf("insert complete.\n"); printf("---------------------------------------------------\n"); return; } node *newnode; newnode=(node*)malloc(sizeof(node)); //newnode->index=nodes++; newnode->llink=NULL; newnode->rlink=NULL; newnode->data=num; if(current->llink==NULL) current->llink=newnode; else current->rlink=newnode; //비교 if(current->data<newnode->data) { int temp; temp=current->data; current->data=newnode->data; newnode->data=temp; } before=current; current=newnode; printf("insert complete.\n"); printf("---------------------------------------------------\n"); } void deletemax() { printf("---------------------------------------------------\n"); if(start->data==0) { printf("the heap is empty\n"); return; } else if(start->llink==NULL) { printf("delete complete\n"); start->data=0; return; } node *temp; temp=start; start->data=current->data; free(current); current=before; while(1) { if(temp->llink==NULL||(temp->llink->data>=temp->data&&(temp->rlink->data>=temp->data||temp->rlink==NULL))) break; else { int t; if(temp->llink->data<temp->data) { t=temp->llink->data; temp->llink->data=temp->data; temp->data=t; } else { t=temp->rlink->data; temp->rlink->data=temp->data; temp->data=t; } } } printf("delete complete.\n"); printf("---------------------------------------------------\n"); } void menu() { printf("---------------------------------------------------\n"); printf("Welcome to heap management\n"); int m=0; start = (node*)malloc(sizeof(node)); //start->index=nodes++; start->rlink=NULL; start->llink=NULL; start->data=0; current=start; while(m!=4) { printf("press 1 to insert, 2 to delete the max node,3 to print, 4 to quit.\n"); scanf("%d", &m); if(m==1) MAXheap(); else if(m==2) deletemax(); else if(m==3) print(); else if(m==4) { printf("BYE."); return; } else printf("Please enter a number between 1 and 3.\n"); printf("---------------------------------------------------\n"); } } void print() { if(start->data==0) { printf("the heap is empty.\n"); printf("---------------------------------------------------\n"); return; } queue[rear++]=start; node *temp; while(1) { int i; for(i=front;i<rear;i++) { printf("%d ", queue[i]->data); if(queue[i]==current) { printf("\n"); front=0; rear=0; printf("---------------------------------------------------\n"); return; } } printf("\n"); int t=rear; for(i=front;i<t;i++) { if(queue[i]->llink!=NULL) queue[rear++]=queue[i]->llink; if(queue[i]->rlink!=NULL) queue[rear++]=queue[i]->rlink; } front=t; } } int main() { menu(); return 0; }
0
0
2
ksw0633
2019년 9월 29일
In 소스 코드 제출
/* #include <stdio.h> #include <string.h> #include <windows.h> #include <math.h> #define MAX 1000 int numstack[1000]; int stack[1000]; char charstack[1000]; char equation[1000]; int numtop=0; int chartop=0; int top=0; void convert(); void calculator(); void convert() { int i; for(i=0;i<strlen(equation);i++) { //printf("------------------------------------------\n"); if(equation[i]>=48&&equation[i]<=57) { int temp=0; while(equation[i]>=48&&equation[i]<=57) { stack[top++]=equation[i]-'0'; i++; } int cnt=0; while(top>0) { temp+=(stack[top-1])*pow(10.0, cnt); top--; } numstack[numtop++]=temp; } if(equation[i]<48||equation[i]>57) { if(equation[i]==')'||equation[i]=='}'||equation[i]==']') calculator(); else charstack[chartop++]=equation[i]; } } calculator(); } void calculator()//괄호 계산은 1번 , 그냥 계산은 0번 { while(chartop>0) { int x, y; int temp; x=numstack[numtop-2]; y=numstack[numtop-1]; if(charstack[chartop-1]=='('||charstack[chartop-1]=='{'||charstack[chartop-1]=='[') { chartop--; return; } if(charstack[chartop-1]=='+') { temp=x+y; numtop=numtop-2; numstack[numtop++]=temp; } else if(charstack[chartop-1]=='-') { temp=x-y; numtop=numtop-2; numstack[numtop++]=temp; } else if(charstack[chartop-1]=='*') { temp=x*y; numtop=numtop-2; numstack[numtop++]=temp; } else if(charstack[chartop-1]=='/') { temp=x/y; numtop=numtop-2; numstack[numtop++]=temp; } chartop--; } } int main() { scanf("%s", equation); // printf("%s\n", equation); convert(); printf("%d\n", numstack[numtop-1]); return 0; } */ #include <stdio.h> #include <stdlib.h> #include <windows.h> #include <string.h> typedef struct node { int index; int data; node *rlink; node *llink; }node; node *start; node *current; int nodes=1; void MAXheap(int num) { current->data=num; compare(); node *newnode; newnode=(node*)malloc(sizeof(node)); newnode->index=nodes++; if(current->llink==NULL) current->llink=newnode; else current->rlink=newnode; current=newnode; } int main() { int num; start = (*node)malloc(sizeof(node)); start->index=nodes++; start->rlink=NULL; start->llink=NULL; current=start; scanf("%d", &num); }
0
0
1
ksw0633
2019년 9월 26일
In 소스 코드 제출
#include <stdio.h> #include <string.h> #include <math.h> #define MAX 1000 int numstack[1000]; int stack[1000]; char charstack[1000]; char equation[1000]; int numtop=0; int chartop=0; int top=0; void convert(); void calculator(int type); void convert() { int i; for(i=0;i<strlen(equation);i++) { printf("------------------------------------------\n"); if(equation[i]>=48&&equation[i]<=57) { int temp=0; while(equation[i]>=48&&equation[i]<=57) { stack[top++]=equation[i]-'0'; i++; } int cnt=0; while(top>0) { temp+=(stack[top-1])*pow(10.0, cnt); top--; } numstack[numtop++]=temp; } if(equation[i]<48||equation[i]>57) { if(equation[i]==')'||equation[i]=='}'||equation[i]==']') calculator(1); else charstack[chartop++]=equation[i]; }/*for(int i=0;i<numtop;i++) printf("%d ", numstack[i]); printf("\n%d\n", numtop); for(int i=0;i<chartop;i++) printf("%c ", charstack[i]); printf("\n%d\n", chartop);*/ } calculator(0); } void calculator(int type)//괄호 계산은 1번 , 그냥 계산은 0번 { if(type==1) { while(charstack[chartop-1]!='('||charstack[chartop-1]!='{'||charstack[chartop-1]!='[') { printf("debug\n"); int x, y; int temp; x=numstack[numtop-2]; y=numstack[numtop-1]; if(charstack[chartop-1]=='+') { temp=x+y; numtop=numtop-2; numstack[numtop++]=temp; } else if(charstack[chartop-1]=='-') { temp=x-y; numtop=numtop-2; numstack[numtop++]=temp; } else if(charstack[chartop-1]=='*') { temp=x*y; numtop=numtop-2; numstack[numtop++]=temp; } else if(charstack[chartop-1]=='/') { temp=x/y; numtop=numtop-2; numstack[numtop++]=temp; } chartop--; } chartop--; } if(type==0) { while(chartop>0) { int x, y; int temp; x=numstack[numtop-2]; y=numstack[numtop-1]; if(charstack[chartop-1]=='+') { temp=x+y; numtop=numtop-2; numstack[numtop++]=temp; } else if(charstack[chartop-1]=='-') { temp=x-y; numtop=numtop-2; numstack[numtop++]=temp; } else if(charstack[chartop-1]=='*') { temp=x*y; numtop=numtop-2; numstack[numtop++]=temp; } else if(charstack[chartop-1]=='/') { temp=x/y; numtop=numtop-2; numstack[numtop++]=temp; } chartop--; } } } int main() { scanf("%s", equation); printf("%s\n", equation); convert(); printf("%d\n", numstack[numtop-1]); return 0; }
0
0
4
ksw0633
2019년 9월 24일
In 소스 코드 제출
#include <stdio.h> #include <malloc.h> typedef struct node { int data; node *rlink; node *llink; }node; void insertnode(node *head); void deletenode(node *head); void clearall(node *head); void printall(node *head); void menu(); void insertnode(node *head) { int x; int num; int i=0; node *newnode; node *nextr; node *nextl; newnode = (node*)malloc(sizeof(node)); printf("Please enter the position you wish to insert.\n"); scanf("%d", &x); while(x<=0) { printf("Please enter a number bigger than 0.\n"); scanf("%d", &x); } node *current; current=head; node *before; while(current!=NULL) { if(i==x) break; before=current; current=current->rlink; i++; } if(x>i) { printf("there aren't enough nodes.\n"); return; } printf("Please insert the number you want to insert.\n"); scanf("%d", &num); newnode->data = num; newnode->rlink=current; newnode->llink=before; if(current!=NULL) current->llink=newnode; if(before!=NULL) before->rlink=newnode; printf("insert complete.\n"); } void deletenode(node *head) { int x; printf("Please enter the position you wish to delete.\n"); scanf("%d", &x); while(x<=0) { printf("Please enter a number bigger than 0.\n"); scanf("%d", &x); } int i; node *current; current=head; if(current->rlink==NULL) { printf("the list is empty\n"); return; } else { node *nextl; node *nextr; nextl=current->llink; nextr=current->rlink; for(i=0;i<x;i++) { current=current->rlink; if(current==NULL) { printf("there aren't enough nodes.\n"); return; } if(current!=NULL) nextl=current->llink; else nextl=nextl->rlink; if(current!=NULL) nextr=current->rlink; } if(x>i) { printf("there aren't enough nodes.\n"); return; } if(nextr==NULL) { nextl->rlink=NULL; free(current); } else { nextl->rlink=nextr; nextr->llink=nextl; } printf("delete complete\n"); } } void printall(node *head) { if(head->rlink==NULL) { printf("the list is empty.\n"); return; } else { node *current; current=head->rlink; printf("List = "); while(current->rlink!=NULL) { printf("%d, ", current->data); current=current->rlink; } printf("%d\n", current->data); } } void deleteall(node *head) { if(head->rlink==NULL) { printf("the list is already empty\n"); return; } else { node *current; node *next; next=head->rlink; current=next; while(current->rlink!=NULL) { next=current->rlink; free(current); current=next; } free(current); head->rlink=NULL; printf("delete complete\n"); } } void menu() { node *head; head=(node*)malloc(sizeof(node)); head->llink=NULL; head->rlink=NULL; int type; while(type!=5) { printf("-----------------------------------------------\n"); printf("Welcome to list management!\n"); printf("Insert 1 to insert in a desired position.\n"); printf("Insert 2 to delete a desired position.\n"); printf("Insert 3 to print all nodes.\n"); printf("Insert 4 to delete all nodes.\n"); printf("Insert 5 to quit.\n"); scanf("%d", &type); if(type==1) insertnode(head); else if(type==2) deletenode(head); else if(type==3) printall(head); else if(type==4) deleteall(head); else if(type==5) { printf("BYE.\n"); return; } else printf("please insert a number between 1 and 5\n"); printf("-----------------------------------------------\n"); } } int main() { menu(); return 0; }
0
0
1
ksw0633
2019년 9월 22일
In 소스 코드 제출
//삽입 검색 삭제 전체 출력 //링크드리스트: 노드안에 데이터와 연결된 주소를 같이 넣어 능동적인 데이터 변형을 할수 있도록 함 /* #include <stdio.h> #include <malloc.h> typedef struct node//리스트의 노드 { int data;// 노드의 데이터 struct node *next;//다음 노드의 주소 }node; typedef struct{ node *head; }linkedlist; linkedlist* createlist(void) { linkedlist *L; L = (linkedlist*)malloc(sizeof(linkedlist)); L->head = NULL; return L; } linkedlist* createlist(void); void freelinkedlist(linkedlist*); void input(linkedlist*, int); void deletenode(linkedlist*); void printlist(linkedlist*); void menu(); void input(linkedlist *L) { int k; printf("----------------------------\n"); node *newnodenode; node *p; newnodenode = (node*)malloc(sizeof(node)); printf("Please type in a number you wish to insert\n"); scanf("%d", &k); newnodenode->data = k; newnodenode->next = NULL; if(L->head == NULL) { L->head = newnodenode; printf("insert complete \n"); printf("----------------------------\n"); return; } p = L->head; while(p->next!=NULL) p = p->next; p->next = newnodenode; printf("insert complete \n"); printf("----------------------------\n"); } void deletenode(linkedlist *L) { node* previous; node* current; printf("----------------------------\n"); if(L->head ==NULL) { printf("delete successful/n"); printf("----------------------------\n"); return; } if(L->head->next==NULL) { free(L->head); L->head = NULL; printf("delete successful\n"); printf("----------------------------\n"); return; } else { previous = L->head; current = L->head->next; while(current->next!=NULL) { previous = current; current = current->next; } free(current); previous->next = NULL; } printf("delete successful\n"); printf("----------------------------\n"); } void freelinkedlist(linkedlist* L) { node *p; printf("----------------------------\n"); while(L->head!= NULL) { p = L->head; L->head = L->head->next; free(p); p=NULL; } printf("all emptied\n"); printf("----------------------------\n"); } void printlist(linkedlist *L) { node *p; printf("----------------------------\n"); printf("L = ("); p = L->head; while(p !=NULL) { printf("%d", p->data); p = p->next; if(p!=NULL) printf(", "); } printf(")\n"); printf("----------------------------\n"); } void menu() { int m=0; linkedlist *L; L = createlist(); printf("Welcome to list management!\n"); while(m!=5) { printf("Press 1 for insert, 2 for delete, 3 for print all, 4 to free all, 5 to quit\n"); scanf("%d", &m); if(m==1) input(L); else if(m==2) deletenode(L); else if(m==3) printlist(L); else if(m==4) freelinkedlist(L); else printf("Please enter a number that is between 1 and 5\n"); } } int main() { menu(); return 0; } */ /* #include <stdio.h> #include <malloc.h> typedef struct node { int data; struct node* next; }node; node *initialize(); void menu(); void insertnode(node *CL); void insertmiddlenode(node *CL, int des); void deletenode(node *CL, int des); void printnode(node *CL); void clearall(node *CL); node * initialize()//리스트 생성 { node *CL; CL=(node*)malloc(sizeof(node)); CL->next = NULL; return CL; } void insertnode(node *CL) { node *newnode; int x; printf("please enter a number you desire to insert.\n"); scanf("%d", &x); newnode = (node*)malloc(sizeof(node)); newnode->data = x; if(CL->next == NULL)//만약 아직 공백 노드이면 첫노드에 값을 넣고, 첫노드를 자기자신과 연결시킴(첫노드가 마지막노드인것) { newnode=CL; newnode->next = newnode; } else//아니면 마지막 노드를 찾아, 첫노드와 연결시킨뒤 마지막 노드와 연결시켜 순환 구조를 이음 { node *temp; temp=CL; while(temp->next!=CL) temp=temp->next; newnode->next=temp->next; temp->next=newnode; CL = newnode; } printf("insert complete\n"); } void insertmiddlenode(node *CL, int des) { node *newnode; node *current; int x; int cnt=0; printf("please enter a number you desire to insert.\n"); scanf("%d", &x); newnode = (node*)malloc(sizeof(node)); newnode->data = x; if(CL->next==NULL)//공백 리스트일 경우 { CL=newnode; newnode->next=newnode; } else//current를 전달 받아 current와 current의 다음노드 중간에 넣음 { current=CL; while(cnt!=des) { if(CL->next==NULL) { printf("There aren't that much nodes.\n"); return; } current=current->next; cnt++; } newnode->next = current->next; current->next = newnode; } printf("insert complete\n"); } void deletenode(node *CL, int des)//전달된 노드(current) 다음의 노드(old)를 삭제함 { int x; int cnt=0; node* current; if(CL->next==NULL) { printf("the node is empty\n"); return; } else { current=CL; while(cnt!=des-1) { if(CL->next==NULL) { printf("There aren't that much nodes.\n"); return; } current=current->next; cnt++; } node *old; old = current->next; current->next = old->next; free(old); } printf("delete complete\n"); } void printnode(node *CL) { printf("List = "); node *current; if(CL->next==NULL) printf("empty\n"); else { current = CL; while(current->next!=CL) { printf("%d, ", current->data); current=current->next; } printf("%d\n", current->data); } } void clearall(node *CL) { node *current; current=CL->next; if(current==NULL) printf("the node is already empty\n"); else { while(current!=CL) { node *temp=current; current=current->next; free(temp); } CL->next=NULL; current=NULL; printf("clear complete\n"); } } void menu() { node *CL; int num; CL = initialize(); printf("Welcome to List Management!\n"); while(num!=6) { printf("Type in 1 to insert/modify the first node.\n"); printf("Type in 2 to insert a node in a desired position.\n"); printf("Type in 3 to delete a desired node\n"); printf("Type in 4 to print all nodes.\n"); printf("Type in 5 to clear all nodes.\n"); printf("Type in 6 to quit\n"); scanf("%d", &num); printf("-----------------------------------------------\n"); if(num==1) insertnode(CL); else if(num==2) { printf("Insert the node location you desire to insert.\n"); int temp; scanf("%d", &temp); insertmiddlenode(CL, temp); } else if(num==3) { printf("Insert the node location you desire to delete.\n"); int temp; scanf("%d", &temp); deletenode(CL, temp); } else if(num==4) printnode(CL); else if(num==5) clearall(CL); else if(num==6) printf("BYE.\n"); else printf("Please insert a number between 1 to 6.\n"); printf("-----------------------------------------------\n"); } } int main() { menu(); return 0; } */ #include <stdio.h> #include <malloc.h> typedef struct node { int data; struct node* next; }node; void menu(); void insertnode(node *tail); void deletenode(node *tail); void clearallnode(node *tail); void printnode(node *tail); void insertnode(node *tail) { printf("Insert the number you would like to insert.\n"); int x; scanf("%d", &x); int des; printf("Insert the node location you desire to insert.\n"); scanf("%d", &des); while(des<=0) { printf("please enter a number that is bigger than 0.\n"); scanf("%d", &des); } if(tail->next==NULL) { tail->data=x; tail->next=tail; } else { int cnt=0; node *newnode; newnode=(node*)malloc(sizeof(node)); newnode->data=x; node *temp; while(cnt!=des-1) { tail=tail->next; cnt++; } temp=tail->next; tail->next=newnode; newnode->next=temp; } printf("Insert complete.\n"); } void deletenode(node *tail) { int des; printf("Insert the node location you desire to delete.\n"); scanf("&d", &des); while(des<=0) { printf("please enter a number that is bigger than 0.\n"); scanf("%d", &des); } if(tail->next=NULL) { printf("the list is already empty\n"); } else { int cnt=0; node *temp; temp=tail; while(cnt!=des-1) { if(temp->next==tail&&cnt<des-1) { printf("there aren't enough nodes.\n"); return; } temp=temp->next; cnt++; } temp=tail->next; tail->next=tail->next->next; free(temp); printf("delete complete.\n"); } } void clearallnode(node *tail) { if(tail->next==NULL) { printf("the list is already empty\n"); return; } node *temp; temp=tail->next; while(temp!=tail) { node *current; current=temp; temp=temp->next; free(current); } tail->next=NULL; printf("clear complete.\n"); } void printnode(node *tail) { if(tail->next==NULL) { printf("the list is empty\n"); return; } printf("List = "); node *current; current = tail->next; while(current!=tail) { printf("%d, ", current->data); current=current->next; } printf("%d\n", current->data); } void menu() { node *CL; CL = (node*)malloc(sizeof(node)); CL->next=NULL; int num; printf("Welcome to list management\n"); while(num!=5) { printf("Type in 1 to insert a node in a desired position.\n"); printf("Type in 2 to delete a desired node\n"); printf("Type in 3 to clear all nodes.\n"); printf("Type in 4 to print all nodes.\n"); printf("Type in 5 to quit\n"); scanf("%d", &num); printf("-----------------------------------------------\n"); if(num==1) insertnode(CL); else if(num==2) deletenode(CL); else if(num==3) clearallnode(CL); else if(num==4) printnode(CL); else if(num==5) printf("BYE.\n"); else printf("Please insert a number between 1 to 5.\n"); printf("-----------------------------------------------\n"); } } int main() { menu(); return 0; }
0
0
2
ksw0633
2019년 9월 21일
In 소스 코드 제출
//삽입 검색 삭제 전체 출력 //링크드리스트: 노드안에 데이터와 연결된 주소를 같이 넣어 능동적인 데이터 변형을 할수 있도록 함 /* #include <stdio.h> #include <malloc.h> typedef struct node//리스트의 노드 { int data;// 노드의 데이터 struct node *next;//다음 노드의 주소 }node; typedef struct{ node *head; }linkedlist; linkedlist* createlist(void) { linkedlist *L; L = (linkedlist*)malloc(sizeof(linkedlist)); L->head = NULL; return L; } linkedlist* createlist(void); void freelinkedlist(linkedlist*); void input(linkedlist*, int); void deletenode(linkedlist*); void printlist(linkedlist*); void menu(); void input(linkedlist *L) { int k; printf("----------------------------\n"); node *newnodenode; node *p; newnodenode = (node*)malloc(sizeof(node)); printf("Please type in a number you wish to insert\n"); scanf("%d", &k); newnodenode->data = k; newnodenode->next = NULL; if(L->head == NULL) { L->head = newnodenode; printf("insert complete \n"); printf("----------------------------\n"); return; } p = L->head; while(p->next!=NULL) p = p->next; p->next = newnodenode; printf("insert complete \n"); printf("----------------------------\n"); } void deletenode(linkedlist *L) { node* previous; node* current; printf("----------------------------\n"); if(L->head ==NULL) { printf("delete successful/n"); printf("----------------------------\n"); return; } if(L->head->next==NULL) { free(L->head); L->head = NULL; printf("delete successful\n"); printf("----------------------------\n"); return; } else { previous = L->head; current = L->head->next; while(current->next!=NULL) { previous = current; current = current->next; } free(current); previous->next = NULL; } printf("delete successful\n"); printf("----------------------------\n"); } void freelinkedlist(linkedlist* L) { node *p; printf("----------------------------\n"); while(L->head!= NULL) { p = L->head; L->head = L->head->next; free(p); p=NULL; } printf("all emptied\n"); printf("----------------------------\n"); } void printlist(linkedlist *L) { node *p; printf("----------------------------\n"); printf("L = ("); p = L->head; while(p !=NULL) { printf("%d", p->data); p = p->next; if(p!=NULL) printf(", "); } printf(")\n"); printf("----------------------------\n"); } void menu() { int m=0; linkedlist *L; L = createlist(); printf("Welcome to list management!\n"); while(m!=5) { printf("Press 1 for insert, 2 for delete, 3 for print all, 4 to free all, 5 to quit\n"); scanf("%d", &m); if(m==1) input(L); else if(m==2) deletenode(L); else if(m==3) printlist(L); else if(m==4) freelinkedlist(L); else printf("Please enter a number that is between 1 and 5\n"); } } int main() { menu(); return 0; } */ #include <stdio.h> #include <malloc.h> typedef struct node { int data; struct node* next; }node; node *initialize(); void menu(); void insertnode(node *CL); void insertmiddlenode(node *CL, int des); void deletenode(node *CL, int des); void printnode(node *CL); void clearall(node *CL); node * initialize()//리스트 생성 { node *CL; CL=(node*)malloc(sizeof(node)); CL->next = NULL; return CL; } void insertnode(node *CL) { node *newnode; int x; printf("please enter a number you desire to insert.\n"); scanf("%d", &x); newnode = (node*)malloc(sizeof(node)); newnode->data = x; if(CL->next == NULL)//만약 아직 공백 노드이면 첫노드에 값을 넣고, 첫노드를 자기자신과 연결시킴(첫노드가 마지막노드인것) { newnode=CL; newnode->next = newnode; } else//아니면 마지막 노드를 찾아, 첫노드와 연결시킨뒤 마지막 노드와 연결시켜 순환 구조를 이음 { node *temp; temp=CL; while(temp->next!=CL) temp=temp->next; newnode->next=temp->next; temp->next=newnode; CL = newnode; } printf("insert complete\n"); } void insertmiddlenode(node *CL, int des) { node *newnode; node *current; int x; int cnt=0; printf("please enter a number you desire to insert.\n"); scanf("%d", &x); newnode = (node*)malloc(sizeof(node)); newnode->data = x; if(CL->next==NULL)//공백 리스트일 경우 { CL=newnode; newnode->next=newnode; } else//current를 전달 받아 current와 current의 다음노드 중간에 넣음 { current=CL; while(cnt!=des) { if(CL->next==NULL) { printf("There aren't that much nodes.\n"); return; } current=current->next; cnt++; } newnode->next = current->next; current->next = newnode; } printf("insert complete\n"); } void deletenode(node *CL, int des)//전달된 노드(current) 다음의 노드(old)를 삭제함 { int x; int cnt=0; node* current; if(CL->next==NULL) { printf("the node is empty\n"); return; } else { current=CL; while(cnt!=des-1) { if(CL->next==NULL) { printf("There aren't that much nodes.\n"); return; } current=current->next; cnt++; } node *old; old = current->next; current->next = old->next; free(old); } printf("delete complete\n"); } void printnode(node *CL) { printf("List = "); node *current; if(CL->next==NULL) printf("empty\n"); else { current = CL; while(current->next!=CL) { printf("%d, ", current->data); current=current->next; } printf("%d\n", current->data); } } void clearall(node *CL) { node *current; current=CL->next; if(current==NULL) printf("the node is already empty\n"); else { while(current!=CL) { node *temp=current; current=current->next; free(temp); } CL->next=NULL; current=NULL; } } void menu() { node *CL; int num; CL = initialize(); printf("Welcome to List Management!\n"); while(num!=6) { printf("Type in 1 to insert/modify the first node.\n"); printf("Type in 2 to insert a node in a desired position.\n"); printf("Type in 3 to delete a desired node\n"); printf("Type in 4 to print all nodes.\n"); printf("Type in 5 to clear all nodes.\n"); printf("Type in 6 to quit\n"); scanf("%d", &num); printf("-----------------------------------------------\n"); if(num==1) insertnode(CL); else if(num==2) { printf("Insert the node location you desire to insert.\n"); int temp; scanf("%d", &temp); insertmiddlenode(CL, temp); } else if(num==3) { printf("Insert the node location you desire to delete.\n"); int temp; scanf("%d", &temp); deletenode(CL, temp); } else if(num==4) printnode(CL); else if(num==5) clearall(CL); else if(num==6) printf("BYE.\n"); else printf("Please insert a number between 1 to 6.\n"); printf("-----------------------------------------------\n"); } } int main() { menu(); return 0; }
0
0
2
ksw0633
2019년 9월 19일
In 소스 코드 제출
//삽입 검색 삭제 전체 출력 //링크드리스트: 노드안에 데이터와 연결된 주소를 같이 넣어 능동적인 데이터 변형을 할수 있도록 함 #include <stdio.h> #include <malloc.h> typedef struct node//리스트의 노드 { int data;// 노드의 데이터 struct node *next;//다음 노드의 주소 }node; typedef struct{ node *head; }linkedlist; linkedlist* createlist(void) { linkedlist *L; L = (linkedlist*)malloc(sizeof(linkedlist)); L->head = NULL; return L; } linkedlist* createlist(void); void freelinkedlist(linkedlist*); void input(linkedlist*, int); void deletenode(linkedlist*); void printlist(linkedlist*); void menu(); void input(linkedlist *L) { int k; printf("----------------------------\n"); node *newnode; node *p; newnode = (node*)malloc(sizeof(node)); printf("Please type in a number you wish to insert\n"); scanf("%d", &k); newnode->data = k; newnode->next = NULL; if(L->head == NULL) { L->head = newnode; printf("insert complete \n"); printf("----------------------------\n"); return; } p = L->head; while(p->next!=NULL) p = p->next; p->next = newnode; printf("insert complete \n"); printf("----------------------------\n"); } void deletenode(linkedlist *L) { node* previous; node* current; printf("----------------------------\n"); if(L->head ==NULL) { printf("delete successful/n"); printf("----------------------------\n"); return; } if(L->head->next==NULL) { free(L->head); L->head = NULL; printf("delete successful\n"); printf("----------------------------\n"); return; } else { previous = L->head; current = L->head->next; while(current->next!=NULL) { previous = current; current = current->next; } free(current); previous->next = NULL; } printf("delete successful\n"); printf("----------------------------\n"); } void freelinkedlist(linkedlist* L) { node *p; printf("----------------------------\n"); while(L->head!= NULL) { p = L->head; L->head = L->head->next; free(p); p=NULL; } printf("all emptied\n"); printf("----------------------------\n"); } void printlist(linkedlist *L) { node *p; printf("----------------------------\n"); printf("L = ("); p = L->head; while(p !=NULL) { printf("%d", p->data); p = p->next; if(p!=NULL) printf(", "); } printf(")\n"); printf("----------------------------\n"); } void menu() { int m=0; linkedlist *L; L = createlist(); printf("Welcome to list management!\n"); while(m!=5) { printf("Press 1 for insert, 2 for delete, 3 for print all, 4 to free all, 5 to quit\n"); scanf("%d", &m); if(m==1) input(L); else if(m==2) deletenode(L); else if(m==3) printlist(L); else if(m==4) freelinkedlist(L); else printf("Please enter a number that is between 1 and 5\n"); } } int main() { menu(); return 0; }
0
0
4

ksw0633

더보기
bottom of page