#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#define FALSE 0
#define TRUE 1
typedef int element;
typedef struct ListNode {
element data;
struct ListNode *link;
} ListNode;
typedef struct {
ListNode *head; // 헤드 포인터
int length; // 노드의 개수
} ListType;
// phead: 리스트의 헤드 포인터의 포인터
// p : 선행 노드
// new_node : 삽입될 노드
void error(char *message)
{
fprintf(stderr,"%s\n",message);
exit(1);
}
void insert_node(ListNode **phead, ListNode *p,
ListNode *new_node)
//giving way to change headNode itself
// previous node to manipulate likes
//new data node to insert in the list
//to give **phead, new node should know
{
if( *phead == NULL ){ // 공백리스트인 경우
new_node->link = NULL;
*phead = new_node;
}
else if( p == NULL ){ // if previous node input value is null, means that new node is supposed to be head of the list
new_node->link = *phead;
*phead = new_node;
}
else { // p 다음에 삽입
new_node->link = p->link;
p->link = new_node;
}
}
// 리스트를 초기화한다.
void init(ListType *list)
{
if( list == NULL ) return;
list->length = 0;
list->head = NULL;//아무것도 가르키지 않는 포인터를 초기화하는것은 아주 중요!!
}
//should find out which node will be previous node or NULL
ListNode *get_node_at_ascending(ListType *list, element data)//so called item in comments
{
ListNode *tmp = list->head;//tracker
ListNode *pre = NULL;//pre must be null if the new node is the smallest value in the list
if( data < 0 )
{
error("position error ocurred");
return NULL; //wrong input of node index
}
if(list->length == 0)
{printf("i am first listnode\n");
return pre;
}
while(tmp != NULL)
{//when temp gets the end of the list, it stops
if( ( data <= tmp->data ) )//if input data is smaller or same than tracker vaule
{
printf("%d is less than previous one\n", data);
return pre;
//do not make additional tracking and return pre
/*to put second node
if all the node values in the list is bigger than item
item should be the first node
THEN pre node MUST RETURN NULL
so that insert function take new item as (new)head node of the list
*/
}
//could't find smaller value than data, move the trackers
pre = tmp;
tmp = tmp -> link;
}
return pre;
}
// 리스트의 항목의 개수를 반환한다.
int get_length(ListType *list)
{
return list->length;
}
// 주어진 위치에 데이터를 정렬된 형태로 삽입한다.
void add_ascending(ListType *list, element data)
{
ListNode *pre;
ListNode *newNode = (ListNode*)malloc(sizeof(ListNode));
if(newNode == NULL )
{
error("memory allocation error occured\n");
}
newNode->data = data;
pre = get_node_at_ascending(list, data);
//now p saves previous node to put
insert_node(&(list->head), pre , newNode );
list->length++;
}
int is_empty(ListType *list)
{
if( list->head == NULL ) return 1;
else return 0;
}
// 버퍼의 내용을 출력한다.
void display(ListType *list)
{
int i;
ListNode *node=list->head;
printf("( ");
for(i=0;i<list->length;i++){
printf("%d ",node->data);
node = node->link;
}
printf(" )\n");
}
//
int main()
{
ListType list1;
init(&list1);
int data = 0;
ListNode* newNode;
//phead->link = list1.head;
while(1) {
printf("숫자를 입력하세요: ");
scanf("%d", &data);
add_ascending(&list1, data);
display(&list1);
if (data == -1) {
break;
}
}
return 0;
}
/*
get_node_at_ascending / add_ascending / English comments
are written by 5396952 HyeonminLEE
else codes and Korean comments by Data Structures in C (천인국 외 2인) , 박주건
*/
#include <stdlib.h>
#include <limits.h>
#define FALSE 0
#define TRUE 1
typedef int element;
typedef struct ListNode {
element data;
struct ListNode *link;
} ListNode;
typedef struct {
ListNode *head; // 헤드 포인터
int length; // 노드의 개수
} ListType;
// phead: 리스트의 헤드 포인터의 포인터
// p : 선행 노드
// new_node : 삽입될 노드
void error(char *message)
{
fprintf(stderr,"%s\n",message);
exit(1);
}
void insert_node(ListNode **phead, ListNode *p,
ListNode *new_node)
//giving way to change headNode itself
// previous node to manipulate likes
//new data node to insert in the list
//to give **phead, new node should know
{
if( *phead == NULL ){ // 공백리스트인 경우
new_node->link = NULL;
*phead = new_node;
}
else if( p == NULL ){ // if previous node input value is null, means that new node is supposed to be head of the list
new_node->link = *phead;
*phead = new_node;
}
else { // p 다음에 삽입
new_node->link = p->link;
p->link = new_node;
}
}
// 리스트를 초기화한다.
void init(ListType *list)
{
if( list == NULL ) return;
list->length = 0;
list->head = NULL;//아무것도 가르키지 않는 포인터를 초기화하는것은 아주 중요!!
}
//should find out which node will be previous node or NULL
ListNode *get_node_at_ascending(ListType *list, element data)//so called item in comments
{
ListNode *tmp = list->head;//tracker
ListNode *pre = NULL;//pre must be null if the new node is the smallest value in the list
if( data < 0 )
{
error("position error ocurred");
return NULL; //wrong input of node index
}
if(list->length == 0)
{printf("i am first listnode\n");
return pre;
}
while(tmp != NULL)
{//when temp gets the end of the list, it stops
if( ( data <= tmp->data ) )//if input data is smaller or same than tracker vaule
{
printf("%d is less than previous one\n", data);
return pre;
//do not make additional tracking and return pre
/*to put second node
if all the node values in the list is bigger than item
item should be the first node
THEN pre node MUST RETURN NULL
so that insert function take new item as (new)head node of the list
*/
}
//could't find smaller value than data, move the trackers
pre = tmp;
tmp = tmp -> link;
}
return pre;
}
// 리스트의 항목의 개수를 반환한다.
int get_length(ListType *list)
{
return list->length;
}
// 주어진 위치에 데이터를 정렬된 형태로 삽입한다.
void add_ascending(ListType *list, element data)
{
ListNode *pre;
ListNode *newNode = (ListNode*)malloc(sizeof(ListNode));
if(newNode == NULL )
{
error("memory allocation error occured\n");
}
newNode->data = data;
pre = get_node_at_ascending(list, data);
//now p saves previous node to put
insert_node(&(list->head), pre , newNode );
list->length++;
}
int is_empty(ListType *list)
{
if( list->head == NULL ) return 1;
else return 0;
}
// 버퍼의 내용을 출력한다.
void display(ListType *list)
{
int i;
ListNode *node=list->head;
printf("( ");
for(i=0;i<list->length;i++){
printf("%d ",node->data);
node = node->link;
}
printf(" )\n");
}
//
int main()
{
ListType list1;
init(&list1);
int data = 0;
ListNode* newNode;
//phead->link = list1.head;
while(1) {
printf("숫자를 입력하세요: ");
scanf("%d", &data);
add_ascending(&list1, data);
display(&list1);
if (data == -1) {
break;
}
}
return 0;
}
/*
get_node_at_ascending / add_ascending / English comments
are written by 5396952 HyeonminLEE
else codes and Korean comments by Data Structures in C (천인국 외 2인) , 박주건
*/
댓글
댓글 쓰기