数据结构线性表---单链表

AfxStd.h

#pragma once
#ifndef AFXSTD_H
#define AFXSTD_H
#include 
#include 
#include
#endif // !AFXSTD_H

List.h

#pragma once
#include "AfxStd.h"
#ifndef LIST_H
#define LIST_H
typedef int ElemType;
typedef struct ListNode {                /*typedef与struct同时定义新类型*/
	ElemType data;
	ListNode *next;
}LNode, *LinkList;
struct List {                            /*存储链表的头结点,以及链表的数据元素个数*/
	ListNode *head;
	int cursize;
};
ListNode *BuyNode();
void InitList(List &L);
bool push_front(List &L, ElemType e);
bool push_back(List &L, ElemType e);
bool ListInsert(List &L, int pos, ElemType e);
bool ListDelect(List &L, int pos);
bool pop_front(List &L);
bool pop_back(List &L);
int GetLength(List &L);
bool EmptyList(List &L);
bool FullList(List &L);
void PrintfList(List &L);
void InsertSort(List &L);
LNode * FindAdress(List &L, int pos);
LNode *FindValue(List &L, ElemType e);
LNode *PrevFindValue(List &L, ElemType e);
void ClearList(List &L);
void Destory(List &L);
bool Remove(List &L, ElemType e);
void RemoveAll(List &L, ElemType e);
bool IsExist(List &L, ElemType e);
void Reserve(List &L);
LNode* Reserves(List &L);
void UnionList(List &L, List &S, List &F);
#endif // !LIST_H

List.cpp

#include"List.h"

ListNode *BuyNode()
{
	ListNode *s = (ListNode *)malloc(sizeof(ListNode));
	if (s == NULL)
	{
		printf("申请空间不成功\n");
		exit(1);
	}
	memset(s, 0, sizeof(LNode));
	return s;
}
void InitList(List &L)
{
	L.head = BuyNode();
	L.cursize = 0;
}

bool push_front(List &L,ElemType e)
{
	//ListInsert(L,1, e);
	LNode *newNode = BuyNode();
	newNode->data = e;
	L.head->next = newNode;
	newNode->next = L.head->next;
	++L.cursize;
	return true;
}
bool push_back(List &L, ElemType e)
{
	ListInsert(L,L.cursize+1, e);
	/*LNode *p = L.head;
	
	int length = L.cursize;
	while (length--)
	{
		p = p->next;
	}
	
	while (p->next!=NULL)
	{
		p = p->next;
	}
	p = p->next = BuyNode();
	p->data = e;
	++L.cursize;
	*/
	return true;
}
bool ListInsert(List &L, int pos, ElemType e)
{
	if (pos<1 || pos>L.cursize + 1||L.head==NULL)
	{
		printf("error");
		return false;
	}
	LNode *p = L.head;
	while (1 != pos--)                     /*先参与运算再自减*/
	{
		p = p->next;
	}
	LNode *newNode = BuyNode();
	newNode->data = e;
	newNode->next = p->next;
	p->next = newNode;
	L.cursize++;
	return true;
}
bool ListDelect(List &L, int pos)
{
	if (pos<1 || pos>L.cursize)
	{
		printf("删除位置有误!");
		return false;
	}
	LNode *p = L.head;
	while (p->next!=NULL&&p->next->next!=NULL)                     /*先参与运算再自减*/
	{
		p = p->next;
	}
	LNode *s = p->next;
	p->next = s->next;
	L.cursize--;
	free(s);
	return true;
}
bool pop_front(List &L)
{
	ListDelect(L,1);
	return true;
}
bool pop_back(List &L)
{
	ListDelect(L,L.cursize);
	return true;
}
int GetLength(List &L)
{
	return L.cursize;
}
bool EmptyList(List &L)
{
	return GetLength(L) == 0;
}

