본문 바로가기

IT/Algorithm

Algorithm: 링크드리스트(Linked list)

#include <stdio.h>

#include <malloc.h>


typedef struct _node {

int name;

struct _node *link;

} node;


node *head, *tail;


void init();

void odered_insert(int name);

void nexted_insert(int k);

void print_node();

node* find(int name);

void del(int name);

void modify(int std_name,int mod_name);



void main(){

node *temp;


init();

odered_insert(1);

odered_insert(2);

odered_insert(3);

odered_insert(4);

odered_insert(5);

modify(5,7);

nexted_insert(3,0);

del(4);

print_node();

}


void init(){

head = (node*)malloc(sizeof(node));

tail = (node*)malloc(sizeof(node));

head->link = tail;

tail->link = 0;

}


void odered_insert(int name){

node *temp;

node *h;

temp = (node*)malloc(sizeof(node));

h = head;

while(h->link != tail){

h = h->link;

}

temp->name = name;

h->link = temp;

temp->link = tail;

}


void nexted_insert(int std_name,int nxt_name){

node *std_temp;

node *nxt_temp;


std_temp = find(std_name);

nxt_temp = (node*)malloc(sizeof(node));

nxt_temp->link = std_temp->link;

nxt_temp->name = nxt_name;

std_temp->link = nxt_temp;

}


void print_node(){

node *temp;


temp = head->link;

while(temp->link != 0){

printf("%d\n",temp->name);

temp = temp->link;

}

}


node* find(int name){

node *temp;

temp = head->link;


while(temp->name != name){

temp = temp->link;

}

return temp;

}

void del(int name){

node *pre_temp;

node *del_temp;


del_temp = find(name);

pre_temp = head->link;

while(pre_temp->link != del_temp){

pre_temp = pre_temp->link;

}

pre_temp->link = del_temp->link;

free(del_temp);

}


void modify(int std_name,int mod_name){

node *temp;


temp = find(std_name);


temp->name = mod_name;

}