#include "Node.h"

Node::Node(int num, Node *n,Node*p)
{
data = num;
next = n;
prev = p;

}
Node::Node(int num)
{

data = num;
next = NULL;
prev = NULL;
}
Node *Node::getPrev()
{
return prev;
}
Node * Node::getNext()
{
return next;
}

void Node::setNext(Node *n)
{
next = n;
}
void Node::setPrev(Node *p)
{
prev = p;
}

int Node::get_data()
{
return data;
}
void Node::set_data(Node *p)
{
data = p->data;
}


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

STL 컨터이네(map,multimap)  (0) 2016.12.01
STL find함수 사용  (0) 2016.12.01
이중연결리스트 노드클래스  (0) 2016.12.01
이중연결리스트.cpp  (0) 2016.12.01
단일연결리스트.cpp  (0) 2016.11.30
#pragma once

#include <iostream>
class Node
{
private :
int data;
Node *prev;
Node *next;
public :
Node(int num, Node *n,Node* p);
Node(int num);

Node * getNext();
Node * getPrev();
int get_data();

void setNext(Node *n);
void setPrev(Node *p);
void set_data(Node *p);
};


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

STL find함수 사용  (0) 2016.12.01
이중연결리스트 노드 cpp  (0) 2016.12.01
이중연결리스트.cpp  (0) 2016.12.01
단일연결리스트.cpp  (0) 2016.11.30
단일연결리스트.h  (0) 2016.11.30

