단 방향 링크 의 기본 조작 및 역순 실현

5486 단어
<pre name="code" class="cpp">#include "stdio.h"
#include "string.h"
#include "stdlib.h"
//                       
typedef struct Node
{
	int data;
	struct Node *next;
}SLIST;


//    SList_Creat,            。      ,
//              , -1        。              。

SLIST *SList_Creat();
int SList_Print(SLIST *pHead);
int SList_NodeInsert(SLIST *pHead, int x, int y);
int SList_NodeDel(SLIST *pHead, int x);
int SList_Destory(SLIST *pHead);
int SList_Resve(SLIST *pHead);

/
SLIST *SList_Creat()
{
	SLIST *pHead = NULL, *pM =NULL, *pCur = NULL;
	int data = 0;
	//1          
	pHead = (SLIST *)malloc(sizeof(SLIST));
	if (pHead == NULL)
	{
		return NULL;
	}
	pHead->data = 0;
	pHead->next = NULL;

	//2        ,      
	printf("
please enter the data of node(-1:quit) "); scanf("%d", &data); //3 // , pCur = pHead; while(data != -1) { // //1 malloc ===PM pM = (SLIST *)malloc(sizeof(SLIST)); if (pM == NULL) { SList_Destory(pHead); // return NULL; } pM->data = data; pM->next = NULL; //2、 pM pCur->next = pM; //3 pM pCur = pM; //pCur = pCur->next; //2 , printf("
please enter the data of node(-1:quit) "); scanf("%d", &data); } return pHead; } // int SList_Creat2(SLIST **mypHead) { int ret = 0; SLIST *pHead = NULL, *pM =NULL, *pCur = NULL; int data = 0; //1 pHead = (SLIST *)malloc(sizeof(SLIST)); if (pHead == NULL) { ret = -1; printf("func SList_Creat2() err:%d ", ret); return ret; } pHead->data = 0; pHead->next = NULL; //2 , printf("
please enter the data of node(-1:quit) "); scanf("%d", &data); //3 // , pCur = pHead; while(data != -1) { // //1 malloc ===PM pM = (SLIST *)malloc(sizeof(SLIST)); if (pM == NULL) { SList_Destory(pHead); // ret = -2; printf("func SList_Creat2() err:%d ", ret); return ret; } pM->data = data; pM->next = NULL; //2、 pM pCur->next = pM; //3 pM pCur = pM; //pCur = pCur->next; //2 , printf("
please enter the data of node(-1:quit) "); scanf("%d", &data); } *mypHead = pHead; return ret; } int SList_Print(SLIST *pHead) { SLIST *p = NULL; if (pHead == NULL) { return -1; } p = pHead->next; printf("
Begin "); while(p) { printf("%d ", p->data); p = p->next; } printf(" End"); return 0; } int SList_Destory(SLIST *pHead) { SLIST *p = NULL, *tmp = NULL; if (pHead == NULL) { return -1; } p = pHead; while(p) { // tmp = p->next; free(p);// p = tmp; // } return 0; } // : x , y ; x , 。 int SList_NodeInsert(SLIST *pHead, int x, int y) { SLIST *pCur = NULL, *pPre = NULL, *pM = NULL; // y malloc pM = (SLIST *)malloc(sizeof(SLIST)); if (pM == NULL) { return -1; } pM->data = y; pM->next = NULL; // pCur Pre pPre = pHead; pCur = pHead->next; while (pCur) { if (pCur->data == x) { // break; } pPre = pCur; // pCur = pCur->next; // } //1 pM //pM->next = pCur; pM->next = pPre->next; //2 pM pPre->next = pM; return 0; } int SList_NodeDel(SLIST *pHead, int x) { SLIST *pCur = NULL, *pPre = NULL; // pCur Pre pPre = pHead; pCur = pHead->next; while (pCur) { if (pCur->data == x) { // break; } pPre = pCur; // pCur = pCur->next; // } if (pCur == NULL) { printf("
"); return -1; } // pPre->next = pCur->next; // free(pCur); return 0; } int SList_Resve(SLIST *pHead) { SLIST *t = NULL, *p = NULL, *q = NULL; if (pHead == NULL) { return -1; } if (pHead->next == NULL || pHead->next->next == NULL) { return -2; } // p = pHead->next; q = pHead->next->next; while (q != NULL) { // q t = q->next; // q->next = p; //3 //3 4 while p = q; //4 q = t; } // p 。q null pHead->next->next = NULL; //pHead->next pHead->next->next pHead->next = p; //p return 0; } void main() { int ret = 0; SLIST *pHead = NULL; pHead = SList_Creat(); ret = SList_Print(pHead); if (ret != 0) { printf("func SList_Print() err:%d
", ret); return ; } ret = SList_NodeInsert(pHead, 20, 19); if (ret != 0) { printf("func SList_NodeInsert() err:%d
", ret); return ; } ret = SList_Print(pHead); if (ret != 0) { printf("func SList_Print() err:%d
", ret); return ; } //SList_NodeInsert(pHead, 100, 99); //SList_Print(pHead); ret = SList_NodeDel(pHead, 19); if (ret != 0) { printf("func SList_NodeDel() err:%d
", ret); return ; } ret = SList_Print(pHead); if (ret != 0) { printf("func SList_Print() err:%d
", ret); return ; } ret = SList_Resve(pHead); ret = SList_Print(pHead); ret = SList_Destory(pHead); if (ret != 0) { printf("func SList_Destory() err:%d
", ret); return ; } system("pause"); }

좋은 웹페이지 즐겨찾기