Instances of C language data structure linked lists (nineteen operations)
- 2020-05-26 09:54:23
- OfStack
Instances of C language data structure linked lists (109 operations)
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
/*************************************************************************************/
/* The first 1 Version of the blogger The original address http://www.cnblogs.com/renyuan/archive/2013/05/21/3091506.html */
/* The first 2 Version of the blogger The original address http://www.cnblogs.com/wireless-dragon/p/5170565.html */
/* 2. Create a linear table, this function input is not timed to terminate the read data */
/* 3. Print linked list, linked list traversal */
/* 4. Queries the number of linked list nodes and returns the length */
/* 5. Check if the single linked list is empty */
/* 6. Bubble sort the linear table */
/* 7. Find the number one in a single linked list n The element of node 1 */
/* 8. Looks for a given value from a single linked list number The first 1 Element, returns the address of the node */
/* 9. I'm going to put the number one in the list n The value of node is changed to number The value of the */
/* 10. Inserts into the header of a single linked list 1 An element */
/* 11. Add to the end of a single linked list 1 An element */
/* 12. To a single linked list n The insertion element is x The node */
/* 13. Inserts an element into an ordered singly linked list x Nodes, so that they are still in order after insertion */
/* 14. Removes the header node from a single linked list */
/* 15. Deletes the table tail from a single linked list */
/* 16. Remove the first from a single linked list n A node */
/* 17. Deletes the value of x The first 1 A node */
/* 18. exchange 2 The position of the elements */
/* 19. Delete the list */
/*************************************************************************************/
typedef int elemType;
typedef struct NODE
{
elemType element;
struct NODE *next;
} Node;
/* 2. Create a linear table, this function input is not timed to terminate the read data */
void creatList(Node **pHead)
{
printf("Please enter the list:\n");
Node *p1, *p2;
p1 = p2 = (Node *)malloc(sizeof(Node));
if (p1 == NULL || p2 == NULL)
exit(0);
memset(p1, 0, sizeof(Node));
scanf("%d", &p1->element);
p1->next = NULL;
while(p1->element > 0)
{
if (*pHead == NULL)
(*pHead) = p1;
else
p2->next = p1;
p2 = p1;
p1 = (Node *)malloc(sizeof(Node));
if (p1 == NULL)
exit(0);
memset(p1, 0, sizeof(Node));
scanf("%d", &p1->element);
p1->next = NULL;
}
}
/* 3. Print linked list, linked list traversal */
void printList(Node *pHead)
{
if (NULL == pHead)
printf("The list is empty\n");
else
while(NULL != pHead)
{
printf("%d ", pHead->element);
pHead = pHead->next;
}
printf("\n");
}
/* 4. Queries the number of linked list nodes and returns the length */
int sizeList(Node *pHead)
{
int size = 0;
while(pHead != NULL)
{
size ++;
pHead = pHead->next;
}
return size;
}
/* 5. Check if the single linked list is empty */
void isEmptyList(Node *pHead)
{
if (pHead == NULL)
{
printf("The list is empty\n");
exit(0);
}
}
/* 7. Find the number one in a single linked list n The element of node 1 */
void getElement(Node *pHead, int num)
{
for (int i = 1; i < num; ++i)
pHead = pHead->next;
printf("The value of the %dth element is:%d\n", num, pHead->element);
}
/* 8. Looks for a given value from a single linked list number The first 1 Element, returns the address of the node */
int getElemAddr(Node *pHead, int number)
{
int i = 1;
while(pHead != NULL)
{
if (pHead->element == number)
return i;
i++;
pHead = pHead->next;
}
return 0;
}
/* 9. I'm going to put the number one in the list n The value of node is changed to number The value of the */
void modifyElem(Node **pList, int addr, int number)
{
Node *pHead; // If changed directly here pList It's called in the main function printList from addr Start printing at
int i = 1;
pHead = *pList;
while(pHead != NULL)
{
if (i == addr)
break;
pHead = pHead->next;
i++;
}
pHead->element = number;
}
/* 10. Inserts into the header of a single linked list 1 An element */
void insertHeadList(Node **pHead)
{
Node *p1;
p1 = (Node *)malloc(sizeof(Node));
if (p1 == NULL)
exit(0);
memset(p1, 0, sizeof(Node));
printf("Please enter a number to be inserted:");
scanf("%d", &p1->element);
p1->next = (*pHead);// At this time pHead It points to the first 1 A node ( There are data domains ) , so the new node has to be inserted before the leading node
(*pHead) = p1; // pHead Pointing to the first 1 A node
}
/* 11. Add to the end of a single linked list 1 An element */
void insertLastList(Node **pHead, int n)
{
Node *p1, *p2;
p2 = (*pHead);
int i;
for (i = 1; i < n; ++i)
p2 = p2->next;
p1 = (Node *)malloc(sizeof(Node));
if (p1 == NULL)
exit(0);
memset(p1, 0, sizeof(Node));
printf("Please enter a number to be inserted:");
scanf("%d", &p1->element);
p1->next = NULL;
p2->next = p1;
}
/* 12. To a single linked list n The insertion element is x The node */
void isAddPos(Node **pHead, int length)
{
Node *p1, *p2;
int position, i;
printf("Please enter the insert position:");
scanf("%d", &position);
if (position > length || position <= 0)
{
printf("Input error, the program ends\n");
exit(0);
}
p1 = (Node *)malloc(sizeof(Node));
p2 = (*pHead);
if (p1 == NULL)
exit(0);
memset(p1, 0, sizeof(Node));
printf("Please enter a number to be inserted:");
scanf("%d", &p1->element);
for (i = 1; i < position - 1; ++i)
p2 = p2->next;
p1->next = p2->next;
p2->next = p1;
}
/* 6. Bubble sort the linear table */
void Arrange(Node **pHead, int length)
{
Node *p1;
p1 = (*pHead);
int i, j, temp;
for (i = length; i > 0; --i)
{
for(j = i - 1; j > 0; --j)
{
if ((p1->element) > (p1->next->element))
{
temp = p1->element;
p1->element = p1->next->element;
p1->next->element = temp;
}
p1 = p1->next;
}
p1 = (*pHead);
}
}
int OrrderList(Node **pHead, int length)
{
Node *p1, *p2;
p1 = (*pHead);
p2 = (Node *)malloc(sizeof(Node));
if (p2 == NULL)
exit(0);
memset(p2, 0, sizeof(Node));
printf("Enter the value of the element to be inserted:");
scanf("%d", &p2->element);
if (p2->element < p1->element)
{
p2->next = p1;
(*pHead) = p2;
return 1;
}
while(p1->next != NULL && p2->element > (p1->next->element))
p1 = p1->next;
if (p1->next == NULL)
{
p2->next = NULL;
p1->next = p2;
return 1;
}
else
{
p2->next = p1->next;
p1->next = p2;
return 1;
}
}
/* 14. Removes the header node from a single linked list */
void DelHeadList(Node **pHead)
{
Node *p1;
p1 = (*pHead);
(*pHead) = (*pHead)->next;
free(p1);
}
/* 15. Deletes the table tail from a single linked list */
void DelLastList(Node **pHead)
{
Node *p1, *p2;
p1 = (*pHead);
p2 = p1->next;
while(p2->next != NULL)
{
p2 = p2->next;
p1 = p1->next;
}
p1->next = NULL;
free(p2);
}
/* 16. Remove the first from a single linked list n A node */
void DelPos(Node **pHead, int length)
{
int n, i;
Node *p1, *p2;
p1 = (*pHead);
p2 = p1->next;
printf("Please enter the serial number number to delete:");
scanf("%d", &n);
if (n < 1 || n > length)
exit(0);
for (i = 1; i < n - 1; ++i)
{
p2 = p2->next;
p1 = p1->next;
}
p1->next = p2->next;
free(p2);
}
/* 17. Deletes the value of x The first 1 A node */
int Delx(Node **pHead)
{
Node *p1, *p2;
p1 = (*pHead);
p2 = p1->next;
int number;
printf("Please input is going to be deleted the value of x:");
scanf("%d", &number);
if (number == (*pHead)->element)
{
(*pHead) = (*pHead)->next;
free(p1);
return 1;
}
while(p2 != NULL)
{
if (p2->element == number)
{
break;
}
p2 = p2->next;
p1 = p1->next;
}
if (p2 == NULL)
{
printf("X does not exist in the list\n");
return 1;
}
else
{
p1->next = p2->next;
free(p2);
return 1;
}
}
/* 18. exchange 2 The position of the elements */
void exchange2pos(Node **pHead, int length)
{
Node *p1, *p2;
int n1, n2, i, j, temp;
printf("Please enter the first number:");
scanf("%d", &n1);
printf("Please enter the second number:");
scanf("%d", &n2);
if (n1 < 1 || n1 > length || n2 < 1 || n2 > length)
exit(0);
p1 = p2 = (*pHead);
for (i = 1; i < n1; ++i)
{
p1 = p1->next;
}
for (j = 1; j < n2; ++j)
{
p2 = p2->next;
}
temp = p1->element;
p1->element = p2->element;
p2->element = temp;
}
/* Delete the list */
void clearList(Node **pHead)
{
Node *p1;
p1 = (*pHead);
while(p1 != NULL)
{
p1 = p1->next;
free((*pHead));
(*pHead) = p1;
}
}
int main(int argc, char const *argv[])
{
/* 1. Initializes a linear table by making the header pointer of a single linked list null */
Node *pList = NULL;
int length = 0, n, addr, number;
/* 2. Create a linear table, this function input is not timed to terminate the read data */
printf("- - - - - - - - - 2 - - - - - - - -\n");
creatList(&pList);
/* 5. Check if the single linked list is empty */
isEmptyList(pList);
printList(pList);
/* 4. Queries the number of linked list nodes and returns the length */
printf("- - - - - - - - - 4 - - - - - - - -\n");
length = sizeList(pList);
printf("the Node length is:%d\n", length);
/* 7. Find the number one in a single linked list n The element of node 1 */
printf("- - - - - - - - - 7 - - - - - - - -\n");
printf("Please input node number (n):");
scanf("%d", &n);
if (n > length || n < 1)
{
printf("N is not within the scope of\n");
exit(0);
}
getElement(pList, n);
/* 8. Looks for a given value from a single linked list number The first 1 Element, returns the address of the node */
printf("- - - - - - - - - 8 - - - - - - - -\n");
addr = 0;
number;
printf("Please enter to find element value (number):");
scanf("%d", &number);
addr = getElemAddr(pList, number);
if (addr == 0)
printf("List the element\n");
else
printf("The location of the number is:%d\n", addr);
/* 9. I'm going to put the number one in the list n The value of node is changed to number The value of the */
printf("- - - - - - - - - 9 - - - - - - - -\n");
addr = 0;
number = 0;
printf("Please input to replace the serial number (n):");
scanf("%d", &addr);
if (addr > length || addr < 0)
{
printf("N is not within the scope of\n");
exit(0);
}
printf("Please input to replace the contents of the (number):");
scanf("%d", &number);
modifyElem(&pList, addr, number);
printf("The revised list is:\n");
printList(pList);
/* 10. Inserts into the header of a single linked list 1 An element */
printf("- - - - - - - - - 10 - - - - - - - -\n");
insertHeadList(&pList);
printList(pList);
/* 11. Add to the end of a single linked list 1 An element */
printf("- - - - - - - - - 11 - - - - - - - -\n");
insertLastList(&pList, length);
printList(pList);
/* 12. To a single linked list n The insertion element value is x The node */
printf("- - - - - - - - - 12 - - - - - - - -\n");
isAddPos(&pList, length);
printList(pList);
/* 6. Bubble sort the linear table */
printf("- - - - - - - - - 6 - - - - - - - -\n");
Arrange(&pList, length);
printList(pList);
/* 13. Inserts an element into an ordered singly linked list x Nodes, so that they are still in order after insertion */
printf("- - - - - - - - - 13 - - - - - - - -\n");
OrrderList(&pList, length);
printList(pList);
/* 14. Removes the header node from a single linked list */
printf("- - - - - - - - - 14 - - - - - - - -\n");
DelHeadList(&pList);
printList(pList);
/* 15. Deletes the table tail from a single linked list */
printf("- - - - - - - - - 15 - - - - - - - -\n");
DelLastList(&pList);
printList(pList);
/* 16. Remove the first from a single linked list n A node */
printf("- - - - - - - - - 16 - - - - - - - -\n");
DelPos(&pList, length);
printList(pList);
/* 17. Deletes the value of x The first 1 A node */
printf("- - - - - - - - - 17 - - - - - - - -\n");
Delx(&pList);
printList(pList);
/* 18. exchange 2 The position of the elements */
printf("- - - - - - - - - 18 - - - - - - - -\n");
exchange2pos(&pList, length);
printList(pList);
/* 19. Delete the list */
printf("- - - - - - - - - 19 - - - - - - - -\n");
clearList(&pList);
printList(pList);
return 0;
}
The above is the C language data structure single linked list operation, all basic operations are included, very comprehensive, this site for the data structure of the article is still a lot of, hope you search and consult, thank you for reading, hope to help you, thank you for the support of this site!