Dynamic linked list additions and deletions and sorting functions

Source: Internet
Author: User

The implementation of the main functions:

#include "SeqList.h" void initseqlist (Seqlist * pseq)//Initialize {assert (PSEQ);p Seq->array = (datatype*) malloc (sizeof ( DataType) *default_capicity);p seq->size = 0;pseq->capicity = default_capicity;} void Printseqlist (seqlist* pseq)//print {assert (PSEQ); size_t i = 0;for (; i < pseq->size; i++) {printf ("%d", Pseq->ar Ray[i]);} printf ("\ n");} void Checkexpandcapicity (seqlist* pseq)//check capacity {assert (PSEQ); if (pseq->size = = pseq->capicity) {DataType *tmp = ( DataType *) malloc (pseq->capicity * 2 * sizeof (DataType)) memcpy (TMP, Pseq->array, sizeof (DataType) *pseq-> Size), free (pseq->array);p Seq->array = tmp;pseq->capicity = Pseq->capicity * 2;}} void Pushfront (seqlist* pseq, DataType x)//head interpolation {int i = 0;assert (PSEQ); Checkexpandcapicity (PSEQ); for (i = pseq->size; i >= 1; i--) {pseq->array[i] = pseq->array[i-1];} Pseq->array[0] = x;pseq->size++;} void Popfront (seqlist* pseq)//Header Delete {size_t i = 0;assert (PSEQ); for (; I < pseq->size-1; i++) {Pseq->array[i] = PSeQ->array[i + 1];} pseq->size--;} void pushback (seqlist* pseq, DataType x)//tail Insert {assert (PSEQ); Checkexpandcapicity (PSEQ);p seq->array[pseq->size] = x;pseq->size++;} void Popback (seqlist* pseq)//tail Delete {assert (PSEQ);p seq->size--;} void Insert (seqlist* pseq, size_t Index, DataType x)//Insert {size_t i = Pseq->size;assert (pseq) at index position, assert (Index < Pseq->size); Checkexpandcapicity (PSEQ); for (; I >index; i--) {pseq->array[i] = pseq->array[i-1];} Pseq->array[index] = x;pseq->size++;} void Modify (seqlist* pseq, size_t Index, DataType x)//Modify {assert (PSEQ); assert (Index < pseq->size);p seq->array[ Index] = x;} void Remove (seqlist* pseq, size_t Index)//delete the number of index positions {size_t i = Index;assert (PSEQ); assert (Index < pseq->size); for (; i < pseq->size-1; i++) {Pseq->array[i] = pseq->array[i + 1];} pseq->size--;} void Swap (datatype* left, datatype* right) {DataType tmp = *left;*left = *right;*right = tmp;} void Bubblesort (seqlist* pseq)//bubble Sort {size_t index, end;int exchange = 0;assert (PSEQ); for (end = pseq->size-1; end > 0;--end) {exchange = 0;for (index = 0; index < end ; index++) {if (Pseq->array[index]>pseq->array[index + 1]) {Swap (Pseq->array + index, Pseq->array + index + 1 ); exchange = 1;}} if (Exchange = = 0) {break;}}}  void Selectsort (seqlist* pseq)//select sort {size_t minindex, index, Begin;assert (PSEQ); for (begin = 0; begin < pseq->size- 1; begin++) {Minindex = begin;for (index = begin + 1; index < pseq->size; index++) {if (pseq->array[minindex]>pseq- >array[index]) {minindex = index;}} if (minindex! = begin) {Swap (Pseq->array + minindex, Pseq->array + Begin);}}} Findret BinarySearch (seqlist* pseq, DataType x)//binary lookup {size_t left=0;size_t right = pseq->size-1;; size_t Middle; Findret ret;ret.isfind = False;assert (PSEQ), while (left<=right) {middle = (left + right)/2;if (x = = Pseq->array[mid Dle]) {ret.isfind = True;ret.index = Middle;return ret;} else if (X>pseq->array[middle]) left = Middle+ 1;elseright = middle-1;} return ret;}

