Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
5_codeup_연습문제_연결리스트
#include <stdio.h>
typedef struct Student {
int no;
char name[10];
}student;
void insertSt (student *p, student t, int *n) {
int i;
for (i=0; i<*n && p[i].no<=t.no; i++) if(p[i].no==t.no) return;
for (int j=*n-1; j>=i; j--) p[j+1]=p[j];
p[i]=t; (*n)++;
}
void deleteSt(student *p, student t, int *n) {
int i;
for(i=0;i<*n;i++) if(p[i].no==t.no) break;
if(i<*n){
for(int j=i;j<*n-1;j++) p[j]=p[j+1];
(*n)--;
}
}
int main () {
int n,len=0,k[5]; char c; student t;
scanf("%d",&n); student p[100];
for (int i=0; i<n; i++) {
scanf(" %c %d %s",&c,&t.no,t.name);
if (c=='I') insertSt(p,t,&len);
else deleteSt(p,t,&len);
}
for (int i=0; i<5; i++) {
scanf("%d",&k[i]);
}
for (int i=0; i<5; i++) {
printf("%d %s\n",p[k[i]-1].no,p[k[i]-1].name);
}
}
#include<stdio.h>
#include<stdlib.h>
typedef struct node {
int no;
char name[10];
struct node *next;
}node;
typedef node* Nptr;
Nptr head;
void insertnode(node p) {
Nptr t, pre=NULL;
Nptr new1=(node*) malloc(sizeof(node));
*new1=p;
for (t=head; t!=NULL && t->no<=p.no; pre=t,t=t->next)
if(t->no == p.no) return;
new1->next=t;
if (pre) pre->next=new1;
else head=new1;
}
void deletenode(node p) {
Nptr t, pre=NULL;
for (t=head;t!=NULL&&t->no<p.no;pre=t,t=t->next);
if (t && t->no==p.no) {
if (pre) pre->next=t->next;
else head=t->next;
free(t);
}
}
int main() {
int n, i, loc, cnt;
int ans[5];
scanf("%d",&n);
for (i=0;i<n;i++) {
char c;
node p;
scanf(" %c %d %s",&c, &p.no, p.name);
if (c=='I') insertnode(p);
else deletenode(p);
}
for (i=0;i<5;i++) {
scanf("%d",&ans[i]);
}
loc=0;
cnt=0;
for(Nptr t=head;t!=NULL && cnt<5; t=t->next,loc++)
if(ans[cnt]-1==loc) {
printf("%d %s\n",t->no, t->name);
cnt++;
}
return 0;
}
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int no;
char name[10];
struct Node * next;
}node;
typedef node* Nptr;
Nptr head=NULL;
void insertSt(node p){
Nptr t, pre=NULL;
Nptr n = (node *)malloc(sizeof(node));
*n = p;
for(t=head;t!=NULL&&t->no<p.no;pre=t,t=t->next);
n->next=t;
if(pre) pre->next=n;
else head = n;
}
void deleteSt(node p) {
Nptr t, pre=NULL;
for(t=head; t!=NULL && t->no<p.no; pre=t,t=t->next);
if(t && t->no==p.no){
if(pre) pre->next = t->next;
else head = t->next;
free(t);
}
}
int main() {
int n,loc,cnt,ans[5];
char c;
node temp;
scanf("%d",&n);
for(int i=0;i<n;i++) {
scanf(" %c %d %s", &c, &temp.no, temp.name);
if(c=='I') insertSt(temp);
else deleteSt(temp);
}
for(int i=0;i<5;i++)
scanf("%d",&ans[i]);
loc=0;
cnt=0;
for(Nptr t=head;t!=NULL && cnt<5; t=t->next,loc++)
if(ans[cnt]-1==loc) {
printf("%d %s\n",t->no, t->name);
cnt++;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.