Experiment 2 linked list experiment odd even number classification

Posted by onthespot on Sat, 29 Jan 2022 18:50:05 +0100

Odd and even number classification based on linked list

1, Demand analysis

1. The data storage structure is linked list
2. The input form is keyboard, including numbers 1-9, spaces, and enter
3. The output form is print form
4. Contains the basic operations of the linked list
5. Be able to complete the sorting operation specified
6. It can separate odd and even numbers from a linked list
7. Ability to delete non specified numbers

2, Outline design

1, Abstract data type definition

ADT numbers{
Data object: {}
Data relation: {< A, b >}
Basic operation:
MakeNode(Link &p, ElemType e)
Operation result: assign the node with the value of e pointed by p
FreeNode(Link &p)
Operation result: release the node indicated by p
InitList(LinkList &L)
Operation result: initialize a linked list L
ClearList(LinkList &L)
Initial condition: linked list exists
Operation result: the linked list L is reset to an empty list and the node space of the original linked list is released
DestroyList(LinkList &L)
Initial condition: linked list L exists
Operation result: destroy the linked list L, and l no longer exists
InsFirst(LinkList &L,Link s)
Initial condition: linked list L exists
Operation result: insert the node indicated by s before the first node
GetCurElem(Link p)
Operation result: returns the value of the data element in the node indicated by p
Compare(Link p1, Link p2)
Operation result: if the element indicated by p1 is greater than p2, return true; otherwise, return FALSE
ExchangeData(Link &p1,Link &p2)
Operation result: exchange the data of two nodes
sort(LinkList &L)
Operation result: sort the linked list into non incremental linked list
DeleteNode(LinkList &L, Link s)
Initial condition: linked list L is not empty
Operation result: delete the node indicated by s
PrintList(LinkList &L)
Initial condition: linked list L exists
Operation result: print out the elements in the linked list on the screen
}

2, Macro definition part

#include<stdio.h>
#include<stdlib.h>
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0

3, Main program flow chart

4, Program structure diagram

3, Detailed design

1, Data type design

