当前位置:文档之家› 数据结构链表c++实现

数据结构链表c++实现

实现了一些单链表操作以及几个算法,都已注明编译环境:VC6.0#ifndef COMMON //头文件common.h中的内容#define COMMON#include <stdio.h>#include <stdlib.h>#include <math.h>#include <conio.h>#include <string.h>#define OVERFLOW 0#define ERROR 0#define OK 1#define FALSE 0#define TRUE 0#define INFEASIBLE 0#define LIST_INIT_SIZE 100#define LISTINCREMENT 10typedef int Status;typedef int ElemType;/**链表存储结构**/typedef struct LNode{ElemType data;struct LNode *next;}LNode,*LinkList; //线性链表类型/******链表相关函数******/Status ListInit(LinkList &A){//初始化链表,分配头结点A=(LinkList)malloc(sizeof(LNode));A->next=NULL;A->data=0;return OK;}int ListLocate(LinkList A,ElemType x){//确定元素x在链表A中的位置,并返回所在结点位置LNode * p=A->next;int location=1;while(p!=NULL && p->data!=x){location++;p=p->next;}if(NULL==p)return ERROR;elsereturn location;}Status ListInsert(LinkList &A,ElemType x,int i){//向带头结点链表A中的i(从1开始)位置插入元素x if(i<=0)return INFEASIBLE;int k=1;LNode *p,*q=NULL;p = A;while(k<i&&p->next!=NULL){p = p->next;k++;}q = (LNode*)malloc(sizeof(LNode));q->data = x;q->next = p->next;p->next = q;return OK;}Status ListDelete(LinkList &A, int i){//删除带头结点的链表A的位置i(从1开始)处的结点if(i<=0)return INFEASIBLE;int k=1;LNode *p = A,*q = A->next;while(q!=NULL&&k<i){p=p->next;q=q->next;k++;}if(q==NULL){printf("位置%d处无结点,无法删除\n",i);return ERROR;}else{p->next=q->next;free(q);printf("删除成功\n");return OK;}}int ListLength(LinkList A){//求得带头结点单链表的长度LNode *p;int count=0;p=A->next;while(p!=NULL){p=p->next;count++;}return count;}void ListPrint(LinkList A){//打印链表所有节点LNode *p;p = A->next;while(p!=NULL){printf("%d\n",p->data);p = p->next;}}int ListCompare(LNode *p,LNode *q){//用于比较两个结点数据域大小的函数if(p->data>q->data)return 0;elsereturn 1;}Status ListSort(LinkList &A){//带头结点的单链表排序算法//利用一个简单的冒泡排序来实现LNode *q = A;LNode *max,*prior,*p,*k;while(q->next!=NULL){max=q->next;prior=max;k=max;p=max->next;while(p!=NULL){if(ListCompare(max,p)){max=p;prior=k;}k=p;p=p->next;}if(max==q->next){q=q->next;continue;}else{prior->next=max->next;max->next=q->next;q->next=max;q=q->next;}}return OK;}Status DeleteMinkMaxk(LinkList &A,int mink,int maxk){//删除递增有序单链表介于值mink与maxk之间的所有结点,并释放被删结点//时间复杂度<=o(n)LNode *p,*prior;p=A->next;prior=A;while(p!=NULL){if(p->data>mink&&p->data<maxk){prior->next=p->next;free(p);p=prior->next;}else{if(p->data<mink)break;prior=prior->next;p=prior->next;}}return OK;}Status DeleteRepeat(LinkList &A){LNode *prior=A->next,*p=A->next->next;while(p!=NULL){if(prior->data==p->data){prior->next=p->next;free(p);p=prior->next;}else{prior=prior->next;p=p->next;}}return OK;}#endif#include "common.h" //主程序main.cpp void main(){LinkList A;printf("******------******\n");//初始化一个链表并赋予处置然后打印出来ListInit(A);for(int i=1;i<10;i++)ListInsert(A,i,i);printf("链表数据:\n");ListPrint(A);printf("******------******\n");//实现单链表长度算法printf("链表长度:%d\n",ListLength(A));printf("******------******\n");//实现单链表结点定位操作printf("定位6在链表中的位置:");int locate = ListLocate(A,6);if(locate==ERROR)printf("未找到\n");elseprintf("%d\n",locate);printf("******------******\n");//单链表实现删除指定结点ListDelete(A,11);ListPrint(A);printf("******------******\n");//单链表排序算法的实现printf("将链表按照递增顺序重新排列\n");ListSort(A);ListPrint(A);printf("******------******\n");//给出mink和maxk,删除递增有序单链表中所有介于二者之间的结点,并释放被删结点printf("删除递增有序链表中介于3和8之间的结点\n");DeleteMinkMaxk(A,3,8);ListPrint(A);printf("******------******\n");//删除递增有序单链表中的重复元素for(i=1;i<10;i++)ListInsert(A,i,i);ListSort(A);ListPrint(A);printf("去掉重复元素后\n");DeleteRepeat(A);ListPrint(A);}数据结构题集(c语言版)清华大学出版社习题的大部分解法可以去我的博客查阅:/u/1776874690。

相关主题