본문 바로가기

소프트웨어/C/C++

c++] 리스트 함수별로 나눈

#include<iostream>


using std::cin;

using std::cout;

using std::endl;

using std::bad_alloc;


struct msList{

char data[10];

struct msList* nList; //다음 노드의 주소값을 갖는다

};


struct msListHead{

int eNo;

msList headNode; //첫 노드의 주소값을 갖는다

};


msListHead* createList(){

struct msListHead* mListHead;

try{

mListHead=(msListHead*)malloc(sizeof(msListHead));

}catch(bad_alloc ex){

ex.what();

cout<<"메모리할당실패"<<endl;

return NULL;

}

//할당성공시

memset(mListHead,0,sizeof(msListHead) );


return mListHead;

}


msList* createNode(){

struct msList* msListNode;

try{

msListNode=(msList*)malloc(sizeof(msList));

}catch(bad_alloc ex){

ex.what();

cout<<"bad alloc"<<endl;

}

memset(msListNode,0,sizeof(msList));


return msListNode;

}



void addNodeAtLast(msListHead* head, msList* node){

struct msList* preNode=NULL;

struct msList* newNode=NULL;

preNode=&(head->headNode);

for(int i=0; i< head->eNo; i++){

preNode=preNode->nList;

}


try{

newNode=(msList*)malloc(sizeof(msList));

}catch(bad_alloc ex){

ex.what(); cout<<"bad alloc"<<endl;

}

memset(newNode,0,sizeof(msList) );

strcpy(newNode->data,node->data);

newNode->nList=NULL;


preNode->nList=newNode;


head->eNo++;

}


void deleteNodeAt(msListHead* head,int index){

if(index > (head->eNo)-1){

cout<<"잘못된 index "<<endl;

return;

}

struct msList* tempPtr=NULL;

struct msList* delPtr=NULL;

tempPtr=&(head->headNode); //mListHeader의 msList 헤더노드의 주소를 갖고옴

//만약 head->headNode.nList를 바로 tempPtr에 넣었으면

//지울려고하는 index까지 이동하게되서 그렇게 하면 안됨.

//직전까지만 가야함

for(int i=0; i<index;i++){ 

tempPtr=tempPtr->nList;

}


delPtr= tempPtr->nList;

tempPtr->nList=delPtr->nList;


free(delPtr);

(head->eNo)--;

}


void showListAt(msListHead* head,int index){

struct msList* currNode=NULL;


currNode=head->headNode.nList;

for(int i=0; i<index; i++){

currNode= currNode->nList;

}


cout<<currNode->data<<endl;

}


void showListAll(msListHead* head){

struct msList* currNode=NULL;


currNode=head->headNode.nList;

for(int i=0; i<=(head->eNo)-1; i++){

cout<<i<<"번. "<<currNode->data<<endl;

currNode=currNode->nList;

}

}


int main(void){

//header 생성

struct msListHead* mListHead=NULL;

mListHead=createList(); 

mListHead->headNode.nList=NULL;


/*

//Node생성

struct msList* msNode=NULL;

msNode=createNode();

strcpy(msNode->data,"kimmansung");


addNodeAtLast(mListHead,msNode);


strcpy(msNode->data,"kimmansu2ng");

addNodeAtLast(mListHead,msNode);*/

int menu=0;

while(1){

cout<<"1.노드추가, 2.노드삭제, 3.노드보기, 4.리스트전체출력 -1.종료"<<endl;

cin>>menu;

if(menu==-1){

break;

}else if(menu==1){

struct msList* msNode=NULL;

msNode=createNode();

cout<<"이름입력 : ";

cin>>msNode->data;

addNodeAtLast(mListHead,msNode);

}else if(menu==2){

int index=0;

cout<<"삭제할 index입력 : "; cin>>index;

deleteNodeAt(mListHead,index);

}else if(menu==3){

int index=0;

cout<<"index입력 : "; cin>>index;

if(index>(mListHead->eNo)-1){

cout<<"범위초과"<<endl;

continue;

}

showListAt(mListHead,index);


}else if(menu==4){

showListAll(mListHead);

}else{

cout<<"다시입력"<<endl;

}

}




return 0;

}