순환 싱글 체인 시트 작업
35562 단어 재 작성 데이터 구조
//
//
//
// ,
typedef struct Node
{
int elem;
struct Node * next;
}clinklist, *pclinklist;
순환 단일 체인 시트 생 성
함수 성명
pclinklist clinklist_create(int size);
함수 실현
pclinklist clinklist_create(int size)
{
pclinklist node, head;
head = (pclinklist)malloc(sizeof(clinklist));
// elem , next
//
head->elem = size;
head->next = NULL;
node = head;
for (int i = 0; i < size; i++)
{
node->next = (pclinklist)malloc(sizeof(clinklist));
node = node->next;
node->elem = rand() % 100;
node->next = NULL;
}
node->next = head->next;
return head;
}
끼어들다
함수 성명
void clinklist_insert(pclinklist list, int locate, int elem);
함수 실현
void clinklist_insert(pclinklist plist, int location, int elem)
{
if (location < 0 || location > plist->elem) return;
pclinklist inode = (pclinklist)malloc(sizeof(clinklist));
inode->elem = elem;
inode->next = NULL;
pclinklist prev = clinklist_find(plist, location - 1);
pclinklist tail = clinklist_find(plist, plist->elem - 1);
inode->next = prev->next;
prev->next = inode;
if (location == 0)
tail->next = plist->next;
clinklist_update_head(plist);
}
삭제
함수 성명
void clinklist_delete(pclinklist list, int locate);
함수 실현
void clinklist_delete(pclinklist plist, int location)
{
if (location < 0 || location >= plist->elem) return;
pclinklist prev = clinklist_find(plist, location - 1);
pclinklist tail = clinklist_find(plist, plist->elem - 1);
pclinklist dnode = prev->next;
prev->next = prev->next->next;
free(dnode);
if (location == 0)
tail->next = plist->next;
clinklist_update_head(plist);
}
두루
함수 성명
void clinklist_display(pclinklist plist);
함수 실현
void clinklist_display(pclinklist plist)
{
pclinklist node = plist->next;
for (int i = 0; i < plist->elem; i++)
{
printf("%4d", node->elem);
node = node->next;
}
printf("
");
}
찾다
함수 성명
pclinklist clinklist_find(pclinklist list, int location);
함수 실현
// -1 header
pclinklist clinklist_find(pclinklist list, int location)
{
if (location < -1 || location >= list->elem) return NULL;
pclinklist node = list;
for (int i = -1; i < location; i++)
{
node = node->next;
}
return node;
}
전체 인 스 턴 스
파일
#pragma once
#ifndef CLINKLIST_H
#define CLINKLIST_H
//
//
//
// ,
typedef struct Node
{
int elem;
struct Node * next;
}clinklist, *pclinklist;
pclinklist clinklist_create(int size);
void clinklist_insert(pclinklist list, int locate, int elem);
void clinklist_delete(pclinklist list, int locate);
void clinklist_display(pclinklist list);
pclinklist clinklist_find(pclinklist list, int location);
int clinklist_is_same(pclinklist head, pclinklist node);
int clinklist_is_empty(pclinklist list);
int clinklist_size(pclinklist list);
void clinklist_update_head(pclinklist list);
#endif
원본 파일
#include
#include
#include "clinklist.h"
int main() {
int size;
int index = 0;
pclinklist list;
printf(" :");
scanf_s("%d", &size);
list = clinklist_create(size);
clinklist_display(list);
for (index = 0; index < size; index++)
{
pclinklist node = clinklist_find(list, index);
printf(" %d :%d
", index, node->elem);
}
printf("
5 2
");
clinklist_insert(list, 4, 2);
clinklist_display(list);
printf("
-1
");
clinklist_insert(list, clinklist_size(list), -1);
clinklist_display(list);
printf("
0 1111
");
clinklist_insert(list, 0, 1111);
clinklist_display(list);
printf("
3
");
clinklist_delete(list, 2);
clinklist_display(list);
printf("
1
");
clinklist_delete(list, 0);
clinklist_display(list);
printf("
");
clinklist_delete(list, clinklist_size(list) - 1);
clinklist_display(list);
return 0;
}
pclinklist clinklist_create(int size)
{
pclinklist node, head;
head = (pclinklist)malloc(sizeof(clinklist));
// elem , next
//
head->elem = size;
head->next = NULL;
node = head;
for (int i = 0; i < size; i++)
{
node->next = (pclinklist)malloc(sizeof(clinklist));
node = node->next;
node->elem = rand() % 100;
node->next = NULL;
}
node->next = head->next;
return head;
}
void clinklist_display(pclinklist plist)
{
pclinklist node = plist->next;
for (int i = 0; i < plist->elem; i++)
{
printf("%4d", node->elem);
node = node->next;
}
printf("
");
}
void clinklist_insert(pclinklist plist, int location, int elem)
{
if (location < 0 || location > plist->elem) return;
pclinklist inode = (pclinklist)malloc(sizeof(clinklist));
inode->elem = elem;
inode->next = NULL;
pclinklist prev = clinklist_find(plist, location - 1);
pclinklist tail = clinklist_find(plist, plist->elem - 1);
inode->next = prev->next;
prev->next = inode;
if (location == 0)
tail->next = plist->next;
clinklist_update_head(plist);
}
void clinklist_delete(pclinklist plist, int location)
{
if (location < 0 || location >= plist->elem) return;
pclinklist prev = clinklist_find(plist, location - 1);
pclinklist tail = clinklist_find(plist, plist->elem - 1);
pclinklist dnode = prev->next;
prev->next = prev->next->next;
free(dnode);
if (location == 0)
tail->next = plist->next;
clinklist_update_head(plist);
}
// -1 header
pclinklist clinklist_find(pclinklist list, int location)
{
if (location < -1 || location >= list->elem) return NULL;
pclinklist node = list;
for (int i = -1; i < location; i++)
{
node = node->next;
}
return node;
}
int clinklist_is_same(pclinklist lnode, pclinklist rnode)
{
return (lnode == rnode) ? 1 : 0;
}
int clinklist_is_empty(pclinklist list)
{
return list->next == NULL;
}
int clinklist_size(pclinklist list)
{
return list->elem;
}
void clinklist_update_head(pclinklist list)
{
pclinklist node = list->next;
int size = node == NULL ? 0 : 1;
while ((node = node->next) != list->next)
size++;
list->elem = size;
}