1. Sequential storage of linear tables with Data Structures

Source: Internet
Author: User
#include <stdio.h>
#include <stdlib.h>

#define Description LinkListSquence
#define SIZE 20

typedef int type;
typedef int bool;

typedef struct{
type data[SIZE];
int length;
}List;

List * InitList(List *list);
bool ListEmpty(List *list);
List * ClearList(List *list);
type GetElem(List *list,int index);
int LocateElem(List *list,type element);
int ListLength(List *list);

bool ListInsert(List * list,int index,type element);
bool ListAppend(List * list,type element);
type ListDelete(List *list,int index);


void ShiftToRight(List * list,int index);
void ShiftToLeft(List * list,int index);

int main(int argc, char ** argv)
{
List list;
type ele ;
InitList(&list);

if(ListEmpty(&list))
{
printf("empty\n");
}
ele = 0;
ListAppend(&list,ele);
printf("length:%d\n",list.length);

ListInsert(&list,list.length-1,ele+1);
printf("length:%d\n",list.length);

return 0;
}

List * InitList(List *list)
{
list->length = 0;
return list;
}

bool ListEmpty(List *list)
{
if(list->length == 0)
{
return 1;
}
return 0;
}

bool ListFull(List * list)
{
return list->length == SIZE;
}

/*Clear the list ,so that can use the element*/
List * ClearList(List *list)
{
list->length = 0;
return list;
}

/*get a element by index*/
type GetElem(List *list,int index)
{
return list->data[index];
}

/*get the index of the element in the list*/
int LocateElem(List *list,type element)
{
int i = list->length - 1;
for(; i >= 0; i--)
{
if(list->data[i] == element)
{
return i;
}
}
return -1;
}

int ListLength(List *list)
{
return list->length;
}

/*index starts with 0 to SIZE-1 */
bool ListInsert(List * list,int index,type element)
{
if(list->length < SIZE && index < SIZE)
{
int i = list->length - 1;
for(;i > index;i--)
{
ShiftToRight(list,i);
}
list->data[index] = element;
list->length++;
return 1;
}

return 0;
}

bool ListAppend(List * list,type element)
{
if(list->length < SIZE)
{
list->data[list->length] = element;
list->length++;
return 1;
}

return 0;
}

/*index starts with 0 to SIZE-1 */
bool ListDelete(List *list,int index)
{
if(list->length < SIZE && index < SIZE)
{
int i = index;
for(;i <= list->length - 1;i++)
{
ShiftToLeft(list,i);
}
list->length--;

return 1;
}

return 0;
}

void ShiftToRight(List * list,int index)
{
list->data[index+1] = list->data[index];
return ;
}

void ShiftToLeft(List * list,int index)
{
list->data[index-1] = list->data[index];
return ;
}

  

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.