'자료구조'에 해당되는 글 3건

  1. 2014.11.14 linked list
  2. 2014.10.24 배열 스택
  3. 2014.10.23 스트링 결합연산

#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 기술 블로그

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

#define MAXSTACK 10

typedef struct stack
{
    char *data[MAXSTACK];
    int top;
} STACK;


void CheckStack(STACK *tmpStack);
char *PopStack(STACK *tmpStack);
void PushStack(STACK *tmpStack);
void PrintStack(STACK *tmpStack);

int main()
{
    STACK *MainStack;
    int iCount = 0;
    char checkMet;
    MainStack = (STACK*)malloc(sizeof(STACK));
    MainStack->top = -1;
    while (iCount < MAXSTACK)
    {
        MainStack->data[iCount] = NULL;
        iCount++;
    }
    while (1)
    {
        system("cls");

        printf("1. push  \n");
        printf("2. pop   \n");
        printf("3. print  \n");
        printf("4. end  \n");

        checkMet = getc(stdin);
        fflush(stdin);
        if (checkMet == '1')
        {
            printf("값을 입력해주세요 : ");
            PushStack(MainStack);

        }
        else if (checkMet == '2')
        {
            printf("POP한 항목은 : %s\n", PopStack(MainStack));
        }
        else if (checkMet == '3')
        {
            PrintStack(MainStack);
        }
        else if (checkMet == '4')
        {
            return 0;
        }
        else
        {
            printf("입력값이 잘못되었습니다");
        }
        system("pause");

    }


}

void CheckStack(STACK *tmpStack)
{
    if (tmpStack->top == -1)
    {
        printf("Stack is empty");
    }
    else
    {
        printf("Stack is not empty");
    }
}

char *PopStack(STACK *tmpStack)
{
    char *tmpString;
    if (tmpStack->top == -1)
    {
        printf("Stack is underflow");
        exit(1);
    }

    tmpString = tmpStack->data[tmpStack->top];
   
    //프린트 했을 때 보기 좋으라고 NULL 지정
    //본래라면 그냥 덮어씌어지고, 스택포인트 때문에 전혀 상관없음;
    tmpStack->data[tmpStack->top] = NULL;
    tmpStack->top--;

    return tmpString;
}

void PushStack(STACK *tmpStack)
{
    int iBuf;
    int iLen = 1;
    char *tmpString = (char*)malloc(sizeof(char));

    if (tmpStack->top > MAXSTACK-1)
    {
        printf("Stack is overflow");
        exit(1);
    }

    while ((iBuf = fgetc(stdin)) != EOF && iBuf != '\n')
    {
        tmpString[iLen - 1] = iBuf;

        tmpString = (char*)realloc(tmpString, (++iLen));
    }
    tmpString = (char*)realloc(tmpString, (--iLen));
    tmpString[iLen] = '\0';
   

    tmpStack->data[++tmpStack->top] = tmpString;

}

void PrintStack(STACK *tmpStack)
{
    int iCount = 9;
    while (iCount > -1)
    {
        printf("[%d] 번째 값 : %s\n", iCount, tmpStack->data[iCount]);
        iCount--;
       
    }
}





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

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

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


char *concate(char *s1, char *s2);
int main()
{
    char *s1 = "aaa";
    char *s2 = "bbb";
    char *s3;

    s3 = concate(s1, s2);
    printf("%s\n", s3);
}

char *concate(char *s1, char *s2)
{
    char *s3;
    int len1, len2, len3, i;

    len1 = strlen(s1);
    len2 = strlen(s2);
    len3 = len1 + len2;

    s3 = (char*)malloc(len3 + 1);

    i = 0;

    while (*s1!='\0')
    {
        s3[i++] = *s1;
        s1++;
    }

    while (*s2 != '\0')
    {
        s3[i++] = *s2;
        s2++;
    }
    s3[i] = '\0';
    return s3;
}

'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 기술 블로그