Home > Back-end >  Data structure problem
Data structure problem

Time:09-29

Open the linked list. "CPP" file, the code according to the request of the following three TODO in complete
A, filling function ShowList implementation will singly linked lists all the elements of the output,
Two, filling function ListLength singly linked lists to table a long operation, return a list of list of long,
Three, the main function:
1) establish a including head nodes and six (6) singly linked lists, singly linked lists to establish basic operations,
2) will display output to the singly linked list of all element,
3) singly linked lists have been built in the specified location (I=3) insert a node 99, realize the basic operations of a singly linked list insert and to display all the elements in the singly linked list
4) in a singly linked list of the specified location (such as I=2) delete a node, realize the basic operations of a singly linked list to delete, to display all the elements in the singly linked list
5) output the table long singly linked lists,
6) in a singly linked list to find five and 99
 
#include
#include
#include
#include
using namespace std;

# define OK 1
# define the ERROR 0
# define OVERFLOW - 2
Typedef int the Status;//the Status is the function return value type, its value is the function result Status code,
Typedef int ElemType;//ElemType to define data types, this is set to int


Typedef struct LNode {
ElemType data;//node data fields
Struct LNode * next;//node pointer domain
} LNode * LinkList;//LinkList for pointer to structure LNode type



The Status InitList (LinkList & amp; 2.6 L) {//algorithm singly linked lists of initialization
//constructs an empty singly linked lists L
L=new LNode;//generated new node as the head node, with the head pointer pointing to L head node
L-> Next=NULL;//head node pointer field empty
return OK;
}

Status GetElem (LinkList L, int I ElemType & amp; E) {//algorithm 2.7 singly linked lists of values
//in the lead the singly linked list of nodes L find the ith element
With e//returns the value of the ith a data element in L
int j;
LinkList p;
P=L - & gt; next;
J=1;//initialization, pointing to the first node p, j for the counter
While (j & lt; I & amp; & P) {//arranges backward scan chain domain until p points to the ith element or p is empty
P=p - & gt; next;//p points to the next node
+ + j;//counter j + 1
}
if (! P | | j & gt; I)
Return the ERROR;//I value is not legal I> n or i<=0
E=p - & gt; The data;//take the case of a node data fields I
return OK;
}//GetElem

ElemType LNode * LocateElem (LinkList L, e) {//algorithm according to the value of 2.8 for
//in the lead the singly linked list of nodes L find the value of e element
LinkList p;
P=L - & gt; next;
While (p & amp; & p-> The data!=e)//suitable chain domain scanning back, until the p is empty or referred to in p nodes data domain equal to e
P=p - & gt; next;//p points to the next node
return p;//find success node address of the value of e p, lookup failure p is NULL
}//LocateElem

The Status ListInsert (LinkList & amp; L, int I ElemType 2.9 e) {//algorithm singly linked lists of insert
//in the lead the singly linked list of nodes L the position I insert values for e new node
int j;
LinkList p, s;
P=L;
J=0;
While (p & amp; & J & lt; I - 1) {
P=p - & gt; next;
+ + j;
}//find the I? A node, p points to the node
if (! P | | j & gt; I - 1)
Return the ERROR;//I> I S=new LNode;//generated new node * s
S - & gt; Data=https://bbs.csdn.net/topics/e;//the node * s data fields set to e
S - & gt; Next=p - & gt; next;//will nodes * s domain to the ai
p-> Next=s;//the node * p * s domain to the nodes
return OK;
}//ListInsert

The Status ListDelete (LinkList & amp; 2.9 L, int I) {//algorithm singly linked list of deleted
//in the lead the singly linked list of nodes L, delete the position I
LinkList p, q;
int j;
P=L;
J=0;
While ((p - & gt; Next) & amp; & (j & lt; I - 1))//find the I? A node, p points to the node
{
P=p - & gt; next;
+ + j;
}
if (! (p - & gt; Next) | | (j & gt; I - 1))
Return the ERROR;//when i> N or i<1, remove position is unreasonable
Q=p - & gt; next;//temporary preservation is delete node address for released
p-> Next=q - & gt; next;/change/delete nodes precursors of pointer field
The delete q;//release delete node space
return OK;
}//ListDelete

Void CreateList_H (LinkList & amp; L, int n) {//algorithm 2.11 forward method to create a singly linked list
//inverse order input values of n elements, to establish an end singly linked list of nodes L
LinkList p;
int i;
L=new LNode;
L-> Next=NULL;//to build a lead the first node of empty list
for(i=0; i{
P=new LNode;//generate new node * p
Cin & gt;> p-> The data;//input element value is assigned to the new node * p data domain
p-> Next=L - & gt; next;
L-> Next=p;//insert the new node * p after the end node

}

}//CreateList_H

Void CreateList_R (LinkList & amp; L, int n) {//algorithm 2.12 after interpolation to create singly linked list
//input is a sequence of n elements values, establish a singly linked list of nodes with header L
LinkList p, r;
int i;
L=new LNode;
L-> Next=NULL;//to build a lead the first node of empty list
R=L;//the tail pointer r to head node
for(i=0; i{
P=new LNode;//generated new node
Cin & gt;> p-> The data;//input element value is assigned to the new node * p data domain
p-> Next=NULL;
R - & gt; Next=p;//insert the new node * p * r end nodes after
R=p;//r point to the new end node * p
}

}//CreateList_R

Void ShowList (LinkList L)
{//to display all the elements in the singly linked list
//TODO

}

Int ListLength LinkList (L)
{//implementation singly linked lists of long operating table
//TODO

}

Int main () {

//TODO

return 0;
}
  • Related