# Data Structure Experiment 1 (Linear Table)

Keywords: data structure

#### 1. Experimental Purpose and Requirements

The main purpose of this internship is to enable students to master the basic operation of linear tables in order storage structure and chain storage structure, and to improve the analysis and problem solving ability. You are required to read and understand the following examples carefully, go through the computer and observe the results, then complete the following exercises independently.

#### 2. Experimental Contents and Principles

Briefly describe what the problem is to be solved in the topic, and explain the form of input and output data.
The basic idea of storage structure and algorithm is briefly described.

Question 1: Insert element e at the corresponding position of an incremental sequential table and use the scanf function to read the data in turn. Using the printf function in the for loop, output the modified sequential table in turn

Question 2: The sum of two polynomials stored in a single-chain table. The input uses the scanf function, and the loop body of the for loop is p->next = q->next; Q->next = p; Q = q->next; The output uses the printf function, the loop of the for loop: p=p->next;.

Question 3: Circular chain table solves the problem of Joseph rings (Round table problem). Each node consecutively saves 1,2,3,4,5...After the specified number is reported, the data of the node is modified to the data of the next node and points to the next node of the next node to delete the current node. The input uses a for loop to generate a new node and inserts it into the next of the current node. Output uses: When the current node is the specified number of reports, the data value of the node is output.

##### Basic Ideas:

In a sequential table, data is stored in a contiguous space, which can be invoked directly through the index value. This is convenient but can not expand the space at will. The algorithm design is simple.

In a chain table, each node has a data field and a next field, different nodes, physical storage location is not adjacent, through the next to save the address of the next node, you can expand at will, but in the call process, you must start by the head node to find in turn. In algorithm design, the main points are: to understand where the current node and next node are, and when deleting and inserting nodes, understand: p->next = p->next->next.

#### 3. Origin

1. Set the data elements in order table A to be incrementally ordered. Try writing a program that inserts x into the appropriate place in the order table so that the table is still ordered.
2. Store polynomial A(x)=a0+a1x1+a2x2+...+anxn (where aI is a non-zero factor) with single-chain table ha and polynomial B(x)=b0+b1x1+b2x2+...+bmxm (where bj is a non-zero factor), requiring C(x)= A(x)+B(x) to be calculated and the results stored in single-chain table hc. Try writing a program.
3. There are n people sitting around a round table. They will start counting from the sth person, count to the mth person, then start counting again from the next person listed, count to the m person will be listed again, repeat this until all the people are listed. The Josephus problem is that for any given n, m, s, the order table of n people in the column order is found.

#### 4. Operation methods and experimental procedures