/*Definition of sequence table/
typedef struct LNode{/ / node type
ElemType data; // Data domain
struct LNode *next; // Pointer field
} *Link, *Position;

typedef struct {/ / linked list type
Link head; // Head node
Link tail; // Last node
int len; // Data element format in linear linked list
} LinkList;

2, Operation data

Status MakeNode(Link &p, ElemType e){
//Assign a node with the value e pointed by p. If the allocation fails, ERROR is returned
p = (Link)malloc(sizeof(LNode));
if(!p){
return ERROR;
}
p->data = e;
}

void FreeNode(Link &p){
//Release the node indicated by p
free§;
p = NULL;
}

Status InitList(LinkList &L){
//Construct an empty linear linked list L
Link p;
p = (Link)malloc(sizeof(LNode)); // Generate header node
if§{
p->next = NULL;
L.head = L.tail = p;
L.len = 0;
}
else
return ERROR;
}

Void clearlist (LinkList & L) {/ / reset the linear linked list L to an empty list and free the node space of the original linked list
Link p,q;
if(L.head!=L.tail) / / not an empty table
{
p=q=L.head->next;
L.head->next=NULL;
while(p!=L.tail)
{
p=q->next;
free(q);
q=p;
}
free(q);
L.tail=L.head;
L.len=0;
}
}

Status destroylist (LinkList & L) {/ / destroy the linear linked list L, and l no longer exists (see Figure 2.44)
ClearList(L); // Empty linked list
FreeNode(L.head);
L.tail=NULL;
L.len=0;
}

Status insfirst (LinkList & L, link s) {/ / L is added to the formal parameter because l needs to be modified
//H points to a node of L, takes h as the head node, and inserts the node referred to by s before the first node
Link h = L.head;
s->next=h->next;
h->next=s;
if(h==L.tail) // h points to the tail node
L.tail=h->next; // Modify tail pointer
L.len++;
}

Status Compare(Link p1, Link p2) {
if(p1->data > p2->data){
return TRUE;
}else{
return FALSE;
}
}

Void exchangedata (link & p1, link & p2) {/ / it is known that p points to a node in the linear linked list. Use e to update the value of the data element in the node indicated by p
ElemType temp = p1->data;
p1->data = p2->data;
p2->data = temp;
}

ElemType GetCurElem(Link p) {/ / it is known that p points to a node in the linear linked list and returns the value of the data element in the node indicated by p
return p->data;
}

void sort(LinkList &L){
for (int i = 0; i < L.len; i++)
{
Link p2 = L.head->next;
if(p2->next==NULL){
return;
}
Link p1 = L.head->next->next;
for (int j = 0; j <= L.len-2; j++)
{
if(Compare(p1,p2)){
ExchangeData(p1, p2);
}
p1 = p1->next;
p2 = p2->next;
}

}

}

Status DeleteNode(LinkList &L, Link s){
if(s == L.head || L.head == L.tail){
return FALSE;
}
if(s == L.tail){
Link p = L.head;
while(p->next!=L.tail){
p = p->next;
}
p->next = NULL;
L.tail = p;
FreeNode(s);
L.len–;
return TRUE;
}
Link p = L.head;
while(p->next!=s){
p = p->next;
}
p->next = p->next->next;
FreeNode(s);
L.len–;
return TRUE;
}

void PrintList(LinkList &L){
Link p = L.head->next;
for (int i = 0; i < L.len; i++)
{
printf("%d ", p->data);
p = p->next;
}

}

3, Operation function

void Function_ 1 (LinkList & L, int mink, int maxk) {/ / filter the data in the specified range
Link p1 = L.head;
while (p1->next!=NULL)
{
Link p2 = p1;
p1 = p1->next;
if(p1->data<mink || p1->data>maxk){
DeleteNode(L, p1);
p1 = p2;
}
}
}
void Function_ 2 (LinkList & l_a, linklist & l_b, linklist & l_c) {/ / odd and even numbers separated
Link p = L_A.head;
while (p->next!=NULL)
{
p = p->next;
if (p->data%2==1)
{
Link s;
MakeNode(s, p->data);
InsFirst(L_B, s);
}
else
{
Link s;
MakeNode(s, p->data);
InsFirst(L_C, s);
}
}
}

4, Main program

int main(){
LinkList L_A, L_B, L_C;
InitList(L_A);
InitList(L_B);
InitList(L_C);

char a;
int b;
scanf("%d", &b);
scanf("%c", &a);
while (a!='\n')
{
    Link p;
    MakeNode(p, b);
    InsFirst(L_A, p);
    scanf("%d", &b);
    scanf("%c", &a);
}
Link p;
MakeNode(p, b);
InsFirst(L_A, p);

sort(L_A);
Function_1(L_A, 0, 20);
Function_2(L_A, L_B, L_C);
sort(L_B);
sort(L_C);
printf("\nL_A:");
PrintList(L_A);
printf("\nL_B:");
PrintList(L_B);
printf("\nL_C:");
PrintList(L_C);

}

4, Debugging analysis

1, Main problems and Solutions
1. It is found that there is a problem in sorting. After debugging, it is found that the pointer does not point to the next element after the cycle is completed when traversing the linked list.
2. An error is reported in the filter function. After debugging, it is found that the pointer becomes a null pointer after the node is deleted and does not point to the next node again, resulting in an error.

5, User instructions

1, The running environment of this program is Microsoft Visual C++ 6.0 under Windows operating system.
2, After opening the program in VC environment, enter an integer separated by a space, and click "enter" to display the required results.
3, Press any key to continue.

6, Test results

7, Appendix

#include<stdio.h>
#include<stdlib.h>
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
typedef int Status;  /**Status Is a function type whose value is the status code of the function result, such as OK*/
typedef int ElemType;

/**Definition of sequence table*/
typedef struct LNode{ // Node Stereotypes 
    ElemType data; //Data domain
    struct LNode *next; //Pointer field
} *Link, *Position;

typedef struct { // Linked list type
    Link head; // Head node
    Link tail; // Last node
    int len; // Data element format in linear linked list
} LinkList;

Status MakeNode(Link &p, ElemType e){
    // Assign a node with the value e pointed by p. If the allocation fails, ERROR is returned
    p = (Link)malloc(sizeof(LNode));
    if(!p){
        return ERROR;
    }
    p->data = e;
}

void FreeNode(Link &p){
    // Release the node indicated by p
    free(p);
    p = NULL;
}

Status InitList(LinkList &L){
    // Construct an empty linear linked list L
    Link p;
    p = (Link)malloc(sizeof(LNode)); // Generate header node
    if(p){
        p->next = NULL;
        L.head = L.tail = p;
        L.len = 0;
    }
    else
        return ERROR;
}

