'Linked List'에 해당되는 글 4건

  1. 2014.11.14 linked list qeueu
  2. 2014.11.14 linked list 정렬입력
  3. 2014.11.14 linked list
  4. 2011.06.21 linked list 예제 코드

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

typedef struct node
{
    int iData;
    struct node *npLink;
} NODE;

typedef struct queue
{
    int iCount;
    struct node *npFront;
    struct node *npRear;
} QUEUE;


void insertQueue(QUEUE *qpQ, int iInData);
int deleteQueue(QUEUE *qpQ);
void printQeueu(QUEUE *qpQ);


int main()
{

    QUEUE *qpQueue = (QUEUE*)malloc(sizeof(QUEUE));

    qpQueue->iCount = 0;
    qpQueue->npFront = NULL;
    qpQueue->npRear = NULL;

   
    insertQueue(qpQueue, 1);
    printf("데이터의 갯수 : %d\n", qpQueue->iCount);
    insertQueue(qpQueue, 2);
    printf("데이터의 갯수 : %d\n", qpQueue->iCount);
    insertQueue(qpQueue, 3);
    printf("데이터의 갯수 : %d\n", qpQueue->iCount);

    printQeueu(qpQueue);
   
    printf("나온값은 : %d\n", deleteQueue(qpQueue));
    printf("데이터의 갯수 : %d\n", qpQueue->iCount);
    printQeueu(qpQueue);

    insertQueue(qpQueue, 4);

    printQeueu(qpQueue);

    printf("나온값은 : %d\n", deleteQueue(qpQueue));
    printf("데이터의 갯수 : %d\n", qpQueue->iCount);
    printf("나온값은 : %d\n", deleteQueue(qpQueue));
    printf("데이터의 갯수 : %d\n", qpQueue->iCount);

    printQeueu(qpQueue);

    printf("나온값은 : %d\n", deleteQueue(qpQueue));
    printf("데이터의 갯수 : %d\n", qpQueue->iCount);
    printQeueu(qpQueue);


    return 0;

}

void insertQueue(QUEUE *qpQ, int iInData)
{
    NODE *npNewNode = (NODE*)malloc(sizeof(NODE));
   
   
    npNewNode->iData = iInData;
    npNewNode->npLink = NULL;

    if (qpQ->iCount == 0)
    {
        qpQ->npFront = npNewNode;
        qpQ->npRear = npNewNode;
    }
    else
    {
        qpQ->npRear->npLink = npNewNode;
        qpQ->npRear = npNewNode;
    }
    qpQ->iCount++;

   
}
int deleteQueue(QUEUE *qpQ)
{
    NODE *npCurNode = NULL;
    int iReData;
   
    if (qpQ->iCount == 0)
    {
        printf("No Data!\n");
        exit(1);
    }
    else
    {
        npCurNode = qpQ->npFront;
        iReData = npCurNode->iData;
        qpQ->npFront = npCurNode->npLink;
        free(npCurNode);
        qpQ->iCount--;
        return iReData;
    }
}

void printQeueu(QUEUE *qpQ)
{
    NODE *npCurNode = NULL;
   
    if (qpQ->iCount == 0)
    {
        printf("No Data!\n");
        exit(1);
    }
    else
    {
        npCurNode = qpQ->npFront;
        while (npCurNode != NULL)
        {
            printf("%d => ", npCurNode->iData);
            npCurNode = npCurNode->npLink;
        }
        printf("NULL\n");

    }
   
}

'IT 전공지식 > 자료구조(Data Structure)' 카테고리의 다른 글

linked list 정렬입력  (0) 2014.11.14
linked list  (0) 2014.11.14
circular queue  (0) 2014.11.07
배열 스택  (0) 2014.10.24
서브스트링 연산  (0) 2014.10.23
Posted by ICT 기술 블로그

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


typedef struct node
{
    int iData;
    struct node *nLink;
} NODE;


NODE *insertNode(NODE *t);
NODE *deleteNode(NODE *t);
void printNode(NODE *t);

int main()
{
    NODE *nodelink = NULL;
    int selectItem;
    while (1)
    {
        system("cls");
        printf("1.데이터 삽입\n");
        printf("2.데이터 삭제\n");
        printf("3.데이터 출력\n");
        printf("4.종료\n");


        scanf_s("%d", &selectItem);

        switch (selectItem)
        {
        case 1:
            nodelink = insertNode(nodelink);
            break;
        case 2:
            nodelink= deleteNode(nodelink);
            break;
        case 3:
            printNode(nodelink);
            break;
        case 4:
            return 0;
        default:
            printf("잘못눌렸습니다\n");
            break;
        }
        system("pause");
    }
   

   
    return 0;
}

NODE *insertNode(NODE *t)
{
    NODE *curNode = t;
    NODE *folNOde = t;
    NODE *newNode = NULL;

    newNode = (NODE*)malloc(sizeof(NODE));
    printf("데이터를 입력하시오 : ");
    scanf_s("%d", &newNode->iData);
    newNode->nLink = NULL;
   
    while ((curNode != NULL) && (curNode->iData < newNode->iData))
    {
        folNOde = curNode;
        curNode = curNode->nLink;
    }

    newNode->nLink = curNode;

    if (curNode == t)
    {
        t = newNode;
    }
    else
    {
        folNOde->nLink = newNode;
    }
   

    return t;
}

