Focus: Practice single-linked list initialization, find, insert, delete operations, focus on the creation of linked list of the pre-interpolation and post-interpolation method two ways.
1. Single-linked list storage structure
The storage structure of-----single-linked list-----
typedef struct lnode{
Elemtype data fields for date;//nodes
The pointer field of struct Lnode *next;//node
}lnode,*linklist;//linklist is a pointer to the Lnode type
2. Implementation of basic operation of single-link list
Initialization of-----Single-linked list-----
Status initlist_l (linklist &l) {//construct an empty single-linked list L
L=new lnode;//generate new nodes as head nodes
l->next=null;//the pointer field of the head node is empty
return OK;
}
-----Search by ordinal-----
Status getelem_l (linklist l,int i,elemtype &e) {//Find the I element in a single-linked list of L in the head knot
p=l->next;j=1;
while (P&&j<i) {
p=p->next;
++j;
}
if (!p| | J>i) return error;//element I does not exist
e=p->date;
return OK;
}
-----Find by Value-----
Lnode *locateelem_l (linklist l, elemtype e) {//Find the element with the value E in the single-linked table L of the lead node
p=l->next;
while (p&&p->date!=e) {
p=p->next;
}
return p;
}
-----single-linked list insertion-----
Status linkinsert_l (linklist &l,int i,elemtype e) {//Insert element E in the first position of the single linked list L in the lead knot
p=l;j=0;
while (p&&j<i-1) {
p=p->next;
++j;
}
if (!p| | J>I-1) return ERROR;
S=new Lnode;
s->date=e;
s->next=p->next;
p->next=s;
return OK;
}
-----The deletion of a single linked list-----
Status linkdelete_l (linklist &l,int i,elemtype &e) {//Remove the L element of I and assign to E
p=l,j=0;
while (p->next&&j<i-1) {
p=p->next; ++j;
}
if (! ( P->next) | | j>i-1; Return error;//i greater than +1 or less than 1
q=p->next;
p->next=q->next;
e=q->date;
Delete q;
return OK;
}
Create a linked list-----The pre-interpolation method-----
void Creatlist_f (linklist &l, int n) {//reverse order Input n element value, establish single-link table L with lead node
L=new Lnode;
l->next=null;
for (i=n; i>0;-I.) {
P=new Lnode;
cin>>p->date;
p->next=l->next; l->next=p;
}
}
Note: The forward interpolation method in reverse order to enter the value of n elements, combined with a diagram easier to understand the pre-interpolation method.
Icon:
Create a linked list-----The interpolation method-----
void creatlist_l (linklist &l,int N) {//positive-order input N-element values, set up single-linked list L with lead node
L=new Lnode;
l->next=null;
R=l;
for (i=0; i<n; i++) {
P=new Lnode;
cin>>p->data;
p->next=null;r->next=p;
}
}
Summary: Both time complexity are O (n), but the post-interpolation method is easier to understand than the previous interpolation method, but the former interpolation method has its usefulness in specific situations, and should be mastered.
Title Description:
design an algorithm that, through a traversal, reverses the link direction of all nodes in the list and still uses the storage space of the original table.
#include <iostream> using namespace std;
typedef struct lnode{int data;
struct Lnode *next;
}lnode, *linklist; void Creatlist (linklist L) {//Create a list linklist p,q;//p, q for struct pointer type p=l;//create head node L cout<< "Number of input nodes" <<end
L int n;
cin>>n;
for (int i=0; i<n; i++) {q=new lnode;
cin>>q->data;
p->next=q;
p=q;
} p->next=null;
Return
} void Reverselist (Linklist L) {//using the pre-interpolation method to reverse the node order without changing the storage space linklist p,q;
p=l->next;
l->next=null;
while (p!=null) {q=p->next;
p->next=l->next;
l->next=p;
p=q;
}} void Listtraverse (Linklist L) {//Traverse list linklist p;
p=l->next;
while (p!=null) {cout<<p->data<< "";
p=p->next;
} cout<<endl;
return;
} int main () {Lnode L;
Creatlist (&L);
Reverselist (&L);
Listtraverse (&L);
while (1); }
Output Result: