"Data structure" in C language implementation of sequential table of various operations (including header deletion, tail delete, insert, reverse, destroy, empty, etc.)

Source: Internet
Author: User

Sequential table of various operations (including header delete, tail delete, insert, reverse order, destroy, empty etc.)//header file #ifndef _seqlist_h#define _seqlist_h#include<stdio.h>typedef int elemtype; #define Init_size 8typedef struct seqlist{elemtype *base;size_t capacity;size_t SIZE;} Seqlist;int IsEmpty (seqlist *list); int isfull (seqlist *list); void Initlist (Seqlist *list); void Push_back (Seqlist *list , Elemtype x), void Show_list (Seqlist *list), void Push_front (Seqlist *list, elemtype x); void Pop_back (Seqlist *list); void Pop_front (seqlist *list); void Insert_pos (Seqlist *list, elemtype x, int pos); void Quit_system (seqlist *list, int *x); int F IND (Seqlist *list, elemtype x); int length (seqlist *list); void Insert_val (Seqlist *list, elemtype x); void sort (seqlist *li ST); void Delete_pos (Seqlist *list,int pos), void Delete_val (Seqlist *list, elemtype x); void Clear (Seqlist *list); void          Destory (seqlist *list); void reverse (seqlist *list); #endif//function file # include "SeqList.h" int i;int isempty (seqlist *list) Check if the table is full {return list->size = = 0;}     int Isfull (seqlist *list)     Check if the table is empty {return list->size >= list->capacity;} void Initlist (Seqlist *list)//table initialization {list->capacity = Init_size;list->base = (Elemtype *) malloc (sizeof (Elemtyp e) *list->capacity); list->size = 0;} void Push_back (Seqlist *list, elemtype x)//tail interpolation {if (Isfull (list)) {printf ("Order table full, cannot insert!\n"); List->base[list->size] = x;list->size++;} void Show_list (Seqlist *list)//Display {if (list->size = = 0) {printf ("The Order table is empty. \ n "); return;} for (i = 0; i<list->size; ++i) {printf ("%d", List->base[i]);} printf ("\ n");} void Push_front (Seqlist *list, elemtype x)//Header {if (Isfull (list)) {printf ("Order table full, cannot insert!\n"); for (i = list->size; i > 0; i--) {list->base[i] = list->base[i-1];} List->base[0] = x; (list->size) + +;} void Pop_front (Seqlist *list)//Header Delete {if (IsEmpty (list)) {printf ("Order table is empty, cannot delete \ n"); return;} for (i = 0; i < list->size; i++) {List->base[i] = list->base[i + 1];} (list->size)--;} void Pop_back (SeqliSt *list)//tail Delete {if (IsEmpty (list)) {printf ("Order table is empty, cannot delete \ n"); return;} list->size--;} void Insert_pos (Seqlist *list, elemtype x, int pos)////by location Insert {if (pos<0 | | pos>list->size) {printf ("inserted in an incorrect location. \ n "); return;} if (IsEmpty (list)) {printf ("Order table is full, cannot insert. \ n"); return; for (i = list->size; i > pos; i--) {list->base[i] = list->base[i-1];} List->base[pos] = x;list->size++;} void Quit_system (seqlist *list, int *x)//exit {*x = 0;} int find (Seqlist *list, elemtype x)//Find {for (i = 0; i < list->size; i++) {if (list->base[i]==x) return i;} return-1;} int Length (seqlist *list)//Request length {return list->size;} void Insert_val (Seqlist *list, elemtype x)//by value Insert {push_back (list, x); sort (list);} void sort (seqlist *list)//Sort {if (IsEmpty (list)) {return;} for (int i = 1, i < list->size; i++) {for (int j = 0; J < list->size-i; J + +) {int temp;if (list->base[j]&gt ; list->base[j + 1]) {temp = List->base[j];list->base[j] = list->base[j + 1];list->base[j + 1] = temp;}}} void Delete_pos (seqlist *list, int pos)//Delete by location {if (IsEmpty (list)) {printf ("The Order table is empty and cannot be deleted. \ n "); return;} if (pos < 0 | | pos >= list->size) {printf ("The deleted location is incorrect. \ n "); return;} for (int i = pos; i < list->size; ++i) {List->base[i] = list->base[i + 1];} list->size--;} void Delete_val (Seqlist *list, elemtype x)//By value Delete {if (IsEmpty (list)) {printf ("The Order table is empty and cannot be deleted. \ n "); return;} int pos = find (List,x), if (pos = =-1) {printf ("The number was not found.") \ n "); return;} Delete_pos (List,pos);} void Clear (Seqlist *list)//empty {list->size = 0;} void Destory (Seqlist *list)//Destroy table {list->base = NULL;} void reverse (Seqlist *list)//reverse order {for (int i = 0, j = list->size-1; I < list->size/2; i++, j--) {i NT TEMP = List->base[i];list->base[i] = list->base[j];list->base[j] = temp;}} Main function # # "SeqList.h" void Main () {seqlist mylist;initlist (&mylist); int select = 1; Elemtype item;int Pos;while (SelecT) {printf ("****************************************************\n");p rintf ("* [1] show_list [2] Quit_sys TEM *\n ");p rintf (" * [3] push_front [4] push_back *\n ");p rintf (" * [5] pop_front [6]                Pop_back *\n ");p rintf (" * [7] insert_pos [8] insert_val *\n ");p rintf (" * [9] Delete_pos                    [Ten] Delete_val *\n ");p rintf (" * [one] find [] length *\n ");p rintf (" * [] Clear [] destory *\n ");p rintf (" * [] reverse (reverse) [+] sort (order) *\n ");p rintf (" ****************** \ n ");p rintf (" Please select: "); scanf_s ("%d ", &select); switch (select) {Case 1:show_list (&mylist); Break;case 2:quit_system (&mylist, &select); Break;case 3:printf ("Please enter the value to be inserted (-1 end):>"); while (scanf_s ("%d", &item), Item! =-1) {Push_front (&mylist, Item);} Break;case 4:printf ("Please enter the value to be inserted (-1 end):>"); while (scanf_s ("%d", &item), Item! =-1) {Push_Back (&mylist, Item);} Break;case 5:pop_front (&mylist); Break;case 6:pop_back (&mylist); Break;case 7:printf ("Please enter the location to insert:"); scanf_s ( "%d", &pos);p rintf ("Enter the value to insert:"); scanf_s ("%d", &item); Insert_pos (&mylist, Item, POS); Break;case 8:printf ("Please enter the number to insert:"), scanf_s ("%d", &item), Insert_val (&mylist, Item), Break;case 9:printf ("Please enter the location to be deleted:"); scanf_s ("%d ", &pos);d Elete_pos (&mylist,pos); Break;case 10:printf (" Enter the value to be deleted: "); scanf_s ("%d ", &item);d Elete_val ( &mylist,item); Break;case 11:printf ("Enter the number to find:"), scanf_s ("%d", &item);p os=find (&mylist, Item); if (pos! = -1) {printf ("The number is number%d. \ n", POS);} elseprintf ("The number was not found. \ n "); Break;case 12:printf (" The Order table length is:%d\n ", Length (&mylist)); Break;case 13:clear (&mylist); Break;case 14: Destory (&mylist); Break;case 15:reverse (&mylist); Break;case 16:sort (&mylist); break;default:break;}}

"Data structure" in C language implementation of sequential table of various operations (including header deletion, tail delete, insert, reverse, destroy, empty, etc.)

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.