void ClearList(LinkList &L){ // Reset the linear linked list L to an empty list and release the node space of the original linked list
    Link p,q;
    if(L.head!=L.tail) // Not an empty table
    {
        p=q=L.head->next;
        L.head->next=NULL;
        while(p!=L.tail)
        {
            p=q->next;
            free(q);
            q=p;
        }
        free(q);
        L.tail=L.head;
        L.len=0;
    }
}

Status DestroyList(LinkList &L){ // Destroy the linear linked list L, l no longer exists (see Figure 2.44)
    ClearList(L); // Empty linked list
    FreeNode(L.head);
    L.tail=NULL;
    L.len=0;
}

Status InsFirst(LinkList &L,Link s){ // L is added to the formal parameter because l needs to be modified
 // H points to a node of L, takes h as the head node, and inserts the node referred to by s before the first node
    Link h = L.head;
    s->next=h->next;
    h->next=s;
    if(h==L.tail) // h points to the tail node
        L.tail=h->next; // Modify tail pointer
    L.len++;
}

Status Compare(Link p1, Link p2) {
    if(p1->data > p2->data){
        return TRUE;
    }else{
        return FALSE;
    }
}

void ExchangeData(Link &p1,Link &p2){ // Given that p points to a node in the linear linked list, e is used to update the value of the data element in the node indicated by p
    ElemType temp = p1->data;
    p1->data = p2->data;
    p2->data = temp;
}

ElemType GetCurElem(Link p){ // It is known that p points to a node in the linear linked list and returns the value of the data element in the node referred to by p
    return p->data;
}

void sort(LinkList &L){
    for (int i = 0; i < L.len; i++)
    {
        Link p2 = L.head->next;
        if(p2->next==NULL){
            return;
        }
        Link p1 = L.head->next->next;
        for (int j = 0; j <= L.len-2; j++)
        {
            if(Compare(p1,p2)){
                ExchangeData(p1, p2);
            }
            p1 = p1->next;
            p2 = p2->next;
        }
        
    }
    
}

Status DeleteNode(LinkList &L, Link s){
    if(s == L.head || L.head == L.tail){
        return FALSE;
    }
    if(s == L.tail){
        Link p = L.head;
        while(p->next!=L.tail){
            p = p->next;
        }
        p->next = NULL;
        L.tail = p;
        FreeNode(s);
        L.len--;
        return TRUE;
    }
    Link p = L.head;
    while(p->next!=s){
        p = p->next;
    }
    p->next = p->next->next;
    FreeNode(s);
    L.len--;
    return TRUE;
}

void PrintList(LinkList &L){
    Link p = L.head->next;
    for (int i = 0; i < L.len; i++)
    {
        printf("%d ", p->data);
        p = p->next;
    }
    
}

void Function_1(LinkList &L, int mink, int maxk){
    //Filter out the data in the specified range
    Link p1 = L.head;
    while (p1->next!=NULL)      
    {
        Link p2 = p1;
        p1 = p1->next;
        if(p1->data<mink || p1->data>maxk){
            DeleteNode(L, p1);
            p1 = p2;
        }
    }
    
}

void Function_2(LinkList &L_A, LinkList &L_B, LinkList &L_C){
    //Separate odd and even numbers
    Link p = L_A.head;
    while (p->next!=NULL)
    {
        p = p->next;
        if (p->data%2==1)
        {
            Link s;
            MakeNode(s, p->data);
            InsFirst(L_B, s);
        }
        else
        {
            Link s;
            MakeNode(s, p->data);
            InsFirst(L_C, s);
        }   
    }
    
}


int main(){
    LinkList L_A, L_B, L_C;
    InitList(L_A);
    InitList(L_B);
    InitList(L_C);

    char a;
    int b;
    scanf("%d", &b);
    scanf("%c", &a);
    while (a!='\n')
    {
        Link p;
        MakeNode(p, b);
        InsFirst(L_A, p);
        scanf("%d", &b);
        scanf("%c", &a);
    }
    Link p;
    MakeNode(p, b);
    InsFirst(L_A, p);

    sort(L_A);
    Function_1(L_A, 0, 20);
    Function_2(L_A, L_B, L_C);
    sort(L_B);
    sort(L_C);
    printf("\nL_A:");
    PrintList(L_A);
    printf("\nL_B:");
    PrintList(L_B);
    printf("\nL_C:");
    PrintList(L_C);
}

Topics: data structure linked list