NODE *deleteNode(NODE *t)
{
    NODE *curnode, *follnode;
    int iFindData;

    printf("삭제할 데이터를 입력하시오 : ");
    scanf_s("%d", &iFindData);
    curnode = t;
    follnode = t;

    while ((curnode != NULL) && (curnode->iData != iFindData))
    {
        follnode = curnode;
        curnode = curnode->nLink;
    }
   
    if (curnode == NULL)
    {
        printf("삭제할 데이터가 없습니다.\n");
        return t;
    }
    else if (curnode == t)
    {
        t = curnode->nLink;
    }
    else if (curnode->nLink == NULL)
    {
        follnode->nLink = NULL;
    }
    else
    {
        follnode->nLink = curnode->nLink;
    }
    free(curnode);
    return t;
}

void printNode(NODE *t)
{
    NODE * tempNode = t;

    if (tempNode == NULL)
    {
        printf("출력할 데이터가 없습니다 \n");
        return;
    }

    while (tempNode != NULL)
    {
        printf( "%d => ", tempNode->iData);
        tempNode = tempNode->nLink;

    }
    printf("NULL\n");
}

'IT 전공지식 > 자료구조(Data Structure)' 카테고리의 다른 글

linked list qeueu  (0) 2014.11.14
linked list  (0) 2014.11.14
circular queue  (0) 2014.11.07
배열 스택  (0) 2014.10.24
서브스트링 연산  (0) 2014.10.23
Posted by ICT 기술 블로그

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


typedef struct node
{
    int iData;
    struct node *nLink;
} NODE;


NODE *insertNode(NODE *t);
NODE *deleteNode(NODE *t);
void printNode(NODE *t);

int main()
{
    NODE *nodelink = NULL;
    int selectItem;
    while (1)
    {
    printf("1.데이터 삽입\n");
    printf("2.데이터 삭제\n");
    printf("3.데이터 출력\n");
    printf("4.종료\n");


        scanf_s("%d", &selectItem);

        switch (selectItem)
        {
        case 1:
            nodelink = insertNode(nodelink);
            break;
        case 2:
            nodelink= deleteNode(nodelink);
            break;
        case 3:
            printNode(nodelink);
            break;
        case 4:
            return 0;
        default:
            printf("잘못눌렸습니다\n");
            break;
        }
    }
   

   
    return 0;
}

NODE *insertNode(NODE *t)
{
    NODE *curNode = t;
    NODE *newNode = NULL;

    newNode = (NODE*)malloc(sizeof(NODE));
    printf("데이터를 입력하시오 : ");
    scanf_s("%d", &newNode->iData);
    newNode->nLink = NULL;



    if(t==NULL)
    {
        t = newNode;
    }
    else
    {
        while (curNode->nLink != NULL)
        {
            curNode = curNode->nLink;
        }
        curNode->nLink = newNode;
    }
       
    return t;
}

NODE *deleteNode(NODE *t)
{
    NODE *curnode, *follnode;
    int iFindData;

    printf("삭제할 데이터를 입력하시오 : ");
    scanf_s("%d", &iFindData);
    curnode = t;
    follnode = t;

    while ((curnode != NULL) && (curnode->iData != iFindData))
    {
        follnode = curnode;
        curnode = curnode->nLink;
    }
   
    if (curnode == NULL)
    {
        printf("삭제할 데이터가 없습니다.\n");
        return t;
    }
    else if (curnode == t)
    {
        t = curnode->nLink;
    }
    else if (curnode->nLink == NULL)
    {
        follnode->nLink = NULL;
    }
    else
    {
        follnode->nLink = curnode->nLink;
    }
    free(curnode);
    return t;
}

void printNode(NODE *t)
{
    NODE * tempNode = t;

    if (tempNode == NULL)
    {
        printf("출력할 데이터가 없습니다 \n");
        return;
    }

    while (tempNode != NULL)
    {
        printf( "%d => ", tempNode->iData);
        tempNode = tempNode->nLink;

    }
    printf("NULL\n");
}

'IT 전공지식 > 자료구조(Data Structure)' 카테고리의 다른 글

linked list qeueu  (0) 2014.11.14
linked list 정렬입력  (0) 2014.11.14
circular queue  (0) 2014.11.07
배열 스택  (0) 2014.10.24
서브스트링 연산  (0) 2014.10.23
Posted by ICT 기술 블로그
기초/C2011. 6. 21. 17:39


'기초 > C' 카테고리의 다른 글

파일입출력  (0) 2011.07.28
Stack  (0) 2011.07.18
malloc  (0) 2011.06.21
구조체 배열 최적화  (0) 2011.06.21
return exit  (0) 2011.06.21
Posted by ICT 기술 블로그