//삽입 검색 삭제 전체 출력
//링크드리스트: 노드안에 데이터와 연결된 주소를 같이 넣어 능동적인 데이터 변형을 할수 있도록 함
/*
#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;
}