(i) Linear table structure 1 array implementation

Source: Internet
Author: User

(i) Array implementation linear table

1. Defining a collection (struct implementation)

1 #define MAX2  struct  {3       int  Data[max]; 4       int Last ; 5 }list;

The data array is used to hold the integer type, and last indicates the subscript of the final element

   2. Define various operations

A. Creating an empty table

    

LIST *makeempty () {    *Ptrl;     = (list *) malloc (sizeof(list));    Ptrl->last =-1;     return Ptrl;}

b. Inserting data

voidInsert (intX, LIST *ptrl,inti) {if(Ptrl->last = = MAX-1) {printf ("Table Full"); return; }    if(i<1|| I>ptrl->last +2) {printf ("location is illegal ."); return; }    intJ;  for(j = ptrl->last; J >= I-1; j--) {Ptrl->data[j +1] = Ptrl->data[j +1]; } Ptrl->data[i-1] =x; Ptrl->last++; return;}

     C. Find

int int data) {    int0;      while (i <= ptrl->last&&ptrl->data[i]! = DATA)        i+ +    ; if (i > Ptrl-> last)         return -1;     Else        return i;}

   D. Delete

 void  Delete (LIST *l, int   i) { int   J;  if  (I<1  | | i>l->last + " Span style= "COLOR: #800080" >1  ) {printf (  " /span> There is no such location!          "  return  ;         for  (j = i; J <= l->last; J++->data[j-1 ] = L->data[j]; } l ->last--;}  

    E. Printing (traversal)

void Print (LIST *l) {    int  i;      for 0; I <= l->last; i++)        printf ("", l->data[i]);}

Complete implementation

1#include <stdio.h>2#include <stdlib.h>3 #defineMAX 104typedefstruct {5     intData[max];6     intLast ;7 }list;8 9 LIST *Makeempty () {TenLIST *Ptrl; OnePtrl = (LIST *)malloc(sizeof(LIST)); APtrl->last =-1; -     returnPtrl; -  the } -  - intFind (LIST *ptrl,intdata) { -     inti =0; +      while(i <= ptrl->last&&ptrl->data[i]! =data) -i++; +     if(I > ptrl->Last ) A         return-1; at     Else -         returni; - } -  - voidInsert (intX, LIST *ptrl,inti) { -     if(Ptrl->last = = MAX-1) { inprintf"Table Full"); -         return; to     } +     if(i<1|| I>ptrl->last +2) { -printf"location is illegal ."); the         return; *     } $     intJ;Panax Notoginseng      for(j = ptrl->last; J >= I-1; j--) { -Ptrl->data[j +1] = Ptrl->data[j +1]; the     } +Ptrl->data[i-1] =x; Aptrl->last++; the     return; + } -  $ voidDelete (LIST *l,inti) { $     intJ; -     if(i<1|| I>l->last +1) { -printf"There is no such location! "); the         return; -     }Wuyi      for(j = i; J <= l->last; J + +) { theL->data[j-1] = l->Data[j]; -     } Wul->last--; - } About  $ voidPrint (LIST *l) { -     inti; -      for(i =0; I <= l->last; i++) -printf"%d", l->data[i]); A}

(i) Linear table structure 1 array implementation

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.