KakaoTalk_20190606_001802318.png
  • 246x0w
Welcome
Curriculum
Install&Go
Board
실제 작동 상태를 확인하려면 라이브 사이트로 이동하세요.
  • 카테고리
  • 전체 게시물
  • 내 게시물
ksw0633
2019년 9월 22일

190922

게시판: 소스 코드 제출

//삽입 검색 삭제 전체 출력


//링크드리스트: 노드안에 데이터와 연결된 주소를 같이 넣어 능동적인 데이터 변형을 할수 있도록 함

/*

#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
댓글
댓글 0개
주소 : 경기도 용인시 광교중앙로 302 블루 스퀘어 602호
연락처 : 031) 216 - 1546
사업자등록번호 : 465-92-00916
​학원 등록 제 4603호