"Data structure" implementation order table (C language)

Source: Internet
Author: User

Header file:


#ifndef _seqlist_h#define _seqlist_h#include <stdio.h> #define init_size 8typedef struct Seqlist{int *base;size_t size;size_t capacity;} seqlist;//the function to implement void Initlist (Seqlist *list); int isfull (seqlist *list); int IsEmpty (seqlist *list); void Show_list ( Seqlist *list); void Tail_insert (Seqlist *list,int x); void Head_insert (Seqlist *list,int x); void sort (seqlist *list); void Head_delete (seqlist *list); void Tail_delete (Seqlist *list); void Pos_insert (Seqlist *list,int x,int pos); void Val_ Insert (seqlist *list, int x), void Pos_delete (Seqlist *list,int POS), int find (seqlist *list,int x), void Val_delete ( Seqlist *list,int x), int length (seqlist *list), void reverse (seqlist *list), void Clear (Seqlist *list), void Destroy ( Seqlist *list); void Quit_system (Seqlist *list,int* x); #endif



Fun



#include "SeqList.h"//define two return values to receive the result of the judgment function int fret = Isfull;int Eret = isempty;//initialization order table void Initlist (Seqlist *list) {LIST-&G T;size = 0;list->capacity = Init_size;list->base = (int*) malloc (sizeof (int) *list->capacity);} Determine if the order table is full int isfull (Seqlist *list) {if (list->size >= list->capacity) {return-1;} Elsereturn 0;} Determine if the order table is empty int isempty (seqlist *list) {if (list->size = = 0) {return-1;} Elsereturn 0;} show void Show_list (Seqlist *list) {int i;if (list->size = = 0) {printf ("The table is empty!\n");} for (i = 0; i < list->size; ++i) {printf ("%d", List->base[i]);} printf ("\ n");} End plug void tail_insert (seqlist *list,int x) {if (fret = =-1) {printf ("The table is a Full,can not insert!\n"); return;} List->base[list->size] = x;list->size++;} Header socket void Head_insert (seqlist *list,int x) {int i;if (fret = =-1) {printf ("The table is Full,can not insert!\n"); return;} for (i = list->size; i > 0; i.) {list->base[i] = list->base[i-1];} List->base[0] = x;list->size++;}sort void sort (seqlist *list) {int i;int j;int temp;for (i = 1; i < list->size; ++i) {for (j = 0; J < list->size I ++J) {if (list->base[j]>list->base[j + 1]) {temp = List->base[j];list->base[j] = list->base[j + 1]; List->base[j + 1] = temp;}}}} Header delete void Head_delete (Seqlist *list) {int i;if (Eret = =-1) {printf ("The table is Empty,can not delete!\n"); return;} for (i = 0; i < list->size; ++i) {List->base[i] = list->base[i + 1];} list->size--;} Tail delete void Tail_delete (Seqlist *list) {if (Eret = =-1) {printf ("The table is Empty,can not delete!\n"); return;} list->size--;} bitwise insert void Pos_insert (seqlist *list,int x,int pos) {int i;if (fret = =-1) {printf ("The table is Full,can not insert!\n"); re Turn;} if (pos<0 | | pos>list->size) {printf ("The position is illegal!\n"); return;} Else{for (i = list->size; i > pos; i) {list->base[i] = list->base[i-1];} List->base[pos] = x;list->size++;}} Insert void Val_insert (Seqlist *list,int x) by value {if (FRET = =-1) {printf ("The table is Full,can not insert!\n"); return;} Tail_insert (list,x); sort (list);} bitwise delete void Pos_delete (Seqlist *list,int pos) {int i;if (Eret = =-1) {printf ("The table is Empty,can not delete!\n"); return; }if (pos<0 | | pos>list->size) {printf ("The position is illegal!\n"); return;} Else{for (i = pos; i < list->size; ++i) {List->base[i] = list->base[i + 1];} list->size--;}} Find int Find (seqlist *list,int x) {int i;for (i = 0; i < list->size; ++i) {if (list->base[i] = = x) {return i;}} printf ("The number is not exit!\n"); return 0;} Delete void Val_delete by value (seqlist *list,int x) {int ret = find (list, x); int i;if (Eret = =-1) {printf ("The table is Empty,can n OT delete!\n "); return;} if (ret = =-1) {printf ("The number is not exist!\n"); return;} Else{for (i = ret; I <list->size; ++i) {List->base[i] = list->base[i + 1];} list->size--;}} length int (seqlist *list) {return list->size;} Invert void reverse (seqlist *list) {int Temp;int i = 0;int j = list-&Gt;size-1;if (Eret = =-1) {printf ("The table is Empty,can not operate!\n"); return;} while (I < j) {temp = List->base[i];list->base[i] = list->base[j];list->base[j] = temp;++i;--j;}} Empty void Clear (Seqlist *list) {list->size = 0;} destroys void Destroy (Seqlist *list) {list->base = NULL;} Exit system void Quit_system (Seqlist *list,int *x) {*x = 0;}


Main function:



C Implementation Order table # include "SeqList.h" int main () {seqlist mylist;initlist (&mylist); int input = 1;int Insert = 0;int pos = 0;whil E (input) {printf ("*********************************************************************\n");p rintf ("* [1] Show_ list [2] Tail_insert *\n "); printf ("* [3] head_insert [4] sort *\n"); printf ("* [5] head_delete [6] tail_delete *\n"); printf ("* [7] pos_insert [8] Val_insert *\n"); printf ("* [9] pos_delete [ten] Find *\n"); printf ("* [one] val_delete [] length *\n"); printf ("* [] reverse [] clear *\n"); printf ("* [] destroy [] quit_system *\n"); printf ("*********************************************************************\n"); printf ("Please enter your choose:"); SCAnf_s ("%d", &input), switch (input) {case 1:show_list (&mylist); Break;case 2:printf ("Please enter the number want to insert:\n "); (scanf_s ("%d ", &insert), insert! =-1) {Tail_insert (&mylist, insert);} Break;case 3:printf ("Please enter the number want to insert:\n") and while (scanf_s ("%d", &insert), insert! =-1) {Head_ins ERT (&mylist, insert);} Break;case 4:sort (&mylist); Break;case 5:head_delete (&mylist); Break;case 6:tail_delete (&mylist); break; Case 7:printf (' Please enter the number want to insert:\n '); scanf_s ("%d", &insert);p rintf ("Please enter the position: \ n "); scanf_s ("%d ", &pos);p Os_insert (&mylist, insert, POS); Break;case 8:printf (" Please enter the number want to Insert:\n "), scanf_s ("%d ", &insert), Val_insert (&mylist, insert); Break;case 9:printf (" Please enter the Position: \ n "); scanf_s ("%d ", &pos);p os_delete (&mylist, POS); Break;case 10:printf (" Please enter the number want To find:\n "), scanf_s ("%d ", &insert);p rintf (" at theD\n ", find (&mylist, insert)); Break;case 11:printf (" Please enter the number want to delete:\n "); scanf_s ("%d ", & insert); Val_delete (&mylist, insert); Break;case 12:printf ("The table's length is%d\n", Length (&mylist)); ; case 13:reverse (&mylist); Break;case 14:clear (&mylist); Break;case 15:destroy (&mylist); Break;case 16: Quit_system (&mylist, &input); break;default:break;}} return 0;}

Show:




Tail Plug:




Head Insert:




Sort:




Head Delete:




Tail Delete:




Positioning insert:




Insert by Value:




Bitwise DELETE:




Find:




Delete by value:




To find the length:




Reverse:




Clear:




Exit System:





"Data structure" implementation order table (C language)

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.