Home > Back-end >  C language data structure dynamic order list, I hope you give some advice.
C language data structure dynamic order list, I hope you give some advice.

Time:11-25

#include
#include
# pragma warning (4996) disable:

Typedef struct SqList
{
Int * elem;
int length;
Int size=5;
} SqList;

//* * * * * * * * * * * * * * * * * * * * * * * * * * * basic operation * * * * * * * * * * * * * * * * * * * * * * * * * * * * *//

Bool Init_SqList (SqList& L)
{
L.e lem=(int *) malloc (L.s considering * sizeof (int));
if (! L.e lem)
return false;
The else
L.l ength=0;
return true;
}

Int Creat_SqList (SqList& L, int len)
{
If (len & gt; 100)
Return 10;

If (len & gt; L.s considering)
{
L.s considering=len;
L.e lem=(int *) realloc (L.e lem, L.s considering * sizeof (int));
}

Printf (" please input % d table data in turn: ", len);
for (int i=0; I & lt; Len. I++)
{
The scanf (" % d ", & amp; L.e lem [I]);
L.l ength++;
}
}

Int Insert_SqList (SqList& Int L, add, int num)
{
If (add<1 | | add> L.l ength + 1)//insert position error
Return 10;
If (L.l ength==L.s considering)
{
If (L.s considering==100)
The return of 20;//total length is full
The else
L.s ize++;
L.e lem=(int *) realloc (L.e lem, L.s considering * sizeof (int));
}

For (int I=L.l ength - 1; I & gt;=the add - 1; I -)
{
L.e lem [I + 1)=L.e lem [I];
}
L.e lem [add 1]=num;
L.l ength++;
return 1;
}

Int Delete_SqList (SqList& Int L, add)
{
If (add<1 | | add> L.l ength)//delete position error
Return 10;

Printf (" do you confirm delete? Confirmed, please press 1, deny, please press 0: ");
int a; The scanf (" % d ", & amp; A);
printf("\n");
If (a==0)
return 0;//exit delete

For (int I=add; I & lt; L.l ength - 1; I++)
{
L.e lem] [I - 1=L.e lem [I];
}
L.l ength -;
return 1;
}

Int Get_SqList (SqList& Int L, add)
{
If (add & gt;=1 & amp; The add & lt;=L.l ength)
Return L.e lem [add 1];

The else
return 0;
}

Int Find_SqList (SqList& L, int num)
{
for (int i=0; I & lt; L.l ength; I++)
{
If (L.e lem [I]==num)
Return the I + 1;
}

return 0;
}

//* * * * * * * * * * * * * * * * * * * * * * * * * * * application operation * * * * * * * * * * * * * * * * * * * * * * * * * * * * *//

Void Print (SqList& L)
{
for (int i=0; I & lt; L.l ength; I++)
Printf (" % d elements: % d \ n ", I + 1, L.e lem [I]);
}

Void the Create (SqList& L)
{
Int len, flag;
Printf (" please input the length of the sequence table: ");
The scanf (" % d ", & amp; Len);
Flag=Creat_SqList (L, len);
If (flag==01)
Printf ("!!! Length error!!!!!! \n");
Print (L);
printf("\n");
}

Void Insert (SqList& L)
{
Int the add, num, flag;
Printf (" please input you want to insert position: ");
The scanf (" % d ", & amp; Add);
Printf (" please input you want to insert data: ");
The scanf (" % d ", & amp; Num);

Flag=Insert_SqList (L, add, num);
The switch (flag)
{
Case 10: printf ("!!! Insert position error!!!!!! \n"); break;
Case 20: printf ("!!! Storage space is full!!!!!! \n"); break;
Case 1: printf (" * * * * * * insert success \ n "); break;
}
Print (L);
printf("\n");
}

Void the Delete (SqList& L)
{
Int the add, flag;
Printf (" please input you want to delete the position: ");
The scanf (" % d ", & amp; Add);
printf("\n");

Flag=Delete_SqList (L, add);
The switch (flag)
{
Case 10: printf ("!!! Delete the wrong location!!!!!! \n"); break;
Case 0: printf (" * * * * * * delete terminated \ n "); break;
Case 1: printf (" * * * * * * delete success \ n "); break;
}
If (flag==1)
Print (L);
printf("\n");
}

Void the Find (SqList& L)
{
Int num, flag;
Printf (" please input you want to find the value of the: ");
The scanf (" % d ", & amp; Num);
Flag=Find_SqList (L, num);
The switch (flag)
{
Case 0: printf ("!!! Didn't find it!!!!!! "); break;
Default: printf (" % d in the position of % d ", num, flag); break;
}
}

Void the Get (SqList& L)
{
Int the add, flag;
Printf (" please input you want to find the location of the: ");
The scanf (" % d ", & amp; Add);
Flag=Get_SqList (L, add);
The switch (flag)
{
Case 0: printf ("!!! Didn't find it!!!!!! "); break;
The position of the default: printf (" % d has a value of % d ", add, flag); break;
}
}

Void menu ()
{
Printf (" * * * * * * * * 1. Create 2. Insert the * * * * * * * * * \ n ");
Printf (" * * * * * * * * 3. Delete 4. Look for * * * * * * * * * \ n ");
Printf (" * * * * * * * * 5. 6. Value output * * * * * * * * * \ n ");
Printf (" * * * * * * * * 7. 8. Out of * * * * * * * * * \ n ");
}


Int main ()
{
SqList L;
Bool flag;
Int count=0;
A: flag=Init_SqList (L);
If (flag==true)
Printf (" * * * order table initialization successful * * * \ n ");
The else
{
Printf ("!!! [English] failed to initiate the order table \ n!!!!!! ");
count++;
If (count==3)
{
Printf ("!!! Don't play, old failure!!!!!! ");
return 0;
}
The else
Goto A;
}

While (1)
{
The menu ();
char a;
Printf (" please input your choice: ");
The scanf (" % d ", & amp; A);
printf("\n");
The switch (a)
{
Case 1: the Create (L); break;
Case 2: Insert (L); break;
Case 3: Delete (L); break;
Case 4: Find (L); break;
Case 5: Get (L); break;
Case 6: Print (L); break;
}
If (a==8)
{
Free (L.e lem);
Printf (" exit success ");
return 0;
}
}
}

CodePudding user response:

nullnullnullnullnullnullnull
  • Related