C language implementation of the Line table

Source: Internet
Author: User

#include <stdio.h>

#include <stdlib.h>


#define LIST_INIT_SIZE 100

#define List_increment 10

#define Status INT


typedef int ELEMTYPE;


typedef struct{

Elemtype *elem;

int length;

int listsize;

}sqlist;


Status initlist (sqlist &l) {

l.elem= (Elemtype *) malloc (list_init_size * sizeof (elemtype));

if (! L.elem) exit (1);

l.length=0;

L.listsize=list_init_size;

return 1;

}

Status destory (sqlist &l) {

Free (L.elem);

return 1;

}

Status clearlist (sqlist &l) {

L.elem=null;

l.length=0;

return 1;

}

Status islistempty (sqlist &l) {

if (l.length=0) return 1;

else return 0;

}

Status listlength (sqlist &l) {

return l.length;

}


Status Getelem (sqlist &l,int i,elemtype &e) {

if (i>=1&&i<=l.length) {

E=L.ELEM[I-1];

return 1;

}

Else

return 0;

}


Returns the position of the first element that is the same as E, does not exist return 0

Status Locateelem (sqlist &l,elemtype e) {

for (int i=0;i<l.length;i++) {

if (l.elem[i]==e)

return (I+1);

}

return 0;

}


Status Prioelem (sqlist &l,elemtype cur_e,elemtype &pre_e) {

for (int i=1;i<l.length;i++) {

if (l.elem[i]==cur_e) {

PRE_E=L.ELEM[I-1];

return 1;

}

}

return 0;

}


Status Nextelem (sqlist &l,elemtype cur_e,elemtype &next_e) {

for (int i=0;i<l.length-1;i++) {

if (l.elem[i]==cur_e) {

NEXT_E=L.ELEM[I+1];

return 1;

}

}

return 0;

}


Status Listinsert (sqlist &l,int i,elemtype e) {

Elemtype *p,*q;

if (i<1| | I>L.LENGTH+1) return 0;

if (l.length>=l.listsize) {

l.elem= (Elemtype *) realloc (L.elem, (l.listsize+list_increment) *sizeof (elemtype));

L.listsize+=list_increment;

}

q=& (L.elem[i-1]);

For (p=& (l.elem[l.length-1]);p >=q;p--) * (p+1) =*p;

*q=e;

l.length++;

return 1;

}


Status printlist (sqlist &l) {

for (int i=0;i<l.length;i++)

printf ("%d\n", L.elem[i]);

return 1;

}


int main () {

SqList l;

Initlist (l);

Elemtype E1,e2,e3,e4,e5;

e1=e2=e3=e4=e5=100;

Listinsert (L,1,E1);

Listinsert (L,2,E2);

Listinsert (L,3,E3);

Listinsert (L,4,E4);

Listinsert (L,5,E5);

Printlist (l);

return 0;

}

Pure Novice mistake please point out

This article from "zjwzjw369" blog, declined reprint!

C language implementation of the Line 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.