Complete implementation of C-language sequential table

Source: Internet
Author: User

#include <stdio.h> #include <stdlib.h> #define ElementType int#define MAXSIZE 100typedef struct{elementtype Data[maxsize];int last;} List;//list in front and back effect is the same as List *makeempty (); ElementType findkth (int K, list *ptrl); int Find (ElementType x,list *ptrl); void Insert (ElementType X, int i, list *ptrl); Vo ID Delete (int i, list *ptrl), int Length (list *ptrl), void Lprint (list *ptrl) {for (int i = 0; i<ptrl->last;i++) printf (" %d\n ", Ptrl->data[i]);} int main () {List l,*ptrl;//accesses the element labeled I l.data[i] or ptrl->data[i]//Linear table length: l.last+1 or ptrl->last+1 Ptrl = Makeempty (); for (int i = 0; i< 10;i++) {Insert (I,i,ptrl);} Insert (15,5,ptrl); Lprint (Ptrl);D elete (5,ptrl); Lprint (Ptrl); return 0;} Initialize, set empty order table list *makeempty () {list *ptrl; Ptrl = (list *) malloc (sizeof (list)); Ptrl->last=0;return Ptrl;} ElementType findkth (int K, List L) {return l.data[k-1];} int Find (ElementType x,list L) {List *ptrl = &l;int i = 0;for (i =0;i<=ptrl->last;i++) if (ptrl->data[ptrl-> last]==x) break;if (i>ptrl->last) Return-1;elsereturn I;} void Insert (ElementType X, int i, List *ptrl) {//is processed by position I, l.data[i-1], first element is l.last[l.last+1] if (i<1| | i>ptrl->last+2) {printf ("the insertion position is unreasonable \ n"); return;} for (int j = ptrl->last;j>=i-1;j--) ptrl->data[j+1]=ptrl->data[j]; ptrl->data[i-1]=x; ptrl->last++;return; }void Delete (int i, List *ptrl) {////By position I, delete l.data[i-1]; the last element is L.data[l.last] if (i<1| | I>ptrl->last) {printf ("The deletion location is unreasonable \ n"); return;} int j;for (j=i;j<ptrl->last;j++) ptrl->data[j-1]=ptrl->data[j]; ptrl->last--;}  int Length (List *ptrl) {return ptrl->last+1;}

  

Complete implementation of C-language sequential table

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.