#include
#include
Typedef int Elemtype;
Typedef struct LNode//define a structure
{
Elemtype data;//node data fields
struct LNode *next;//node pointer field
} LNode * LinkList;//pointer type
Int InitList (LinkList L)//singly linked lists with a head node initialization
{
L=(LinkList) malloc (sizeof (LNode));
if (! L)//storage allocation failure out
{
Printf (" allocates memory failed! \n");
exit(0);
}
L - & gt; Next=NULL;//the length and width of the table is null
return 0;
}
Int LengthList LinkList (L)//length calculation list
{
Int length=0;
LinkList p;
P=L - & gt; Next;
While (p)
{
Length++;
P=p - & gt; Next;
}
Return length;
}
Int GetElem (int I LinkList L, Elemtype e)//returns the ith a L e element value
{
LinkList p;//initialization, p to first finally, counter j=1
Int j=1;
P=L - & gt; Next;//order list scan back until p is empty or p points to the ith element
While (p & amp; & J & lt; I)
{
P=p - & gt; Next;//p points to the next element
+ + j;
}
if (! P | | j & gt; I)//I illegal
{
Printf (" query less than the element! \n");
return 0;
}
E=p - & gt; The data;
return 0;
}
Void CreateList_F (LinkList L, int n)//head method to create a singly linked list, n to the number of elements in the insert
{
int i;
LinkList p;
L=(LinkList) malloc (sizeof (LNode));
L - & gt; Next=NULL;
Printf (" please input you want to insert the number of elements: ");
The scanf (" % d ", & amp; N);
Printf (" please enter your inserted element values (separated by a space) : ");
For (I=n; I & gt; 0; - I)
{
P=(LNode *) malloc (sizeof (LNode));
Scanf_s (" % d ", & amp; P - & gt; The data);
P - & gt; Next=L - & gt; Next;
L - & gt; Next=p;
}
}
Int InsertList_L (int I LinkList L, Elemtype e)//in the position I insert elements in L e
{
LinkList p, s;
int j=0;
P=L;
While (p & amp; & J & lt; I)//looking for node I - 1
{
P=p - & gt; Next;
+ + j;
}
if (! P | | j & gt; I) exit (1);//I is greater than the long table + 1 or less than 1
S=(LinkList) malloc (sizeof (LNode));//generate new node s
S - & gt; Data=https://bbs.csdn.net/topics/e;//the node data fields set to e
S - & gt; Next=p - & gt; Next;//insert node s L
P - & gt; Next=s;
return 0;
}
Int DeleteList_L (LinkList L, int I)//delete the L of the ith element, and returns its value e
{
LinkList p, q;
int j=0;
P=L;
Printf (" please enter to delete node location: ");
While (p - & gt; Next & amp; & J & lt; I - 1)//find the ith node, and makes the p points to its precursor
{
P=p - & gt; Next;
+ + j;
}
if (! (p - & gt; Next) | | j & gt; I) exit (1);//delete position unreasonable
Q=p - & gt; Next;//temporary storage to be deleted node address for release
P - & gt; Next=q - & gt; Next;/change/delete nodes of precursor pointer field
free(q);//release
return 0;
}
Void ShowList (LinkList L)//print the whole list
{
LinkList p;
P=L - & gt; Next;
If (p==NULL)
{
Printf (" this is an empty list! \n");
}
Printf (" singly linked lists ");
While (p)
{
Printf (" - & gt; % d ", p - & gt; The data);
P=p - & gt; Next;
}
printf("\n");
}
The main ()
{
LinkList L;
int n;
Int I, k;
Printf (" singly linked lists operation: \ n ");
Printf (" \ t1 head interpolation based singly linked lists \ n ");
Printf (" \ t2. Output singly linked lists \ n ");
Printf (" \ n \ t3 delete nodes ");
Printf (" \ t4 exit \ n ");
Do
{
Printf (" choose the required functions: ");
The scanf (" % d ", & amp; K);
The switch (k)
{
Case 1: CreateList_F (L, n);
break;
Case 2: printf (" singly linked lists is: \ n ");
ShowList (L);
break;
Case 3:
DeleteList_L (L, I);
break;
Case 4: printf (" quit ");
exit(0);
Default: printf (" input error \ n ");
exit(0);
}
} the while (1);
}
CodePudding user response:
int InitList (LinkList L)
{
L=(LinkList) malloc (sizeof (LNode));//modify parameters of no use, leave the function parameter was end
if (! L)
{
Printf (" allocates memory failed! \n");
exit(0);
}
L - & gt; Next=NULL;
return 0;
}
CodePudding user response:
Complete C and C + + tutorial: https://blog.csdn.net/it_xiangqiang/category_10581430.htmlC and C + + algorithm complete tutorial: https://blog.csdn.net/it_xiangqiang/category_10768339.html
CodePudding user response:
# include
#include
Typedef int Elemtype;
Typedef struct LNode//define a structure
{
Elemtype data;//node data fields
struct LNode *next;//node pointer field
} LNode * LinkList;//pointer type
Int InitList (LinkList L)//singly linked lists with a head node initialization
{
L=(LinkList) malloc (sizeof (LNode));
if (! L)//storage allocation failure out
{
Printf (" allocates memory failed! \n");
exit(0);
}
L - & gt; Next=NULL;//the length and width of the table is null
return 0;
}
Int LengthList LinkList (L)//length calculation list
{
Int length=0;
LinkList p;
P=L - & gt; Next;
While (p)
{
Length++;
P=p - & gt; Next;
}
Return length;
}
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull