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.
#define LIST_INIT_SIZE 100

typedef struct {
	int *elem;
	int length;

int InitList(Sqlist &L){
	//Construct Sequence Table
		return 0;
	return 1;
void readList(Sqlist &L){
	int i,len;
	printf("Enter the length of the order table:");
	L.length = len;
	printf("Please enter an orderly increasing order table:");
int InsertElem(Sqlist &L,int e){
	int i,j;
	for(i=0; i<L.length; i++){
		if(L.elem[i] > e){//Find the insertion position i for element e 
		L.elem[j+1] = L.elem[j];//Start at position i, then move backwards to make room for insertion
	L.length++; // Length+1 
	return 1;
void writeList(Sqlist &L){
	int i; 
		printf("%d ",L.elem[i]);
int main(){
	int x;
	Sqlist L;
	writeList(L); // View the order table of inputs
	printf("Please enter a number to insert:");
	getchar();//Eat the Enter key
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. . .


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

//Create Polynomial 
Linklist creatPolyn(){
	Linklist  head,p,q;
	int m;
	head = (Linklist)malloc(sizeof(list));
	head->next= NULL;
	q = head;
	printf("Enter the number of terms of the polynomial:");
	for(int i=0;i<m;i++){
		p = (Linklist)malloc(sizeof(list));
		printf("Enter #%d Coefficient of item:",i+1);
		printf("Enter #%d Index of item:",i+1);
		p->next = q->next;
		q->next = p;
		q = q->next;
	return head;
//Polynomial Addition 
Linklist AddPolyn(Linklist pa,Linklist pb){
	Linklist a,b,pc,c;
	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 
		int m,n,sum;
		m=a->e; n=b->e; // m,n save exponents of a and b, respectively 
			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;
				a->c = sum;
				pc->next = a;
				//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;
			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 
		pc->next = a;
		pc->next = b;
	return c;
void printList(Linklist L){
	while(L != NULL){
		printf("%d*X^%d ",L->c,L->e);
		L = L->next;
int main(){
	Linklist pa,pb,pc;
	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");
	printf("The second polynomial entered is:\n");
	pc = AddPolyn(pa->next,pb->next);
	printf("The sum of the two polynomials is:");
	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. . .


typedef struct list{
	int data;
	struct list *next;
//Building a circular chain table date is 1,2,3,4,5,6,7..... 
void InitLink(Linklist &L,int n){
	L = (list*)malloc(sizeof(list));//No header node
	L->next = L;
	L->data = 1;
	Linklist p;
	for (int i = n; i > 1; i--)
		p= (Linklist)malloc(sizeof(list));
		p->next = L->next;
		L->next = p;
		p->data = i;
void outList(Linklist L,int s,int m,int n){
	//list* p = L;
	Linklist p = L;
	Linklist q;
	int i;
	}//Find the location of s and record it with p 
	printf("The order in which the rings appear in turn is:");
	for(i=0;i<n;i++){//n laps 
		int j = 1;
			p = p->next;//Report the number in turn, and end the loop when j=m. Find the corresponding p-node
		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 
int main(){
	int m,n,s;
	printf("Enter the number of people around the table:");
	printf("Please enter the starting number:");
	printf("Please enter the number reported:");
	list* L;
	printf("Circular Chain List:\n");
	for(int q=0;q<n;q++){
		printf("L->data = %d\n",L->data);
		L = L->next;
3.	Run Results
 Enter the number of people around the table: 10
 Please enter the starting number:2
 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
 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