Single-chain table operation (Data Structure Experiment 1), single-chain table data structure experiment
Lab content
1. initialize a single-chain table with table header nodes.
2. Create a single-chain table with a table header node from the inserted node in the header. Set the element type in the table to integer, and input the element value from the keyboard.
3. Insert a node from the end of the table to create a single-chain table with a table header node. Set the element type in the table to integer, and input the element value from the keyboard.
4. Print a single-chain table with table header nodes.
5. Empty a single-chain table with table header nodes.
Code: (write the functions and provide the call method. You can modify the code according to the experiment requirements)
# Include <stdio. h> # include <stdlib. h> # include <malloc. h> # define M 100 typedef int Etype; // defines the type of the Node value of the single-chain table as integer typedef struct Node {Etype data; // The data field struct Node * link in the single-chain table; // pointer field of a single-chain table} Node; typedef Node * List; // defines a single-chain table List BuildList1 (); List BuildList2 (); void PrintList (List first ); void clear (List * first); // create a linked List by using the header insertion method. List BuildList1 () {Node * L; Etype x, n; L = (Node *) malloc (sizeof (Node); // Request Header Node space L-> link = NULL; // Initialize an empty linked list printf ("Enter the number of elements: \ n"); scanf ("% d", & n); printf ("Enter the elements: \ n "); // x is the data in the Linked List data field for (int I = 0; I <n; I ++) {scanf (" % d ", & x ); node * p; p = (Node *) malloc (sizeof (Node); // apply for a new Node p-> data = x; // assign a value to the node data field p-> link = L-> link; // Insert the node to the header L --> | 2 | --> | 1 | --> null l-> link = p;} return L ;} // create the List BuildList2 () {Node * L, * r; Etype n, x; L = (Node *) malloc (sizeof (Node) by means of the End Plug-in )); // Request Header Node space L-> link = NULL; // Initialize an empty linked list printf ("Enter the number of elements: \ n"); scanf ("% d", & n); r = L; // r always points to the terminal node, start with head node printf ("Please input element: \ n"); for (int I = 0; I <n; I ++) {scanf ("% d ", & x); Node * p; p = (Node *) malloc (sizeof (Node); // apply for a new Node p-> data = x; // assign a value to the node data field r-> link = p; // Insert the node to the header L --> | 1 | --> | 2 | --> NULL r = p ;} r-> link = NULL; return L;} // output linked List void PrintList (List first) {Node * Li; if (first = NULL) {printf ("the linked list is empty! \ N "); return;} Li = first-> link; while (Li! = NULL) {printf ("% d", Li-> data); Li = Li-> link;} printf ("\ n ");} // clear the linked table void clear (List * first) {Node * p = * first; while (* first) {p = (* first)-> link; free (* first); * first = p;} int main () {Node * L; L = BuildList1 (); // call BuiltdList1 (prefix) create a single-chain table algorithm PrintList (L); // print a single-chain table clear (& L); // clear a single-chain table L = BuildList2 (); // call BuiltdList2 (post-insertion) create a single-chain table algorithm PrintList (L); // print a single-chain table clear (& L); // clear a single-chain table}
Data structure machine Experiment (single-chain table)
It's only 10 minutes to get things ....
This program is mainly used for single-chain table operations!
# Include <iostream>
# Include <iomanip>
Using namespace std;
Struct node {
Double data;
Struct node * next;
};
Struct node * create ();
Struct node * rescreate ();
Void printlist (struct node * head );
Int len (struct node * head );
Void insertnode (struct node * head, int I, double x );
Void delnode (struct node * head, double x );
Void delnode2 (struct node * head, int I );
Void sort (struct node * head );
Int main ()
{
Struct node * p;
P = rescreate ();
Printlist (p );
// Cout <endl <len (p) <endl;
Insertnode (p, 3,-100 );
Cout <endl;
Printlist (p );
Delnode2 (p, 4 );
Cout <endl;
Printlist (p );
Sort (p );
Cout <endl <"After sort \ n ";
Printlist (p );
System ("pause ");
Return 0;
}
Struct node * create ()
{
Struct node * head, * oldp, * p, * s;
Double newnum;
Head = new struct node;
Oldp = head;
Cin> newnum;
While (newnum! =-32768 ){
P = new struct node;
P-> data = newnum;
Oldp-> next = p;
Oldp = p;
Cin> newnum;
}
S = head;
Head = head-> next;
Delete s;
P-> next = head;
Return head;
}
Struct node * rescreate ()
{
Struct node * tail, * oldp, * p;
Double newnum;
Tail = NULL;
Oldp = tail;
Cin> newnum;
While (newnum! =-32768 ){
P = new struct node;
P-> data = newnum;
P-> next = oldp;
Oldp = p;
Cin> newnum;
}
Return p;
}
Void printlist (struct node * head)
{
While (head ){
Cout <setw (5) Head = head-> next;
}
}
Int len (struct ...... remaining full text>
Data Structure Single-chain table experiment (C language version)
My sophomore year's experiment is still... However, I 'd like to say that IT is not suitable for reaching out to the party. I suggest you do more exercises on your own. You won't be able to ask. There are many people on the Internet who are willing to help you solve the problem.
# Include "stdio. h "# include" string. h "# include" ctype. h "# include" stdlib. h "# include" io. h "# include" math. h "# include" time. h "# define OK 1 # define ERROR 0 # define TRUE 1 # define FALSE 0 # define MAXSIZE 20/* Initial bucket allocation */typedef int Status; /* Status is the function type, and its value is the function result Status code, such as OK */typedef int ElemType;/* The ElemType depends on the actual situation, assume int */Status visit (ElemType c) {printf ("% d", c); return OK;} typedef struct Node {ElemType data; struct Node * next;} Node; typedef struct Node * LinkList;/* define the LinkList * // * initialize the ordered linear table */Status InitList (LinkList * L) {* L = (LinkList) malloc (sizeof (Node);/* generate a header Node and point L to this header Node */if (! (* L)/* storage allocation failed */return ERROR; (* L)-> next = NULL;/* the pointer field is blank */return OK ;} /* initial condition: the sequence linear table L already exists. Operation Result: if L is empty, TRUE is returned. Otherwise, FALSE */Status ListEmpty (LinkList L) {if (L-> next) return FALSE; else return TRUE;} is returned ;} /* initial condition: the sequence linear table L already exists. Operation Result: reset L to an empty table */Status ClearList (LinkList * L) {LinkList p, q; p = (* L)-> next; /* p points to the first node */while (p)/* not to the end of the table */{q = p-> next; free (p); p = q ;} (* L)-> next = NULL;/* the header node pointer field is NULL */return OK;}/* initial condition: the ordered linear table L already exists. Operation Result: returns the number of data elements in L */int ListLength (LinkList L) {int I = 0; LinkList p = L-> next; /* p points to the first node *&#...... remaining full text>