Jupitor's Blog

[C언어] List 본문

IT/CㆍC++로 배우는 자료구조론 연습문제

[C언어] List

Jupitor6245 2018. 12. 4. 18:39

#include<stdio.h>

#define SIZE 10

#include <assert.h>

#include <stdlib.h>



typedef struct nodeRecord

{

int Data;

struct nodeRecord* next;

} node;

typedef node* Nptr;


typedef struct STRList {

int count;

STRData* header;

}List;


void adjList(List *x, int position, int value)

{

if (x->count == 0)

{

printf("No Data to adjust\n");

}

else if (position >= x->count)

{

printf("No data at position %d \n", position);

}

else

{

Data* p1 = x->header;


for (int i = 0; i < position - 1; i++)

p1 = p1->next;


p1->value = value;

}

}


void addList(List* x, int position, int value)

{

if (position <= 0)

{

printf("\n Wrong Position input");

return;

}

Data* p = (Data*)malloc(sizeof(Data));

p->next = NULL;

p->value = value;


if (x->count == 0)

{

x->header = p;

}

else if (position == 1)

{

Data* p1 = x->header;

x->header = p;

p->next = p1;

}

else if (position > x->count)

{

Data* p1 = x->header;

for (int i = 0; i < x->count - 1; i++)

{

p1 = p1->next;

}

p1->next = p;

}

else

{

Data* p1 = x->header;


for (int i = 0; i < position - 2; i++)

p1 = p1->next; 


p->next = p1->next;

p1->next = p;

}

x->count++;

}


void deleteList(List* x, int position)

{


if (x->count == 0)

{

printf("No Data to delete\n");

}

else if (position > x->count)

{

printf("No data at position %d \n", position);

}

else if (x->count == 1 && position == 1)

{

free(x->header);

x->header = NULL;

}

else if(position == 1 && x->count != 1)

{

Data* p1 = x->header->next;

free(x->header);

x->header = p1;

}

else

{

Data* p1 = x->header;

Data* p2 = x->header;

int i;

for (i = 1; i < position - 1; i++)

p1 = p1->next;


p2 = p1->next;


if (position == x->count)

{

p1->next = NULL;

free(p2);

}

else

{

p1->next = p2->next;

free(p2);

}

}

x->count--;

}


void deleteByValue(List* x, int value)

{

Data* p = x->header;

int i = 1;


while (p != NULL)

{

if (p->value == value)

{

printf("\n\n value of Position %d was Deleted \n\n", i);

deleteList(x, i);

return;

}

else

{

p = p->next;

i++;

}

}


printf("\n\n No Data Has the value %d In List \n\n", value);

}



void printList(List* x)

{

Data* p = x->header;

int i = 1;

printf("\n\n\n");

if (x->count == 0)

printf("No Data to display\n");

else

{

for (i = 0; i < x->count; i++)

{

printf("\n  Pisition %d : %d ", i + 1, p->value);

p = p->next;

}

}

printf("\n\n\n");

}


int main() {

List x;

List* x1 = &x;

char input;

int num;

int num_value;

x.count = 0;

x.header = NULL;


while (1)

{

printf("\n\n q : quit\n a : add list\n d : delete list\n j : adjust list value\n p : print list\n f : find value and delete \n=======================\n  your choice : ");

input = getchar();

if (input == 'q')

{

break;

}

else if (input == 'a')

{

printf("which position? : ");

scanf_s("%d", &num);

printf("what value position? : ");

scanf_s("%d", &num_value);

addList(x1, num, num_value);

}

else if (input == 'd')

{

printf("which position? : ");

scanf_s("%d", &num);

fflush(stdin);

deleteList(x1, num);

}

else if (input == 'p')

{

printList(x1);

}

else if (input == 'j')

{

printf("which position? : ");

scanf_s("%d", &num);

fflush(stdin);

printf("what value? : ");

scanf_s("%d", &num_value);

fflush(stdin);


adjList(x1, num, num_value);

}

else if (input == 'f')

{

printf("which value? : ");

scanf_s("%d", &num);


deleteByValue(&x, num);

}

getchar();

}


return 0;

}