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

190924

게시판: 소스 코드 제출

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