void PrintfList(List &L)
{
	LNode *p = L.head->next;
	while(p!=NULL)
	{ 
		printf("%d\t",p->data);
		p = p->next;
	}
}
void InsertSort(List &L)
{
	printf("%s","sqsqw");
	for (int i = 0; i < L.cursize;i++)
	{
		LNode *p = L.head;
	    while (p->next!=NULL&&p->next->next!=NULL)
	{
		LNode *s = p->next;
		LNode *r = s->next;
		if (s->data>r->data)
		{
			s->next = r->next;
			p->next = r;
			r->next = s;
			
		}
		p = p->next;
	}
	
	}
}

LNode * FindAdress(List &L, int pos)
{

	if (pos<1 || pos>L.cursize + 1)
	{
		printf("查询位置有误");
		return NULL;
	}
	LNode *p = L.head;
	while (1 == pos--)                     /*先参与运算再自减*/
	{
		p = p->next;
	}
	LNode *e = p->next;
	return e;
}
LNode *FindValue(List &L, ElemType e)
{
	LNode *p = L.head->next;
	while (p != NULL && p->data != e)
	{
		p = p->next;
	}
	return p;
}

LNode *PrevFindValue(List &L, ElemType e)
{
	LNode *p = L.head;
	while (p->next!=NULL&&p->next->data!=e)
	{
		p = p->next;
	}
	if (p->next==NULL)
	{
		p = NULL;
	}
	return p;
}

void ClearList(List &L)
{
	LNode *p = L.head;
	while (p->next != NULL)
	{
		LNode *s = p->next;	
		p->next = s->next;
		free(s);
	}
	L.cursize = 0;
}

void Destory(List &L)
{
	ClearList(L);
	free(L.head);
	L.head = NULL;
}

bool Remove(List &L, ElemType e)
{
	LNode *p = PrevFindValue(L,e);
	bool res = false;
	if (p!=NULL)
	{
		LNode *q = p->next;
		p->next = q->next;
		free(q);
		L.cursize--;
		res = true;
	}
	return res;
}

void RemoveAll(List &L, ElemType e)
{
	LNode *p = L.head;
	while (p->next != NULL)
	{
		if (!Remove(L, e))
		{
			p = p->next;
		}
	}
}

bool IsExist(List &L, ElemType e)
{
	int length = L.cursize;
	LNode *p = L.head;
	while (1 == length--)                     /*先参与运算再自减*/
	{
		if (p->next->data == e)
		{
			return true;
		}
	}
	return false;
}

void Reserve(List &L)
{
	if (L.cursize<2)
	{
		return;
	}
	LNode *p = L.head->next;

	L.head->next = NULL;
	while(p!=NULL)
	{

		LNode*r = p;
		p = p->next;
	
		r->next = L.head->next;
		L.head->next = r;
	}
}
LNode* Reserves(List &L)               /*原地爆炸*/
{
	LNode *pNode = L.head;
	LNode *pPrev = NULL;
	LNode *pReserver = NULL;
		while (pNode!=NULL)
	{
		LNode *pNext = pNode->next;
		if (pNext != NULL)
		{
			pReserver = pNode;
			pNode->next = pPrev;
			pPrev = pNode;
			pNode = pNext;
		}
	}
		return pReserver;
}
void UnionList(List &L,List &S,List &F)
{
	LNode *l = L.head->next;
	LNode *s = S.head->next;
	LNode *f = F.head->next;

	while (l!=NULL&&s!=NULL)
	{
		if (l->data <= s->data)
		{
			push_back(F,l->data);
			l = l->next;
		}
		else
		{
			push_back(F, s->data);
			s = s->next;
		}
		f = f->next;
	}
	while (l!=NULL)
	{
		push_back(F, l->data);
		l = l->next;
		f = f->next;
	}
	while (s != NULL)
	{
		push_back(F, s->data);
		s = s->next;
		f = f->next;
	}
}


你可能感兴趣的:(数据结构,C语言)