Header file:

#pragma once#define default_capicity 3typedef int DataType; #include <stdio.h> #include <string.h> #include <assert.h> #include <malloc.h>typedef struct seqlist{datatype *array;size_t size;size_t capicity;//Current capacity} Seqlist;typedef enum tag{true,//true false,//false}tag;typedef struct Findret{tag isfind;//whether to find the label size_t index;//find the subscript for the data} Findret;void initseqlist (seqlist *pseq); void Printseqlist (Seqlist *pseq); void Checkexpandcapicity (SeqList* pSeq); void Pushfront (Seqlist *pseq, DataType x), void Popfront (Seqlist *pseq), void pushback (Seqlist *pseq, DataType x); void PopB ACK (seqlist *pseq), void Insert (Seqlist *pseq, size_t Index, DataType x), void Modify (Seqlist *pseq, size_t index, DataType x), void Remove (Seqlist *pseq, size_t index), void Swap (datatype* left, datatype* right), void Bubblesort (seqlist* pseq); void Selectsort (seqlist* pseq); Findret BinarySearch (seqlist* pSep, DataType x);

Test program section:

#include "SeqList.h" void test1 ()//test initialization, print, tail/tail Delete {seqlist s;initseqlist (&s); Pushback (&s, 1); Pushback (&s, 2); Pushback (&s, 3); Pushback (&s, 4); Printseqlist (&s); Popback (&s); Printseqlist (&s);} void Test2 ()//test head plug, head delete {seqlist s;initseqlist (&s); Pushfront (&s, 3); Pushfront (&s, 4); Pushfront (&s, 5); Pushfront (&s, 6); Printseqlist (&s); Popfront (&s); Printseqlist (&s);} void Test3 ()//test inserts at the index position, modifies the value of the index position, deletes the value of the index position {seqlist s;initseqlist (&s); Pushback (&s, 1); Pushback (&s, 2); Pushback (&s, 3); Pushback (&s, 4); Printseqlist (&s); Insert (&s, 2, 8); Printseqlist (&s); Modify (&s,2,5); Printseqlist (&s); Remove (&s, 2); Printseqlist (&s);} void Test4 ()//test bubble sort {seqlist s;initseqlist (&s); Pushback (&s, 3); Pushback (&s, 1); Pushback (&s, 5); Pushback (&s, 4); Pushback (&s, 2); Printseqlist (&s); Bubblesort (&s); Printseqlist (&s);} void Test5 ()//Test Select sort {seqlist s;initseqlist (&s); Pushback (&s, 3); Pushback (&s, 1); Pushback (&s, 5); Pushback (&s, 4); Pushback (&s, 2); Printseqlist (&s); Selectsort (&s); Printseqlist (&s);} void Test6 ()//test binary search {DataType x; Findret ret; Seqlist s;initseqlist (&s); Pushback (&s, 1); Pushback (&s, 2); Pushback (&s, 3); Pushback (&s, 4); Pushback (&s, 5); x = 4;ret = BinarySearch (&s, x); if (Ret.isfind = = TRUE) {printf ("%d%d\n", X,ret.index);} elseprintf ("Find failed!\n"), x = 8;ret = BinarySearch (&s, X), if (Ret.isfind = = TRUE) {printf ("%d%d", X, Ret.index);} elseprintf ("Find failed!\n"); x = 1;ret = BinarySearch (&s, X), if (Ret.isfind = = TRUE) {printf ("%d%d\n", X, Ret.index);} elseprintf ("Find failed!\n"); x = 5;ret = BinarySearch (&s, X), if (Ret.isfind = = TRUE) {printf ("%d%d\n", X, Ret.index);} elseprintf ("Find failed!\n");} int main () {test1 (); Test2 ();//test3 (); test4 (); Test5 (); Test6 (); GetChar (); return 0;}


Dynamic linked list additions and deletions and sorting functions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.