기본 콘텐츠로 건너뛰기

simpleLinkedList-ascending int list (ex2)

#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인) , 박주건
*/

댓글

이 블로그의 인기 게시물

JS 5.5 task6 Create an extendable calculator

<HTML>   <body>   </body>   <script> function Calculator() {   let methods = {     "-" : (a, b) => a - b,     "+" : (a, b) => a + b   };   //methods is an object which keeps key for operators   //and value to return the actual operation values   //each returns the result of operation that key(operator) does   this.calculate = function (str){     //calculate is one element in the function Calculator     //it takes the string and returns the value     //in the function element list is delimeted by , not ;     let split = str.split(" "),     a = +split[0],     op = split[1],     b = split [2]     if(!methods[op] || isNaN(a) || isNaN(b)) {       return NaN; // error handling     }     return methods[op](a,b);   }   this.addMethod = function(name, func){     methods[name] = func;     //this is how to add new key and ele to object   } } let powerCalc = new Calculator; powerCalc.addMethod("*&

JS 5.7 task5 Store read dates

<HTML>   <body>   </body>    <script>    let messages = [        {text: "Hello", from: "John"},        {text: "How goes?", from: "John"},        {text: "See you soon", from: "Alice"}    ];    let readMap = new WeakMap();    alert(readMap.size);    readMap.set(messages[0], new Date(2019, 3, 5));   </script> </HTML> <!-- task4 needed weakSet to save simply readmessage, this task needs to save THE TIME IT WAS READ along with the message itself the message out of the set or map means it hasn't been read I kinda feel good and bad at the same time to happen to read the solution but I do get to think more about the difference with tasks and be more available to understand the main contents so I think, its good? -->

How to set base url when deployed in Heroku? : base url and axios

https://stackoverflow.com/questions/47164330/axios-api-calls-in-heroku/47165888 baseUrl = process.env.baseURL || "http://localhost:5000" Even more stable way https://stackoverflow.com/questions/52129849/how-to-get-the-base-url-variable-on-a-deployed-heroku-node-app const production  = 'https://examplePage.com'; const development = 'http://localhost:3000/'; const url = (process.env.NODE_ENV ? production : development); process.env.NODE_ENV will resolve to undefined if you are running on localhost production mode. and return production if you have deployed the app production mode.