[데이터 구조] 단일 체인 표 의 첨삭 검사
1948 단어 데이터 구조 (C 언어 판)
#include
#include
typedef struct LinkedList
{
int elem;
LinkedList * next;
}List,* PList;
PList CreateLinkedList(int size);
void DisplayLinkedList(PList L);
void InsertLinkedList(PList L, int locate, int elem);
void DeleteLinkedList(PList L,int locate);
void main()
{
int size;
PList L;
printf(" :");
scanf("%d",&size);
L = CreateLinkedList(size); //
DisplayLinkedList(L);
printf("
5 2
");
InsertLinkedList(L, 5, 2); //
DisplayLinkedList(L);
printf("
3
");
DeleteLinkedList(L, 3); // 3
DisplayLinkedList(L);
}
PList CreateLinkedList(int size)
{
PList P,L;
L = (PList)malloc(sizeof(List));
L->elem = 0;
L->next = NULL;
P = L;
for (int i = 0; i < size; i++)
{
P->next = (PList)malloc(sizeof(List)); //
P = P->next;
P->elem = rand()%100;
P->next = NULL;
}
return L;
}
void DisplayLinkedList(PList L)
{
PList P = L;
while (P->next != NULL)
{
P = P->next;
printf("%d\t",P->elem);
}
printf("
");
}
void InsertLinkedList(PList L, int locate, int elem) // Locate elem
{
PList P = L;
PList Q = NULL;
if (locate != 0)
locate--;
while (P->next != NULL && locate != 0)
{
locate--;
P = P->next;
}
if (locate != 0)
printf("
");
else
{
Q = (PList)malloc(sizeof(List)); //
Q->elem = elem;
Q->next = P->next;
P->next = Q;
}
}
void DeleteLinkedList(PList L,int locate)
{
PList P = L;
PList Q = NULL;
if (locate != 0)
locate--;
while (P->next != NULL && locate != 0)
{
locate--;
P = P->next;
}
if (locate != 0)
printf("
");
else
{
Q = P->next;
P->next = P->next ->next;
free(Q);
}
}