sample input :
3 // Enter a number a, It will be entered later a Data elements 8 9 3 //a Data elements , After inserting the tail node in turn . Form single linked list node sequence :8,9,3 3
// Enter a number b, It will be entered later b Data elements 10 89 22 //b Data elements , Insert in turn 0 After node No . Form single linked list node sequence :8,22,89,10,9,3 89
// Delete a value of 89 Node of 1 // delete 1 Node No
sample output :
8 10 9 3 #include <stdio.h> #include <stdlib.h> typedef int T; struct LinkNode
{ T data; LinkNode* next; }; struct LinkList { LinkNode* front; // Point to head node .
LinkNode* rear; // Point to the tail node . LinkNode* pre; // Points to the previous node of the current location node . LinkNode* curr; //
Points to the current location node . int position; // The number of the current child node . int len; // Size of linear table ( Number of linked list nodes ). }; LinkList*
LL_Create() // Create a linear table of linked storage , Initially empty table , return llist Pointer . { LinkList* llist=(LinkList*)malloc
(sizeof(LinkList)); llist->front=NULL; llist->rear=NULL; llist->pre=NULL; llist
->curr=NULL; llist->position=0; llist->len=0; return llist; } // 2) void LL_Free
(LinkList* llist) // Release the node of the linked list , Then release llist The structure to which it refers . { LinkNode* node=llist->front;
LinkNode* nextnode; while(node){ nextnode=node->next; free(node); node=nextnode;
} free(llist); } // 3) void LL_MakeEmpty(LinkList* llist) //
Changes the current linear table to an empty table , Therefore, all nodes need to be released . { LinkNode* node=llist->front; LinkNode* nextnode;
while(node){ nextnode=node->next; free(node); node=nextnode; } llist->front=NULL
; llist->rear=NULL; llist->pre=NULL; llist->curr=NULL; llist->position=0; llist
->len=0; } // 4) int LL_Length(LinkList* llist) // Returns the current length of a linear table . { return llist->
len; } // 5) bool LL_IsEmpty(LinkList* llist) // If the current linear table is empty , Then return true, Otherwise, return TRUE. {
return llist->len==0; } // 6) bool LL_SetPosition(LinkList* llist, int i) //
Set the current position of the linear table to i Position No . // Set successfully , Then return true, Otherwise, return false( The linear table is empty , or i Not in valid return ). //
Assume that the current length of the linear table is len, that i The effective range is [0,len]. { int k; /* If the linked list is empty , Then return */ if (llist->len==0)
return false; /* If the position is out of bounds */ if( i < 0 || i > llist->len) { printf(
"LL_SetPosition(): position error"); return false; } /* Find the corresponding node */ llist->curr =
llist->front; llist->pre = NULL; llist->position = 0; for ( k = 0; k < i; k++) {
llist->position++; llist->pre = llist->curr; llist->curr = (llist->curr)->next;
} /* Returns the current node location */ return true; } // 7) int LL_GetPosition(LinkList* llist) //
Gets the number of the node in the current position of the linear table . { return llist->position; } // 8) bool LL_NextPosition(
LinkList* llist) // Sets the next position of the current position of the linear table as the current position . //
Set successfully , Then return true, Otherwise, return false( The linear table is empty , Or the current position is the end of the table ). { if (llist->position >= 0 && llist->
position< llist->len) /* If the current node exists , Then its successor node is set as the current node */ { llist->position++; llist->
pre= llist->curr; llist->curr = llist->curr->next; return true; } else return
false; } // 9) T LL_GetAt(LinkList* llist) // Returns the value of the data element in the current position of a linear table . { if(llist->
curr==NULL) { printf("LL_GetAt(): Empty list, or End of the List.\n"); LL_Free(
llist); exit(1); } return llist->curr->data; } // 10) void LL_SetAt(LinkList*
llist, T x) // Change the value of the data element of the current position of the linear table to x. { if(llist->curr==NULL) { printf(
"LL_SetAt(): Empty list, or End of the List.\n"); LL_Free(llist); exit(1); }
llist->curr->data=x; } // 11) bool LL_InsAt(LinkList* llist, T x) //
Inserts a data element before the current position of the linear table x. The current position pointer points to the new data element node . // If the insertion fails , return false, Otherwise, return true. { LinkNode *
newNode=(LinkNode*)malloc(sizeof(LinkNode)); if (newNode==NULL) return false;
newNode->data=x; if (llist->len==0){ /* Insert in empty table */ newNode->next=NULL; llist->
front= llist->rear = newNode; } // The current position is the header . else if (llist->pre==NULL) { /*
Insert at header node */ newNode->next = llist->front; llist->front = newNode; } else { /*
Insert in the middle of the list or at the end of the list */ newNode->next = llist->curr; llist->pre->next=newNode; }
// Insert after end of table . if (llist->pre==llist->rear) llist->rear=newNode; /* Increase the size of the linked list */ llist->
len++; /* The newly inserted node is the current node */ llist->curr = newNode; return true; } // 12) bool
LL_InsAfter(LinkList* llist, T x) // Inserts a data element after the current position of the linear table x. Empty tables are allowed to be inserted . The current position pointer points to the new node . //
If the insertion fails , return false, Otherwise, return true. { LinkNode *newNode=(LinkNode*)malloc(sizeof(LinkNode));
if (newNode==NULL) return false; newNode->data=x; if (llist->len==0) { /*
Insert in empty table */ newNode->next=NULL; llist->front = llist->rear = newNode; } else if (
llist->curr == llist->rear || llist->curr == NULL) { /* Insert after tail node */ newNode->next
= NULL; llist->rear->next=newNode; llist->pre=llist->rear; llist->rear=newNode;
llist->position=llist->len; } else{ /* Insert in the middle */ newNode->next = llist->curr->
next; llist->curr->next=newNode; llist->pre=llist->curr; llist->position ++; }
/* Increase the size of the linked list */ llist->len ++; /* The newly inserted node is the current node */ llist->curr = newNode; return true;
} // 13) bool LL_DelAt(LinkList* llist) // Delete the data element node in the current position of the linear table . //
If the deletion fails ( Empty table , Or the current position is after the tail node ), Then return false, Otherwise, return true. { LinkNode *oldNode; /*
If the table is empty or after the end of the table , Then give an error prompt and return */ if (llist->curr==NULL) { printf("LL_DelAt(): delete a
node that does not exist.\n"); return false; } oldNode=llist->curr; /*
Delete the header node */ if (llist->pre==NULL) { llist->front = oldNode->next; } /*
Delete the node in or at the end of the table */ else if(llist->curr!=NULL){ llist->pre->next = oldNode->next; } if
(oldNode == llist->rear) { /* Delete the tail node of the table , Then modify the tail pointer and the current node position value */ llist->rear = llist->
pre; } /* The successor node is used as the new current node */ llist->curr = oldNode->next; /* Release the original current node */ free(oldNode)
; /* Reduced list size */ llist->len --; return true; } // 14) bool LL_DelAfter(LinkList*
llist) // Delete the data element after the current position of the linear table . // If the deletion fails ( Empty table , Or the current position ), Then return false, Otherwise, return true. {
LinkNode*oldNode; /* If the table is empty or to the end of the table , Then give an error prompt and return */ if (llist->curr==NULL || llist->curr
== llist->rear) { printf("LL_DelAfter(): delete a node that does not exist.\n");
return false; } /* Save the pointer of the deleted node and delete the node from the list */ oldNode = llist->curr->next; llist->
curr->next=oldNode->next; if (oldNode == llist->rear) /* Delete the tail node of the table */ llist->rear
= llist->curr; /* Release deleted node */ free(oldNode); /* Reduced list size */ llist->len --; return true
; } // 15) int LL_FindValue(LinkList* llist, T x) // Find the first value in the linear table as x The number of the data element of the . //
Return value -1 It means not found , Return value >=0 Indicates the number . { LinkNode* p=llist->front; int idx=0; while(p!=NULL &&
p->data!=x) { idx++; p = p->next; } if (idx>=llist->len) return -1; else return
idx; } // 16) int LL_DelValue(LinkList* llist, T x) //
Delete the first value as x Data elements of , Returns the number of the data element . If not, the value is x Data elements of , Then return -1. { int idx=LL_FindValue(llist, x);
if (idx<0) return -1; LL_SetPosition(llist, idx); LL_DelAt(llist); return idx; }
// 17) void LL_Print(LinkList* llist) // Print the entire linear table . { LinkNode* node=llist->front;
while (node) { printf("%d ", node->data); node=node->next; } printf("\n"); } int
main() { LinkList* llist=LL_Create(); int i; int x; int a; scanf("%d", &a); for(
i=0; i<a; i++) { scanf("%d",&x); LL_InsAfter(llist,x); } LL_SetPosition(llist, 0
); scanf("%d", &a); for(i=0; i<a; i++) { scanf("%d", &x); LL_SetPosition(llist,0
); LL_InsAfter(llist,x); } // LL_Print(llist); scanf("%d", &x); LL_DelValue(
llist, x); scanf("%d", &i); LL_SetPosition(llist, i); LL_DelAt(llist); LL_Print(
llist); LL_Free(llist); }
Technology
Daily Recommendation