/* *Copyright(c)2017, School of computer science, Yantai University *All right reserved. *File name: main.cpp list.h list.cpp *Author: Huang Shisheng *Completion date: September 21, 2017 *Version number: v1.0 * *Problem Description: single chain table algorithm library *Enter Description: None *Program output: see window */
Main function code:
#include <stdio.h> #include <malloc.h> #include "../linklist.h" int main() { LinkList *L; InitList(L); ListInsert(L, 1, 15); ListInsert(L, 1, 10); ListInsert(L, 1, 5); ListInsert(L, 1, 20); DispList(L); DestroyList(L); return 0; }
#include <stdio.h> #include <malloc.h> #include "../linklist.h" void CreateListF(LinkList *&L,ElemType a[],int n)//Establishing single chain table by head insertion { LinkList *s; int i; L=(LinkList *)malloc(sizeof(LinkList)); //Create head node L->next=NULL; for (i=0; i<n; i++) { s=(LinkList *)malloc(sizeof(LinkList));//Create a new node s->data=a[i]; s->next=L->next; //Insert * s before the original start node and after the head node L->next=s; } } void CreateListR(LinkList *&L,ElemType a[],int n)//Establishing single chain table by tail insertion { LinkList *s,*r; int i; L=(LinkList *)malloc(sizeof(LinkList)); //Create head node L->next=NULL; r=L; //r always points to the terminal node, and at the beginning points to the head node for (i=0; i<n; i++) { s=(LinkList *)malloc(sizeof(LinkList));//Create a new node s->data=a[i]; r->next=s; //Insert * s after * r r=s; } r->next=NULL; //Set the next field of the terminal node to NULL } void InitList(LinkList *&L) { L=(LinkList *)malloc(sizeof(LinkList)); //Create head node L->next=NULL; } void DestroyList(LinkList *&L) { LinkList *p=L,*q=p->next; while (q!=NULL) { free(p); p=q; q=p->next; } free(p); //At this time, q is NULL,p points to the tail node and releases it } bool ListEmpty(LinkList *L) { return(L->next==NULL); } int ListLength(LinkList *L) { LinkList *p=L; int i=0; while (p->next!=NULL) { i++; p=p->next; } return(i); } void DispList(LinkList *L) { LinkList *p=L->next; while (p!=NULL) { printf("%d ",p->data); p=p->next; } printf("\n"); } bool GetElem(LinkList *L,int i,ElemType &e) { int j=0; LinkList *p=L; while (j<i && p!=NULL) { j++; p=p->next; } if (p==NULL) //The ith data node does not exist return false; else //The ith data node exists { e=p->data; return true; } } int LocateElem(LinkList *L,ElemType e) { LinkList *p=L->next; int n=1; while (p!=NULL && p->data!=e) { p=p->next; n++; } if (p==NULL) return(0); else return(n); } bool ListInsert(LinkList *&L,int i,ElemType e) { int j=0; LinkList *p=L,*s; while (j<i-1 && p!=NULL) //Find the i-1 node { j++; p=p->next; } if (p==NULL) //Node with bit order i-1 not found return false; else //Find node * p with bit order i-1 { s=(LinkList *)malloc(sizeof(LinkList));//Create new node * s s->data=e; s->next=p->next; //Insert * s after * p p->next=s; return true; } } bool ListDelete(LinkList *&L,int i,ElemType &e) { int j=0; LinkList *p=L,*q; while (j<i-1 && p!=NULL) //Find the i-1 node { j++; p=p->next; } if (p==NULL) //Node with bit order i-1 not found return false; else //Find node * p with bit order i-1 { q=p->next; //q points to the node to delete if (q==NULL) return false; //If the ith node does not exist, return false e=q->data; p->next=q->next; //Delete * q node from single chain table free(q); //Release * q node return true; } }
linklist.h
Operation result display:#ifndef LINKLIST_H_INCLUDED #define LINKLIST_H_INCLUDED typedef int ElemType; typedef struct LNode //Define node type of single chain table { ElemType data; struct LNode *next; //Point to successor node }LinkList; void CreateListF(LinkList *&L,ElemType a[],int n);//Establishing single chain table by head insertion void CreateListR(LinkList *&L,ElemType a[],int n);//Establishing single chain table by tail insertion void InitList(LinkList *&L); //Initialize linear table void DestroyList(LinkList *&L); //Destroy linear table bool ListEmpty(LinkList *L); //Judge whether the linear table is empty int ListLength(LinkList *L); //Find the length of linear table void DispList(LinkList *L); //Output linear table bool GetElem(LinkList *L,int i,ElemType &e); //Find the value of a data element in a linear table int LocateElem(LinkList *L,ElemType e); //Find by element value bool ListInsert(LinkList *&L,int i,ElemType e); //Insert data element bool ListDelete(LinkList *&L,int i,ElemType &e); //Delete data element #endif // LINKLIST_H_INCLUDED
Summary after learning:
Through this project, I learned the algorithm library of single chain table, and also learned to adopt the idea of "gradual" to avoid the complexity caused by too many functions as much as possible.
Published 34 original articles·
Zan Zan 9.
10000 visitors+