# Thread: [Data structure]----sequential list 1

1. ## [Data structure]----sequential list 1

i'm studying data structure,so i want to show my exercises from the book,hope everyone help me improve my skill and optimize it.Thanks

Code:
```/************************************************************************/
/*  after adding a new element in a non decreasing sequential list and  */
/*  don't change the sequence                                           */
/************************************************************************/

#include <stdio.h>

#define    DATA_NUM    15

void inputData(int *dataArray, int *len)
{
int i = 0;
int data = 0;
int length = 0;

for (i=0; i<DATA_NUM; ++i)
{
scanf("%d", &data);

if (-1 == data)
{
break;
}

*(dataArray + i) = data;
++length;
}
*len = length;
}

void outputData(int *data, int len)
{
int i = 0;

printf("the data list is : ");

for (i=0; i<len; ++i)
{
printf("%d ", *(data + i));
}
printf("\n");
}

void insertData(int *dataArray, int *len, int data)
{
int i = 0;
int j = 0;

for (i=0; i<*len; ++i)
{
if (*(dataArray + i) >= data)
{
break;
}
}

for (j=0; j<=*len-i; ++j)
{
*(dataArray + *len - j + 1) = *(dataArray + *len - j);
}

*(dataArray + i) = data;
*len += 1;
}

int main()
{
int dataArray[DATA_NUM] = {0};
int len = 0;
int data = 0;

inputData(dataArray, &len);
outputData(dataArray, len);
printf("input new data: ");
scanf("%d", &data);
insertData(dataArray, &len, data);
outputData(dataArray, len);

return 0;
}```

2. ## [Data structure]----sequential list 2

Code:
```/************************************************************************/
/*          delete all the value is x in the sequential list            */
/************************************************************************/

#include <stdio.h>

#define    DATA_NUM    15

void inputData(int *dataArray, int *len)
{
int i = 0;
int data = 0;
int length = 0;

for (i=0; i<DATA_NUM; ++i)
{
scanf("%d", &data);

if (-1 == data)
{
break;
}

*(dataArray + i) = data;
++length;
}
*len = length;
}

void outputData(int *data, int len)
{
int i = 0;

printf("the data list is : ");
for (i=0; i<len; ++i)
{
printf("%d ", *(data + i));
}
printf("\n");
}

void delSameData(int *dataArray, int *len, int data)
{
int i = 0;
int j = 0;
int index = 0;

for (i=0; i<*len; ++i)
{
if (*(dataArray + i) != data)
{
continue;
}

++index;

for (j=i; j<*len; ++j)
{
*(dataArray + j) = *(dataArray + j + 1);
}
--i;
}

*len -= index;
}

int main()
{
int dataArray[DATA_NUM] = {0};
int len = 0;
int delData = 0;

inputData(dataArray, &len);
outputData(dataArray, len);
printf("input delete data: ");
scanf("%d", &delData);
delSameData(dataArray, &len, delData);
outputData(dataArray, len);

return 0;
}```

3. Instead of writing *(dataArray + i) write dataArray[i]. Likewise for *(dataArray + *len - j + 1).

4. ## [Data structure]----sequential list 3

Code:
```/************************************************************************/
/*     delete all the value between x and y in the sequential list      */
/************************************************************************/

#include <stdio.h>

#define    DATA_NUM    15

void inputData(int *dataArray, int *len)
{
int i = 0;
int data = 0;
int length = 0;

for (i=0; i<DATA_NUM; ++i)
{
scanf("%d", &data);

if (-1 == data)
{
break;
}

*(dataArray + i) = data;
++length;
}
*len = length;
}

void outputData(int *data, int len)
{
int i = 0;

printf("the data list is : ");
for (i=0; i<len; ++i)
{
printf("%d ", *(data + i));
}
printf("\n");
}

void delDataAToB(int *dataArray, int *len, int a, int b)
{
int i = 0;
int j = 0;
int index = 0;

for (i=0; i<*len; ++i)
{
j = i;
while (*(dataArray + j) >= a && *(dataArray + j) <= b)
{
++index;
++j;
continue;
}

if (index > 0)
{
for (; j<*len; ++j)
{
*(dataArray + j - index) = *(dataArray + j);
}

*len -= index;
--i;
index = 0;
}
}
}

int main()
{
int dataArray[DATA_NUM] = {0};
int len = 0;

inputData(dataArray, &len);
outputData(dataArray, len);
printf("input the scope of deleting data: ");
outputData(dataArray, len);

return 0;
}```

5. ## [Data structure]----sequential list 4

Code:
```/************************************************************************/
/*  put the data of less than 0 in the front of the sequential          */
/*  list,more than 0 is rear                                            */
/************************************************************************/

#include <stdio.h>

#define    DATA_NUM    15

void inputData(int *dataArray, int *len)
{
int i = 0;
int data = 0;
int length = 0;

for (i=0; i<DATA_NUM; ++i)
{
scanf("%d", &data);

if (-1 == data)
{
break;
}

*(dataArray + i) = data;
++length;
}
*len = length;
}

void outputData(int *data, int len)
{
int i = 0;

printf("the data list is : ");
for (i=0; i<len; ++i)
{
printf("%d ", *(data + i));
}
printf("\n");
}

void putDataByZero(int *dataArray, int len)
{
int i = 0;
int temp = 0;
int *p = dataArray;
int *q = p;

while (p < &dataArray[len-1])
{
while ((*p >= 0) && (p < &dataArray[len-1]))
{
++p;
}

temp = *p;
*p = *q;
*q = temp;
++q;
}
}

int main()
{
int dataArray[DATA_NUM] = {0};
int len = 0;

inputData(dataArray, &len);
outputData(dataArray, len);
putDataByZero(dataArray, len);
outputData(dataArray, len);

return 0;
}```

6. Four posts merged into one