/*****************************************************************************
*  ------------------------------- dlist.c --------------------------------  *
*****************************************************************************/
#include <stdlib.h>
#include <string.h>
#include "dlist.h"
/*****************************************************************************
*  ------------------------------ dlist_init ------------------------------  *
*****************************************************************************/
void dlist_init(DList *list, void (*destroy)(void *data)) {
 /*****************************************************************************
 *  Initialize the list.                                                      *
 *****************************************************************************/
 list->size = 0;
 list->destroy = destroy;
 list->head = NULL;
 list->tail = NULL;
 return;
}
/*****************************************************************************
*  ---------------------------- dlist_destroy -----------------------------  *
*****************************************************************************/
void dlist_destroy(DList *list) {
 void               *data;
 /*****************************************************************************
 *  Remove each element.                                                      *
 *****************************************************************************/
 while (dlist_size(list) > 0) {
  if (dlist_remove(list, dlist_tail(list), (void **)&data) == 0 && list->
   destroy != NULL) {
    /***********************************************************************
    *  Call a user-defined function to free dynamically allocated data.    *
    ***********************************************************************/
    list->destroy(data);
  }
 }
 /*****************************************************************************
 *  No operations are allowed now, but clear the structure as a precaution.   *
 *****************************************************************************/
 memset(list, 0, sizeof(DList));
 return;
}
/*****************************************************************************
*  ---------------------------- dlist_ins_next ----------------------------  *
*****************************************************************************/
int dlist_ins_next(DList *list, DListElmt *element, const void *data) {
 DListElmt          *new_element;
 /*****************************************************************************
 *  Do not allow a NULL element unless the list is empty.                     *
 *****************************************************************************/
 if (element == NULL && dlist_size(list) != 0)
  return -1;
 /*****************************************************************************
 *  Allocate storage for the element.                                         *
 *****************************************************************************/
 if ((new_element = (DListElmt *)malloc(sizeof(DListElmt))) == NULL)
  return -1;
 /*****************************************************************************
 *  Insert the new element into the list.                                     *
 *****************************************************************************/
 new_element->data = (void *)data;
 if (dlist_size(list) == 0) {
  /**************************************************************************
  *  Handle insertion when the list is empty.                               *
  **************************************************************************/
  list->head = new_element;
  list->head->prev = NULL;
  list->head->next = NULL;
  list->tail = new_element;
 }
 else {
  /**************************************************************************
  *  Handle insertion when the list is not empty.                           *
  **************************************************************************/
  new_element->next = element->next;
  new_element->prev = element;
  if (element->next == NULL)
   list->tail = new_element;
  else
   element->next->prev = new_element;
  element->next = new_element;
 }
 /*****************************************************************************
 *  Adjust the size of the list to account for the inserted element.          *
 *****************************************************************************/
 list->size++;
 return 0;
}
/*****************************************************************************
*  ---------------------------- dlist_ins_prev ----------------------------  *
*****************************************************************************/
int dlist_ins_prev(DList *list, DListElmt *element, const void *data) {
 DListElmt          *new_element;
 /*****************************************************************************
 *  Do not allow a NULL element unless the list is empty.                     *
 *****************************************************************************/
 if (element == NULL && dlist_size(list) != 0)
  return -1;
 /*****************************************************************************
 *  Allocate storage to be managed by the abstract data type.                 *
 *****************************************************************************/
 if ((new_element = (DListElmt *)malloc(sizeof(DListElmt))) == NULL)
  return -1;
 /*****************************************************************************
 *  Insert the new element into the list.                                     *
 *****************************************************************************/
 new_element->data = (void *)data;
 if (dlist_size(list) == 0) {
  /**************************************************************************
  *  Handle insertion when the list is empty.                               *
  **************************************************************************/
  list->head = new_element;
  list->head->prev = NULL;
  list->head->next = NULL;
  list->tail = new_element;
 }
 else {
  /**************************************************************************
  *  Handle insertion when the list is not empty.                           *
  **************************************************************************/
  new_element->next = element; 
  new_element->prev = element->prev;
  if (element->prev == NULL)
   list->head = new_element;
  else
   element->prev->next = new_element;
  element->prev = new_element;
 }
 /*****************************************************************************
 *  Adjust the size of the list to account for the new element.               *
 *****************************************************************************/
 list->size++;
 return 0;
}
/*****************************************************************************
*  ----------------------------- dlist_remove -----------------------------  *
*****************************************************************************/
int dlist_remove(DList *list, DListElmt *element, void **data) {
 /*****************************************************************************
 *  Do not allow a NULL element or removal from an empty list.                *
 *****************************************************************************/
 if (element == NULL || dlist_size(list) == 0)
  return -1;
 /*****************************************************************************
 *  Remove the element from the list.                                         *
 *****************************************************************************/
 *data = element->data;
 if (element == list->head) {
  /**************************************************************************
  *  Handle removal from the head of the list.                              *
  **************************************************************************/
  list->head = element->next;
  if (list->head == NULL)
   list->tail = NULL;
  else
   element->next->prev = NULL;
 }
 else {
  /**************************************************************************
  *  Handle removal from other than the head of the list.                   *
  **************************************************************************/
  element->prev->next = element->next;
  if (element->next == NULL)
   list->tail = element->prev;
  else
   element->next->prev = element->prev;
 }
 /*****************************************************************************
 *  Free the storage allocated by the abstract data type.                     *
 *****************************************************************************/
 free(element);
 /*****************************************************************************
 *  Adjust the size of the list to account for the removed element.           *
 *****************************************************************************/
 list->size--;
 return 0;
}



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

이중연결리스트 노드 cpp  (0) 2016.12.01
이중연결리스트 노드클래스  (0) 2016.12.01
단일연결리스트.cpp  (0) 2016.11.30
단일연결리스트.h  (0) 2016.11.30
이중연결리스트  (0) 2016.11.30

