[Learning] recorded experimental data structure of Guangzhou University

Experimental data structure

In order to master the data structure of this course, I rebuilt the course of the experiment, in order to lay the basic skills.

Experiment 1: Storage Structure

  1. Linear table lists experimental
    1. Randomly generated list into three integer between 10 100-999. I use the cycle chain, with a single list is not very different, but the last element is the last point of the first node.
void Create (LinkNode * & L) // given a first node, an output list 
{// reverse head interpolation 
	L = new new LinkNode; 
	L-> Data = 0; 
	L-> Next = L; // Start the head node pointing their 
	LinkNode * S; 
	int X = 100; 
	int Y = 999; 
	srand ((unsigned). 3); 
	for (int I = 0; I <10; I ++) 
	{ 
		S = new new LinkNode; 
		S-> Data = ( rand ()% (y-x + 1)) + x; // this one, simply modify the values of x, y of the upper and lower limits can be directly modified 
		S-> Next = L-> Next; 
		L-> Next = S; 
		
	 } 
}

 

 

 

[10.27] to complete the creation of a single list, in ascending insert elements

#include"LinkList.h"

using namespace std;

void Insert(LinkNode *L,int num)
{
    LinkNode * p = L-> next, * pre = L, * Tem; // p points to the first node for comparing, pre pointing toward the front of the point p. Tem is a new node
    LinkNode = new;
    Tem-> data = num; // put to value Tem node, otherwise problems
    if (L-> next == NULL) // L when the node is not inserted
    {
        Tem->next = NULL;
        L->next = Tem;
        return;
    }
    while (p && (p-> data <num)) // find the right position of the insert
    {
        for p =;
        p = p->next;
    }
    if (p == NULL) // not find a suitable location until they came to the final list
    {
        pre->next = Tem;
        Tem->next = NULL;
    }
    else // find a suitable location
    {
        Tem->next = p;
        pre->next = Tem;
    }
    
}

void Display(LinkNode *L)
{
    LinkNode * o;
    o = L->next;
    while(o != NULL)
    {
        cout<<o->data<<" ";
        o = o->next;
    }
}
void Create(LinkNode *L)
{
    L = new LinkNode;
    L->data = 0;
    L-> next = NULL; // initialize L
    int num = 0;
    cout << "Please enter the element to be inserted, the end of the input-1" << endl;
    cin>>num;
    while(num != -1)
    {
        Insert(L,num);
        L->data = L->data +1;
        cin>>num;
    }
    cout << "input completed, list elements as follows:" << endl;
    Display(L);
}

 
#include"stdio.h" 
#include<iostream>
#include<stdlib.h>

struct LinkNode
{
    LinkNode *next;
    int data;
 }; 

void Insert(LinkNode *L, int num);
void Display(LinkNode *L);
void Create(LinkNode *L);
View Code
 1 #include"LinkList.h"
 2 
 3 using namespace std;
 4 
 5 
 6 int main()
 7  {
 8     LinkNode *L = NULL;
 9     Create(L);
10     return 0;
11  }

 

 

 

Guess you like

Origin www.cnblogs.com/wymannpan/p/11615518.html