Current location - Loan Platform Complete Network - Big data management - Number structure programming question for big help in C?
Number structure programming question for big help in C?

Linear tables are implemented as arrays and single linked tables are implemented as structures with linked table pointers.

Here data entry, I use random numbers to generate two sets of -99 to 99 two digit numbers to fill the linear table and single linked table, using bubble sort ascending order, so that the negative numbers are to the front of the positive numbers. In two functions:

#include <stdio.h>

#include <stdlib.h>

#include <time.h>

#define M 10/Maximum number of data

typedef struct llist

{

int n;

struct llist *next;

}LLT;

void doByST();//do it with a sequential table

void doByLLT();//do it with a singly linked table<

int main()

{

srand(time(NULL));//use random numbers (positive and negative two-digit numbers -99~99) to populate sequential and single linked tables

doByST();

doByLLT();

}

void doByST()//do with sequential table

{

int i,j,st[M];

printf("Data in sequential table: \n");

for(i=0;i<M;i++) st[i]=((rand()%2)?1:-1)*(rand()% 90+10),printf("%d ",st[i]);

printf("\n puts all negative values in front of all positive values: \n");

for(i=0;i<M;i++)

for(j=i+1;j<M;j++)

if(st[i ]>st[j])

st[i]^=st[j],st[j]^=st[i],st[i]^=st[j];

for(i=0;i<M;i++) printf("%d",st[i]);//print result

printf("\n\n");

}

void doByLLT()//do it with a single linked table

{

int i;

LLT llist[M],*lltp=llist,*lltp2=NULL;

for(i=0;i<M-1;i++) llist[i].next=&llist[i+1];//single linked table

llist[i].next=NULL;

printf("Data in \n single linked table: \n");

while(lltp!=NULL)

{

lltp->n=((rand()%2)?1:-1)*(rand()%90+10);

printf("%d ",lltp->n);

lltp=lltp->next;

}

printf("\n put all the negative values to precede all positive values\n");

lltp=llist;

while(lltp!=NULL)

{

lltp2=lltp->next;

while(lltp2!=NULL)

{

< p> if(lltp->n>lltp2->n)

lltp->n^=lltp2->n,lltp2->n^=lltp->n,lltp->n^=lltp2->n;

lltp2=lltp2-& gt;next;

}

lltp=lltp->next;

}

lltp=llist;//--Print the result

while(lltp!=NULL) printf("%d ",lltp->n),lltp= lltp->next;

}