/*****************************************************************************
*  ------------------------------- dlist.h --------------------------------  *
*****************************************************************************/
#ifndef DLIST_H
#define DLIST_H
#include <stdlib.h>
/*****************************************************************************
*  Define a structure for doubly-linked list elements.                       *
*****************************************************************************/
typedef struct DListElmt_ {
 void               *data;
 struct DListElmt_  *prev;
 struct DListElmt_  *next;
} DListElmt;
/*****************************************************************************
*  Define a structure for doubly-linked lists.                               *
*****************************************************************************/
typedef struct DList_ {
 int                size;
 int                (*match)(const void *key1, const void *key2);
 void               (*destroy)(void *data);
 DListElmt          *head;
 DListElmt          *tail;
} DList;
/*****************************************************************************
*  --------------------------- Public Interface ---------------------------  *
*****************************************************************************/
void dlist_init(DList *list, void (*destroy)(void *data));
void dlist_destroy(DList *list);
int dlist_ins_next(DList *list, DListElmt *element, const void *data);
int dlist_ins_prev(DList *list, DListElmt *element, const void *data);
int dlist_remove(DList *list, DListElmt *element, void **data);
#define dlist_size(list) ((list)->size)
#define dlist_head(list) ((list)->head)
#define dlist_tail(list) ((list)->tail)
#define dlist_is_head(element) ((element)->prev == NULL ? 1 : 0)
#define dlist_is_tail(element) ((element)->next == NULL ? 1 : 0)
#define dlist_data(element) ((element)->data)
#define dlist_next(element) ((element)->next)
#define dlist_prev(element) ((element)->prev)
#endif



//******************************************************************************
// 이중연결리스트 !!
//******************************************************************************
// 단일연결리스트 -> 단방향성
//*****************************5*************************************************
#include <stdio.h>
#include <malloc.h>
//******************************************************************************
// 구조체 선언
//******************************************************************************
typedef struct _Node
{
int data; // data
struct _Node * next; //다음 링크
struct _Node * prev; //이전 링크
}Node;
//******************************************************************************
//head , tail 더미 노드 활용 !!
//******************************************************************************
Node *head;
//******************************************************************************
// Utill
//******************************************************************************
Node *CreateNode(int data) //노드 생성
{
Node *Newnode = (Node *)malloc(sizeof(Node));
Newnode->data = data;
Newnode->next = NULL;
Newnode->prev = NULL;
return Newnode;
}
//******************************************************************************
void init()
{
head = CreateNode(0);
head->next = NULL;
head->prev = head;
}
//******************************************************************************
// 삽입연산 함수들
//******************************************************************************
void push_back(int data)
{
Node *Newnode = CreateNode(data);
Newnode->next = NULL;
tail->prev->next = Newnode;
Newnode->prev = tail->prev;
tail->prev = Newnode;
}
void push_front(int data)
{
Node * Newnode = CreateNode(data);
Newnode->prev = head;
head->next->prev = Newnode;
Newnode->next = head->next;
head->next = Newnode;
}
void insert(int key, int data)
{
Node *p = head->next;
Node *Newnode = CreateNode(data);
while(p != tail){
if(p->data == key){
Newnode->next = p->next;
Newnode->prev = p;
p->next->prev = Newnode;
p->next = Newnode;
return;
}
else{
p = p-> next;
}
}
Newnode->next = tail;
tail->prev->next = Newnode;
Newnode->prev = tail->prev;
tail->prev = Newnode;
}
//******************************************************************************
//삭제연산 함수들
//******************************************************************************
void pop_back()
{
Node *p = tail->prev;
if(tail->prev == head){
printf("list empty !!!");
}
else{
p->prev->next = tail;
tail->prev = p->prev;
free(p);
}
}
void pop_front()
{
Node *p = head->next;
if(head->next == tail){
printf("list empty");
}
else{
head->next = p->next;
free(p);
}
}
void erase(int key)
{
Node *p = head->next;
Node *s;
while(p != tail){
if(p->data == key){
p->prev->next = p->next;
p->next->prev = p->prev;
s = p->next;
free(p);
p = s;
//return;
}
else{
p = p-> next;
}
}
}
//******************************************************************************
void show()
{
Node *p = head->next;
while(p != tail)
{
printf("%d-->", p->data); 
p = p->next;
}
puts("");
}


void main()
{
init();
push_back(30);
show();
push_back(50);
show();
push_front(30);
show();
push_front(30);
show();
push_front(50);
show();
push_front(30);
show();
erase(30);
show();
}


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

단일연결리스트.cpp  (0) 2016.11.30
단일연결리스트.h  (0) 2016.11.30
단일연결리스트 head만 사용  (0) 2016.11.30
단일 연결리스트 더미노드  (0) 2016.11.30
자료구조  (0) 2016.11.30

+ Recent posts