//****************************************************************************
//단일 연결리스트 head노드만을 이용 !!
//****************************************************************************

#include <stdio.h>
#include <stdlib.h>

typedef struct _Node
{
int data;
struct _Node *next;
}Node;
//****************************************************************************
Node * head;
//****************************************************************************
Node *CreateNode(int data)
{
Node * newnode = (Node *)malloc(sizeof(Node));
newnode->data = data;
newnode->next = NULL;
return newnode;
}//노드 생성 함수 !!
//****************************************************************************
void init()
{
head = CreateNode(0);
}
//****************************************************************************
// 삭제 함수
//****************************************************************************
//맨 뒤의 노드를 삭제 !!
//****************************************************************************
void pop_back()
{
Node *p, *s;
if(head->next == NULL){
puts("list empty !!");
}
else{
s= head;
p = head->next;
while(p->next != NULL){
s = p;
p = p->next;
}
}
s ->next = NULL;
free(p);
}
//****************************************************************************
//맨 앞의 노드를 삭제 !!
//****************************************************************************
void pop_front()
{
Node *p;
if(head->next == NULL){
printf("list empty !! \n");
}
else{
p = head->next;
head->next = p->next;
free(p);
}
}
//****************************************************************************
// data에 해당하는 노드 삭제!!
//****************************************************************************
void erase_value(int data)
{
Node *p,*s;
p = head->next;
s = head;
while(p->next != NULL){
if(p->data == data){
s->next = p->next;
free(p);
return;
}
else{
s=p;
p = p->next;
}
}
printf("No date");
puts("");
}
//****************************************************************************
// 1번째 3번째
//****************************************************************************
void erase_idx(int idx)
{
Node *p,*s;
p = head->next;
s = head;
int i=0;
while(p->next != NULL){
if(i == idx-1){
s->next = p->next;
free(p);
return;
}
else{
s = p;
p = p->next;
}
i++;
}
printf("No date");
puts("");

}
//****************************************************************************
//****************************************************************************
//입력 함수들
//****************************************************************************
void push_back(int data)
{
Node * p = head;
Node * newnode = CreateNode(data);
while(p->next != NULL)
{
p = p->next;
}
p ->next = newnode;
}
void push_front(int data)
{
Node *newnode = CreateNode(data);
newnode->next = head->next;
head->next = newnode;
}
void insert(int key, int data)
{
Node * p = head->next;
while(p->next != NULL){
if(p->next->data == key) {
Node * newnode =CreateNode(data);
newnode->data = data;
newnode->next = p->next;
p->next = newnode;
return;
}
else{
p = p->next;
}
}
push_back(data);
}
void insert_back(int key, int data)
{
Node *p = head->next;
while(p->next !=NULL){
if(p->data == key){
Node *newnode = CreateNode(data);
p->next->data = data;
newnode->next = p->next;
p->next = newnode;
return;
}
else{
p = p->next;
}
}
push_back(data);
}
void show()
{
Node * p = head->next;
while(p != NULL){
printf("%d --> ",p->data);
p = p->next;
}
puts("");
}
int Find_Number(int num)
{
Node *p=head;
while(p->next != NULL)
{
if(p->data == num)
{
printf("%d\n",p->data);
return p->data;
}
else
{
p = p->next;
}
}
puts("nonono");
return 0;
}
int Fine_idx(int data)
{
int i=0;
Node *p=head;
while(p->next != NULL){
if(i == data){
printf("%d\n",p->data);
return p->data;
}
else{
p = p->next;
}
i++;
}
puts("nonono");
return NULL;
}
int Fine_Back()
{
Node *p = head;
while(p->next != NULL){
p = p->next;
}
printf("%d\n",p->data);
return p->data;
}
int Fine_Start()
{
Node *p = head->next;
if(p){
printf("%d\n",p->data);
return p->data;
}
else{
puts("empty data");
}
return 0;
}

void main()
{
init();
push_back(5);
show();
push_back(4);
show();
push_front(2);
show();
push_front(15);
show();
push_front(17);
show();
insert(2,40);
show();
insert_back(15,33);
show();
Find_Number(2);
Fine_idx(1);

}


'Programing > 자료구조' 카테고리의 다른 글

단일연결리스트.cpp  (0) 2016.11.30
단일연결리스트.h  (0) 2016.11.30
이중연결리스트  (0) 2016.11.30
단일 연결리스트 더미노드  (0) 2016.11.30
자료구조  (0) 2016.11.30

+ Recent posts