단일 체인 테이블 의 관련 조작
25914 단어 싱글 체인 리스트
#ifndef _SLIST_H
#define _SLIST_H
#ifdef __cplusplus
extern "C" {
#endif
/*******1. *****/
/***** *@ *@ m_data: *@m_pNext: ***/
struct listNode
{
int m_data;
listNode* m_pNext;
};
/******* *@ array , n *@ *@auther:spf *@version 1.0 *@data:2015.7.18 */
listNode* buildList(int* array, int n);
/*************** *@ *@pHead: *@value: */
void AddDataToTrial(listNode** pHead, int value);
/***************** * *pHead : *@ *@ */
bool insertList(listNode** pHead, int index, int value);
/***************** * *pHead : *@ */
bool deleteList(listNode** pHead, int index);
/****************** * @ pHead */
bool printList(listNode* pHead);
/*******2. *****/
/****** * * &pHead **/
bool initList(listNode* &pHead);
/****** * * &pHead **/
bool BuildList(listNode* &pHead,int *array,int n);
/****** * * &pHead **/
bool clearList(listNode* &pHead);
/****** * * &pHead **/
int lengthOfList(listNode* &pHead);
/****** * , 1, 0 * &pHead **/
bool isEmptyOfList(listNode* &pHead);
/****** * , , NULL * &pHead **/
listNode* searchElement(listNode* &pHead,int x);
/****** * i(i>=0) , i , * i 0 i , NULL * &pHead **/
listNode* Locate(listNode* &pHead, int i);
/****** * value i(i>=1) , i , 0 , 1 * i 0 i , NULL * &pHead **/
bool Insert(listNode* &pHead,int i, int value);
/****** * i(i>=1) , i , 0 , 1, value * i 0 i , NULL * &pHead **/
bool Remove(listNode* &pHead, int i, int &value);
/****** * * &pHead **/
bool Print(listNode* &pHead);
/****** * * pHead **/
bool recursivePrint(listNode* first);
/****** * ( ) * &pHead **/
bool PrintListReverse(listNode* first);
/****** * ( ) * &pHead **/
bool PrintListReverseRecursive(listNode* first);
/****** * ( ) * &pHead **/
bool ReverseSlist(listNode*& pHead);
/****** * ( ) * &pHead **/
listNode* ReverseSlistRecursive(listNode* pHead);
#ifdef __cplusplus
}
#endif
#endif
#include "stdafx.h"
#include "slist.h"
#include <stack>
#include <stdlib.h>
#include <iostream>
/******* *****/
listNode* buildList(int* array, int n){
listNode*t, *pHead= new listNode();
pHead->m_data = array[0];
pHead->m_pNext = NULL;
t = pHead;//
for (size_t i = 1; i < n;++i)
{
listNode* pNode = new listNode();
t->m_pNext = pNode;
pNode->m_data = array[i];
pNode->m_pNext=NULL;//
t = pNode;
}
return pHead;//
}
void AddDataToTrial(listNode** pHead, int value)
{
listNode* pNew = new listNode();
pNew->m_data = value;
pNew->m_pNext = NULL;
if (*pHead == NULL)
*pHead = pNew;// ,
else
{
listNode* pNode = *pHead;//
while (pNode->m_pNext!=NULL)
{
pNode = pNode->m_pNext;
}
pNode->m_pNext = pNew;//
}
}
bool insertList(listNode** pHead, int index, int value)
{
if (index < 1)
{
std::cerr << " " << std::endl;
return 0;
}
else if (index == 1)
{
//
listNode* pNew = new listNode();
if (pNew == NULL)
{
std::cerr << " " << std::endl;
return 0;
}
pNew->m_data = value;
pNew->m_pNext =*pHead;
*pHead = pNew;
//
}
else
{
listNode *pNode = *pHead;//
while (pNode != NULL && 0 < index - 2)
{
pNode = pNode->m_pNext;
--index;
}
if (pNode==NULL&&(*pHead)!=NULL)
{
std::cerr << " , " << std::endl;
return 0;
}
else
{
//
listNode* pNew = new listNode();
if (pNew == NULL)
{
std::cerr << " " << std::endl;
return 0;
}
pNew->m_data = value;
//
pNew->m_pNext = pNode->m_pNext;
pNode->m_pNext = pNew;//
}
}
return 1;//
}
bool deleteList(listNode** pHead, int index)
{
if (index < 1)
{
std::cerr << " 1, !" << std::endl;
return 0;
}
if (index == 1)
{
listNode* pNode = *pHead;
*pHead = (*pHead)->m_pNext;
delete pNode;
pNode = NULL;
}
else
{
listNode* pNode = *pHead;
while (pNode != NULL && 0 < index - 2)// i-1
{
pNode = pNode->m_pNext;
--index;
}
if (pNode==NULL||pNode->m_pNext==NULL)
{
std::cerr << " , "<<std::endl;
return 0;
}
else
{
listNode *q = pNode->m_pNext;
pNode->m_pNext = q->m_pNext;
delete q;
q = NULL;
}
}
return 1;
}
bool printList(listNode* pHead)
{
listNode* pNode = pHead;
int i = 0;
while (pNode != NULL)
{
++i;
std::cout << " " << i << " :" << pNode->m_data << std::endl;
pNode = pNode->m_pNext;//
}
std::cout << " " << i << " " << std::endl;
return 1;
}
bool ReverseSlist(listNode*& pHead)
{
if(pHead==nullptr&&pHead->m_pNext==nullptr)//
return 0;
listNode* pre, *temp, *head;//pre temp ,head
pre = temp = nullptr;
head = pHead;
while (head!=nullptr)
{
temp = head;//
head = head->m_pNext;//
temp->m_pNext = pre;//
pre = temp;//
}
pHead = temp;//
if (temp == nullptr)
return 0;
return 1;
}
listNode* ReverseSlistRecursive(listNode* pHead)
{
if (pHead == nullptr || pHead->m_pNext == nullptr)//
return pHead;
else
{
listNode* newNode = ReverseSlistRecursive(pHead->m_pNext);
pHead->m_pNext->m_pNext = pHead;
pHead->m_pNext = nullptr;
return newNode;
}
}
/******* *****************/
bool initList(listNode* &pHead)
{
pHead = new listNode;
if (!pHead)
{
std::cerr << " " << std::endl;
return 0;
}
pHead->m_pNext = NULL;//
return 1;
}
bool BuildList(listNode* &pHead, int *array, int n)
{
listNode* p = pHead;
if (p == NULL)
{
std::cerr << " " << std::endl;
return 0;
}
for (int i = 0; i < n;i++)
{
listNode* pNew = new listNode;
pNew->m_data = array[i];
pNew->m_pNext = NULL;
p->m_pNext = pNew;
p = p->m_pNext;
}
return 1;
}
bool clearList(listNode* &pHead)
{
listNode* p;
while (pHead->m_pNext!=NULL)//
{
p = pHead->m_pNext;
pHead->m_pNext = p->m_pNext;
delete p;
}
return 1;
}
int lengthOfList(listNode* &pHead)
{
int cout = 0;
listNode* p = pHead->m_pNext;
while (p!=NULL)
{
++cout;
p = p->m_pNext;
}
return cout;
}
bool isEmptyOfList(listNode* &pHead)
{
return (pHead->m_pNext==NULL);
}
listNode* searchElement(listNode* &pHead, int x)
{
listNode* p = pHead->m_pNext;//
while (p != NULL&&p->m_data != x)
{
p = p->m_pNext;
}
return p;
}
listNode* Locate(listNode* &pHead, int i)
{
if (i < 0)
return NULL;
listNode* p = pHead;
int cout = 0;
while (p!=NULL&&cout<i)
{
p = p->m_pNext;
cout++;
}
return p;
}
bool Insert(listNode* &pHead,int i, int value)
{
listNode* p = pHead;
int cout = 0;
while (p!=NULL&&cout<i-1)
{
cout++;
p = p->m_pNext;
}
if (p == NULL||i<1)
{
std::cerr << " " << std::endl;
return 0;
}
listNode* pNew = new listNode();
pNew->m_data = value;
pNew->m_pNext = p->m_pNext;
p->m_pNext = pNew;
return 1;
}
bool Remove(listNode* &pHead, int i, int &value)
{
listNode* p = pHead;
int cout = 0;
while (p!=NULL&&cout<i-1)// i-1
{
p = p->m_pNext;
cout++;
}
if (p == NULL || p->m_pNext==NULL||i < 1)
{
std::cerr << " " << std::endl;
return 0;
}
listNode* d = p->m_pNext;// i
p->m_pNext = d->m_pNext;
value = d->m_data;
delete d;
d = NULL;
return 1;
}
bool Print(listNode* &pHead)
{
if (pHead == NULL||pHead->m_pNext==NULL)
{
std::cerr << " " << std::endl;
return 0;
}
listNode* p = pHead->m_pNext;
int cout = 0;
while (p!=NULL)
{
cout++;
std::cout << " " << cout << " " << p->m_data << std::endl;
p = p->m_pNext;
}
return 1;
}
bool recursivePrint(listNode* first)
{
if (first == NULL)
return 0;
std::cout << first->m_data << std::endl;
recursivePrint(first->m_pNext);
}
bool PrintListReverse(listNode* first)
{
listNode* p = first;
std::stack<listNode*> nodes;
while (p != NULL)
{
nodes.push(p);
p = p->m_pNext;
}
while (!nodes.empty())
{
p = nodes.top();
std::cout << p->m_data << std::endl;
nodes.pop();
}
return 1;
}
bool PrintListReverseRecursive(listNode* first)
{
listNode* p = first;
if (p != NULL)
{
if (p->m_pNext != NULL)
{
PrintListReverseRecursive(p->m_pNext);
}
std::cout << p->m_data << std::endl;
}
return 1;
}
// list.cpp : 。
//
#include "stdafx.h"
#include "slist.h"
#include <cstdlib>
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
int array[] = { 1, 2, 3,4 };
listNode* list = buildList(array,4);
//initList(list);
//BuildList(list,array, 4);
//1/
// 1. 1
/*listNode* list=new listNode(); list->m_data=0; list->m_pNext=NULL;*/
/*list=NULL; if(NULL==list) cout<<" "<<endl;*/
//insertList(&list,0,0);
//2. , 1
//insertList(&list, 2, 0);
//3. , 1
//insertList(&list, 1, 0);
//4. , 1
//insertList(&list, 1, 0);
//5. , +1
//insertList(&list, 2, 0);
//6. , +1,
//insertList(&list, 6, 5);
//Insert(list, 5, 5);
/*listNode* f = Locate(list, 5); if (f==NULL) std::cout << " "<< std::endl; else std::cout<<f->m_data<<std::endl;*/
//Print(list);
//recursivePrint(list->m_pNext);
//PrintListReverse(list);
//PrintListReverseRecursive(list);
//ReverseSlist(list);
listNode* list2 = ReverseSlistRecursive(list);
printList(list2);
system("pause");
return 0;
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
데이터 구조 상의 연습 (2) 싱글 체인 시트#include "stdafx.h" #include "stdlib.h" #include "malloc.h" #define TRUE 1 #define FALSE 0 #define OK 1 #define ERROR 0 ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.