##### Lists the source programs that passed debugging.
```1.topic
#include<stdio.h>
#include<stdlib.h>
#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10

typedef struct {
int *elem;
int length;
}Sqlist;

int InitList(Sqlist &L){
//Construct Sequence Table
L.elem=(int*)malloc(20*sizeof(int));
if(!L.elem)
return 0;
L.length=0;
return 1;
}
int i,len;
i=0;
printf("Enter the length of the order table:");
scanf("%d",&len);
L.length = len;
printf("Please enter an orderly increasing order table:");
for(i=0;i<L.length;i++){
scanf("%d",&L.elem[i]);
getchar();
}
}
int InsertElem(Sqlist &L,int e){
int i,j;
i=0;
for(i=0; i<L.length; i++){
if(L.elem[i] > e){//Find the insertion position i for element e
break;
}
}
for(j=L.length-1;j>=i;j--)
L.elem[j+1] = L.elem[j];//Start at position i, then move backwards to make room for insertion
L.elem[j+1]=e;
L.length++; // Length+1
return 1;
}
void writeList(Sqlist &L){
int i;
for(i=0;i<L.length;i++){
printf("%d ",L.elem[i]);
}
printf("\n");
}
int main(){
int x;
Sqlist L;
InitList(L);
writeList(L); // View the order table of inputs
printf("Please enter a number to insert:");
scanf("%d",&x);
getchar();//Eat the Enter key
InsertElem(L,x);
writeList(L);
}
1.	Run result:
Please enter the length of the order table: 6
Please enter an orderly increasing order table: 1 2 3 6 9 13
1 2 3 6 9 13
Please enter the number to insert: 5
1 2 3 5 6 9 13

--------------------------------
Process exited after 16.66 seconds with return value 0
Press any key to continue. . .

```
```2.topic
#include<stdio.h>
#include<stdlib.h>

typedef struct list{
int c;//coefficient
int e;//index
struct list *next;

//Create Polynomial
int m;
printf("Enter the number of terms of the polynomial:");
scanf("%d",&m);
for(int i=0;i<m;i++){
printf("Enter #%d Coefficient of item:",i+1);
scanf("%d",&p->c);
printf("Enter #%d Index of item:",i+1);
scanf("%d",&p->e);
p->next = q->next;
q->next = p;
q = q->next;
}
}
a = pa;//Save pa Header Node
//printf("pa->c = %d pa->e = %d\n",pa->c,pa->e);
b = pb;//Save pb's header node
//Save the result of adding pa, pb polynomials in c
c = (Linklist)malloc(sizeof(list)); //Apply for a space for C
c->next = NULL; // Generate Head Nodes
pc = c;//Give the head node to PC for easy operation afterwards! Without the variable pc, if you manipulate c directly, the result of return c will be the last one, because the address of c has changed in the process
while(a&&b){
int m,n,sum;
m=a->e; n=b->e; // m,n save exponents of a and b, respectively
if(m>n){
pc->next = b;
pc = pc->next;
b = b->next;
//printf("b polynomial exponent is small%d*X^%d\n", pc->c, pc->e);
}
else if(m == n){
sum = a->c + b->c;
if(sum!=0){
a->c = sum;
pc->next = a;
pc=pc->next;
//printf("polynomial exponent as large as%d*X^%d\n", pc->c, pc->e);
}
//There's a small bug here that I won't fix! The bug is that when the most of the two polynomials sum to zero, the result is the last of the polynomials with more terms
//a,b have been processed and moved one bit back.
a = a->next;
b = b->next;
}
else{
pc->next = a;
pc = pc->next;
a = a->next;
//printf("a polynomial exponent is small%d*X^%d\n", pc->c, pc->e);
}

}
// Judge that polynomial did not go to NULL and point pc->next directly at the remaining polynomial
if(a!=NULL){
pc->next = a;
}
if(b!=NULL){
pc->next = b;
}
return c;
}
while(L != NULL){
printf("%d*X^%d ",L->c,L->e);
L = L->next;
}
printf("\n");
}
int main(){
printf("Please enter the coefficients and exponents of the first polynomial\n");
pa = creatPolyn();
printf("Please enter the coefficients and exponents of the second polynomial\n");
pb = creatPolyn();
printf("The first polynomial entered is:\n");
printList(pa->next);
printf("The second polynomial entered is:\n");
printList(pb->next);
printf("The sum of the two polynomials is:");
printList(pc->next);
return 0;
}
2.	Run result:
Please enter the coefficients and exponents of the first polynomial
Please enter the number of terms of the polynomial:3
Enter the coefficient of item 1: 1
Enter the index for item 1:1
Enter the coefficients for item 2:-5
Enter the index for item 2:5
Enter a factor for item 3: 7
Enter the index for item 3:7
Please enter the coefficients and exponents of the second polynomial
Please enter the number of terms of the polynomial:4
Enter the coefficient of item 1: 1
Enter the index for item 1:1
Enter the coefficient of item 2: 4
Enter the index for item 2:4
Enter a factor of item 3: 8
Enter the index for item 3: 8
Enter the factor for item 4: 9
Enter the index for item 4:9
The first polynomial entered is:
1*X^1 -5*X^5 7*X^7
The second polynomial entered is:
1*X^1 4*X^4 8*X^8 9*X^9
The sum of two polynomials is:2*X^1 4*X^4 -5*X^5 7*X^7 8*X^8 9*X^9

--------------------------------
Process exited after 20.26 seconds with return value 0
Press any key to continue. . .

```
```3.topic
#include<stdio.h>
#include<stdlib.h>

typedef struct list{
int data;
struct list *next;
//Building a circular chain table date is 1,2,3,4,5,6,7.....
L->next = L;
L->data = 1;
for (int i = n; i > 1; i--)
{
p->next = L->next;
L->next = p;
p->data = i;
}
}
void outList(Linklist L,int s,int m,int n){
//list* p = L;
int i;
for(i=1;i<=n;i++){
if(p->data==s){
break;
}
p=p->next;
}//Find the location of s and record it with p
printf("data:%d\n",p->data);
printf("The order in which the rings appear in turn is:");
for(i=0;i<n;i++){//n laps
int j = 1;
while(j<m){
p = p->next;//Report the number in turn, and end the loop when j=m. Find the corresponding p-node
j++;
}
printf("%d ",p->data);
// Output the data domain of the current p-node, and modify the current data domain to be the data domain of the next node, pointing to the next node at the same time! Plays the effect of deleting the current node
q= p->next;//Save the next node of p with q and release it
p->data = p->next->data;//Change the data field of p to the data field of the next node
p->next = p->next->next;//Point to Next Node
free(q);
}
}
int main(){
int m,n,s;
printf("Enter the number of people around the table:");
scanf("%d",&n);
scanf("%d",&s);
scanf("%d",&m);

list* L;

printf("Circular Chain List:\n");
for(int q=0;q<n;q++){
printf("L->data = %d\n",L->data);
L = L->next;
}
outList(L,s,m,n);
}
3.	Run Results
Enter the number of people around the table: 10
Please enter the number reported: 3
Circular Chain List:
L->data = 1
L->data = 2
L->data = 3
L->data = 4
L->data = 5
L->data = 6
L->data = 7
L->data = 8
L->data = 9
L->data = 10
data:2
The order of ringing out in sequence is: 47 10 3 8 2 9 6 1 5
--------------------------------
Process exited after 4.362 seconds with return value 0
Press any key to continue. . .

```

Posted by mrhinman on Sat, 20 Nov 2021 15:43:23 -0800