C__단일 체인 표 의 실현 (첨삭 검사 수정)

5616 단어 데이터 구조
SList.h
#define _CRT_SECURE_NO_WARNINGS 1 
#pragma once  //             

#include 
#include 

typedef int SLDateType;
typedef struct SListNode {
	SLDateType date;
	struct SListNode* next;
}SListNode;

void SListPrintf(SListNode* pList);
//     
void SListPushBack(SListNode** ppList, SLDateType x);
void SListPushfront(SListNode** ppList, SLDateType x);
//     
void SListPopBack(SListNode** ppList);
void SListPopfront(SListNode** ppList);
//   x     
SListNode* SListFind(SListNode* pList, SLDateType x); 
//  pos       
void SListInsertAfter(SListNode* pos, SLDateType x); 
//   pos       
void SListEraseAfter(SListNode* pos);
//  
void SListDestory(SListNode** pList);



SList.c
#define _CRT_SECURE_NO_WARNINGS 1 
#include "SList.h"

//1->2->3->4
void SListPrintf(SListNode* pList)
{
	SListNode* cur = pList;
	while (cur != NULL) {
		printf("%d->", cur->date);
		cur = cur->next;
	}
	printf("NULL
"); } SListNode* BuySListNode(SLDateType x) { SListNode* newNode = (SListNode*)malloc(sizeof(SListNode)); newNode->date = x; newNode->next = NULL; return newNode; } void SListPushBack(SListNode** ppList, SLDateType x) { //1. //2. SListNode* newNode = BuySListNode(x); if (*ppList == NULL) { *ppList = newNode; } else { SListNode* tail = *ppList; while (tail->next != NULL) { tail = tail->next; } tail->next = newNode; } } void SListPushfront(SListNode** ppList, SLDateType x) { SListNode* newNode = BuySListNode(x); newNode->next = *ppList; *ppList = newNode; } //1->2->3->4 void SListPopBack(SListNode** ppList) { // , , , if (*ppList == NULL) { return; } else if ((*ppList)->next == NULL) { free(*ppList); *ppList == NULL; } else { SListNode* tail = *ppList; SListNode* prev = NULL; while (tail->next != NULL) { prev = tail; tail = tail->next; } free(tail); prev->next = NULL; } } void SListPopfront(SListNode** ppList) { if (*ppList == NULL) { return ; } else { SListNode* newhead = (*ppList)->next; free(*ppList); *ppList = newhead; } } SListNode* SListFind(SListNode* pList, SLDateType x) { if (pList == NULL) { return; } else { SListNode* cur = pList; while (cur) { if (cur->date == x) { return cur; } cur = cur->next; } } return NULL; } // find pos void SListInsertAfter(SListNode* pos, SLDateType x) { SListNode* next = pos->next; SListNode* newnode = BuySListNode(x); newnode->next = next; pos->next = newnode; } // pos 1 2 3 4 5 - 3 void SListEraseAfter(SListNode* pos) { if (pos->next) { SListNode* next = pos->next; pos->next = next->next; } else { return; } } void SListDestory(SListNode** pList) { if (*pList == NULL) { return; } else { while (*pList) { SListNode* next = (*pList)->next; free(*pList); *pList = next; } *pList = NULL; } } // // , // ---- // //typedef struct ListNode Node; //struct ListNode* reverseList(struct ListNode* head) //{ // Node* newhead = NULL; // Node* cur = head; // while (cur) // { // // cur // Node* next = cur->next; // cur->next = newhead; // newhead = cur; // cur = next; // } //} // K ---- //typedef struct ListNode { // int val; // struct ListNode* next; //}Node; //struct ListNode* mergeTwoLists(struct ListNode* l1, struct ListNode* l2) //{ // if (l1 == NULL && l2 != NULL) { // return l2; // } // // if (l2 == NULL && l1 != NULL) { // return l1; // } // Node* newhead = NULL, *tail = NULL; // while (l1 && l2) { // // 1 2 3 4 2 3 4 5 // if (l1->val <= l2->val) { // if (tail == NULL) { // tail = newhead = l1; // } // else { // tail->next = l1; // tail = l1; // } // l1 = l1->next; // } // else { // if (newhead == NULL) { // tail = newhead = l2; // } // else { // tail->next = l2; // tail = l2; // } // l2 = l2->next; // } // } // if (l1) { // tail->next = l1; // } // if (l2) { // tail->next = l2; // } // return newhead; //};

test.c
#define _CRT_SECURE_NO_WARNINGS 1 
#include "SList.h"

void test1()
{
	SListNode* s = NULL;
	SListPushBack(&s, 1);
	SListPushBack(&s, 2);
	SListPushBack(&s, 3);
	SListPushfront(&s, 0);

	SListPrintf(s);
	SListPopBack(&s);
	SListPrintf(s);
}

void test2()
{
	SListNode* s = NULL;
	SListPushBack(&s, 1);
	SListPushBack(&s, 2);
	SListPushBack(&s, 3);
	SListPushfront(&s, 0);

	SListPrintf(s);
	SListPopfront(&s);
	SListPopfront(&s);
	SListPopfront(&s);
	SListPrintf(s);
	SListPopfront(&s);
	SListPopfront(&s);
	SListPrintf(s);

}


void test3()
{
	SListNode* s = NULL;
	SListPushBack(&s, 1);
	SListPushBack(&s, 2);
	SListPushBack(&s, 3);
	SListPushfront(&s, 0);

	SListPrintf(s);
	SListNode* cur = SListFind(s, 2);
	cur->date = 30;
	SListPrintf(s);

}

void test4()
{
	SListNode* s = NULL;
	SListPushBack(&s, 1);
	SListPushBack(&s, 2);
	SListPushBack(&s, 3);
	SListPushfront(&s, 0);

	SListPrintf(s);
	SListNode* cur = SListFind(s, 0);

	SListInsertAfter(cur, 40);
	SListPrintf(s);

	SListEraseAfter(cur);
	SListEraseAfter(cur);

	SListPrintf(s);

	SListDestory(&s);
	SListPrintf(s);
}

int main()
{
	test4();


	system("pause");
	return 0;
}

좋은 웹페이지 즐겨찾기