Sequence list
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<windows.h>
# define LIST_INIT_SIZE 100
# define LISTINCREMENT 10
typedef char DataType;
typedef struct
{
DataType *elem;
int length;
int listsize;
}SeqList;
// Initialize sequence list
int InitSeqList(SeqList *list)
{
list->elem = (DataType *) malloc((LIST_INIT_SIZE + 1) * sizeof(DataType));
if(NULL == list->elem)
{
printf("Error in memory!!!!\n");
return 0;
}
list->length = 0;
list->listsize = LIST_INIT_SIZE;
printf("Initialization successed!!\n");
return 1;
}
// Insert data into the sequence list
int InsertSeqList(SeqList &list , int intPosition , DataType datatype)
{
if(list.length >= list.listsize)
{
DataType *newbase = (DataType *)realloc(list.elem , (list.listsize + LISTINCREMENT) * sizeof (DataType));
if (!newbase)
return 0 ;
list.elem = newbase;
list.listsize += LISTINCREMENT;
printf("Created new base is successed.\n");
}
if(intPosition < 1 || intPosition > list.length+1)
{
printf("Error!! The inserted data's position is outside the law!\nPlease try again!!\n");
return 0;
}
else
{
for(int num = list.length ; num >= intPosition ; num--)
{
list.elem[num + 1] = list.elem[num];
}
list.elem[intPosition] = datatype;
list.length++;
printf("Insert successed.\n");
return 1;
}
}
// Delete data in sequence list
int DeleteSeqList(SeqList *list , int intPosition)
{
if(intPosition < 1 || intPosition > list->length || NULL == list->listsize)
{
printf("Error! No data in this position, Please try again!\n");
return 0;
}
char array[1024] , *choice;
printf("Do you want to delete this data? (Y or N): ");
scanf("%s" , &array);
choice = array;
switch(*choice)
{
case 'Y':
case 'y':
for(int num = intPosition + 1; num <= list->length ; num ++)
{
list->elem[num] = list->elem[num + 1];
}
list->length --;
printf("Delete successed!\n");
return 1;
break;
case 'N':
case 'n':
return 0;
break;
default:
printf("Warning!! you enter data of error! Please try again!");
break;
}
}
// Count sequence list sizes
int LengthSeqList(SeqList *list)
{
return list->length;
}
// Search the data in the sequence list
int SearchSeqList(SeqList *list , DataType datatype)
{
return 0;
}
// Return the data in the sequence list
DataType GetFromSeqList(SeqList *list , int intPosition)
{
return list->elem[intPosition];
}
// Display all of the data in the sequence list
void ShowSeqList(SeqList *list)
{
printf("Display all of the data in the sequence list\n");
if(0 == list->length)
{
printf("Sequence list is null");
}
else
{
for(int loop = 1 ; loop <= list->length ; loop++)
{
printf("%c " , list->elem[loop]);
}
printf("\n");
}
}
// Main function
void main()
{
char array[1024] , *choice ;
int intPosition , flag = 1 , result;
SeqList list;
DataType datatype;
while(flag)
{
printf("\n\n\n\t\t\t--sequence list--\n\n");
printf("\t************************************************\n");
printf("\t* 1 - Initialize sequence list *\n");
printf("\t* 2 - Insert data into sequence list *\n");
printf("\t* 3 - Delete data in the sequence list *\n");
printf("\t* 4 - return sequence list's length *\n");
printf("\t* 5 - Search data order by value *\n");
printf("\t* 6 - Read element value *\n");
printf("\t* 7 - Display sequence list *\n");
printf("\t* 0 - Exit *\n");
printf("\t* *\n");
printf("\t* This Demo is Made by: Radeon LING *\n");
printf("\t* Email: Radeon_ling@eastday.com *\n");
printf("\t* *\n");
printf("\t* Copy right: 2005 Millennium Studio *\n");
printf("\t************************************************\n");
printf("Please chioce menu number: ");
scanf("%s" , &array);
choice = array;
switch(*choice)
{
case '1':
InitSeqList(&list);
break;
case '2':
printf("\nPlease enter number position and value. \n(Enter Style: intPosition , value): ");
scanf("%d,%c" , &intPosition , & datatype);
InsertSeqList(list , intPosition , datatype);
break;
case '3':
printf("\nPlease enter position of deleted value:");
scanf("%d" , &intPosition);
DeleteSeqList(&list , intPosition);
ShowSeqList(&list);
break;
case '4':
result = LengthSeqList(&list);
printf("List length is %d\n" , result);
break;
case '5':
result = SearchSeqList(&list , datatype);
printf("Need software update .. .. .. ..\n");
break;
case '6':
printf("Please enter position in the list: ");
scanf("%d" , &intPosition);
printf("Position %d is %c.\n" , intPosition , GetFromSeqList(&list , intPosition));
break;
case '7':
ShowSeqList(&list);
break;
case '0':
printf("Do you want exit? (Y or N)");
scanf("%s" , &array);
choice = array;
if('Y' == *choice || 'y' == *choice)
{
flag = 0;
printf("End of Program.\nWelcome use the Millennium Software.\n");
}
break;
default:
printf("Error, please enter 0 - 7, try angain!\n");
break;
}
}
return;
}
#include<stdlib.h>
#include<string.h>
#include<windows.h>
# define LIST_INIT_SIZE 100
# define LISTINCREMENT 10
typedef char DataType;
typedef struct
{
DataType *elem;
int length;
int listsize;
}SeqList;
// Initialize sequence list
int InitSeqList(SeqList *list)
{
list->elem = (DataType *) malloc((LIST_INIT_SIZE + 1) * sizeof(DataType));
if(NULL == list->elem)
{
printf("Error in memory!!!!\n");
return 0;
}
list->length = 0;
list->listsize = LIST_INIT_SIZE;
printf("Initialization successed!!\n");
return 1;
}
// Insert data into the sequence list
int InsertSeqList(SeqList &list , int intPosition , DataType datatype)
{
if(list.length >= list.listsize)
{
DataType *newbase = (DataType *)realloc(list.elem , (list.listsize + LISTINCREMENT) * sizeof (DataType));
if (!newbase)
return 0 ;
list.elem = newbase;
list.listsize += LISTINCREMENT;
printf("Created new base is successed.\n");
}
if(intPosition < 1 || intPosition > list.length+1)
{
printf("Error!! The inserted data's position is outside the law!\nPlease try again!!\n");
return 0;
}
else
{
for(int num = list.length ; num >= intPosition ; num--)
{
list.elem[num + 1] = list.elem[num];
}
list.elem[intPosition] = datatype;
list.length++;
printf("Insert successed.\n");
return 1;
}
}
// Delete data in sequence list
int DeleteSeqList(SeqList *list , int intPosition)
{
if(intPosition < 1 || intPosition > list->length || NULL == list->listsize)
{
printf("Error! No data in this position, Please try again!\n");
return 0;
}
char array[1024] , *choice;
printf("Do you want to delete this data? (Y or N): ");
scanf("%s" , &array);
choice = array;
switch(*choice)
{
case 'Y':
case 'y':
for(int num = intPosition + 1; num <= list->length ; num ++)
{
list->elem[num] = list->elem[num + 1];
}
list->length --;
printf("Delete successed!\n");
return 1;
break;
case 'N':
case 'n':
return 0;
break;
default:
printf("Warning!! you enter data of error! Please try again!");
break;
}
}
// Count sequence list sizes
int LengthSeqList(SeqList *list)
{
return list->length;
}
// Search the data in the sequence list
int SearchSeqList(SeqList *list , DataType datatype)
{
return 0;
}
// Return the data in the sequence list
DataType GetFromSeqList(SeqList *list , int intPosition)
{
return list->elem[intPosition];
}
// Display all of the data in the sequence list
void ShowSeqList(SeqList *list)
{
printf("Display all of the data in the sequence list\n");
if(0 == list->length)
{
printf("Sequence list is null");
}
else
{
for(int loop = 1 ; loop <= list->length ; loop++)
{
printf("%c " , list->elem[loop]);
}
printf("\n");
}
}
// Main function
void main()
{
char array[1024] , *choice ;
int intPosition , flag = 1 , result;
SeqList list;
DataType datatype;
while(flag)
{
printf("\n\n\n\t\t\t--sequence list--\n\n");
printf("\t************************************************\n");
printf("\t* 1 - Initialize sequence list *\n");
printf("\t* 2 - Insert data into sequence list *\n");
printf("\t* 3 - Delete data in the sequence list *\n");
printf("\t* 4 - return sequence list's length *\n");
printf("\t* 5 - Search data order by value *\n");
printf("\t* 6 - Read element value *\n");
printf("\t* 7 - Display sequence list *\n");
printf("\t* 0 - Exit *\n");
printf("\t* *\n");
printf("\t* This Demo is Made by: Radeon LING *\n");
printf("\t* Email: Radeon_ling@eastday.com *\n");
printf("\t* *\n");
printf("\t* Copy right: 2005 Millennium Studio *\n");
printf("\t************************************************\n");
printf("Please chioce menu number: ");
scanf("%s" , &array);
choice = array;
switch(*choice)
{
case '1':
InitSeqList(&list);
break;
case '2':
printf("\nPlease enter number position and value. \n(Enter Style: intPosition , value): ");
scanf("%d,%c" , &intPosition , & datatype);
InsertSeqList(list , intPosition , datatype);
break;
case '3':
printf("\nPlease enter position of deleted value:");
scanf("%d" , &intPosition);
DeleteSeqList(&list , intPosition);
ShowSeqList(&list);
break;
case '4':
result = LengthSeqList(&list);
printf("List length is %d\n" , result);
break;
case '5':
result = SearchSeqList(&list , datatype);
printf("Need software update .. .. .. ..\n");
break;
case '6':
printf("Please enter position in the list: ");
scanf("%d" , &intPosition);
printf("Position %d is %c.\n" , intPosition , GetFromSeqList(&list , intPosition));
break;
case '7':
ShowSeqList(&list);
break;
case '0':
printf("Do you want exit? (Y or N)");
scanf("%s" , &array);
choice = array;
if('Y' == *choice || 'y' == *choice)
{
flag = 0;
printf("End of Program.\nWelcome use the Millennium Software.\n");
}
break;
default:
printf("Error, please enter 0 - 7, try angain!\n");
break;
}
}
return;
}
Download Demo code: Sequence list.rar