C ++ implements common functions of linked lists.
First, the header file defines common functions of the linked list:
typedef struct node {int data;struct node* next;} Node;class LinkListUtil{public:LinkListUtil();~LinkListUtil();//createNode* createByArray(int arr[], int len);int getLenght(Node *head);void println(Node *head);Node* remove(Node *head, int num);//Insert element into the link list by the index.//if index is -1 then insert the num into the link list end.Node* insert(Node *head, int num, int index = -1);Node* sort(Node *head);Node* reserve(Node *head);Node* removeHead(Node *head);};
Then implement:
#include "stdafx.h"#include "LinkListUtil.h"LinkListUtil::LinkListUtil(){}LinkListUtil::~LinkListUtil(){}Node* LinkListUtil::createByArray(int arr[], int len){//Declare head node, previous node, new node.Node *head, *p, *n;//Init head node.head = new Node();p = head;for (int i = 0; i < len; i++){n = new Node();n->data = arr[i];p->next = n;p = n;}head = head->next;//head->next = nullptr;//?return head;}int LinkListUtil::getLenght(Node *node){int len = 0;Node *temp = node;while (temp != nullptr){temp = temp->next;len++;}return len;}void LinkListUtil::println(Node *node){Node *temp = node;while (temp != nullptr){printf("the node data is : %d\n", temp->data);temp = temp->next;}}Node* LinkListUtil::remove(Node *head, int num){Node *temp = head;Node *pre = nullptr;//find the nodewhile (temp != nullptr){if (temp->data != num){pre = temp;temp = temp->next;}else {break;}}if (pre == nullptr){head = head->next;}else {pre->next = temp->next;}delete temp;return head;}Node* LinkListUtil::insert(Node *head, int num, int index){int len = getLenght(head);if (index >= len) {printf("The index is greater than the length of link list!\n"); return head;}Node *temp = head;Node *pre = nullptr;//if index is -1 then insert the num into the link list end.int tempIndex = 0;if (index <= -1)index = len;//find the insert point.while (temp != nullptr && index != tempIndex){tempIndex++;pre = temp;temp = temp->next;}//insert the num.Node *nd;nd = new Node();nd->data = num;if (pre == nullptr){nd->next = head;delete pre, temp;return nd;}else {nd->next = temp;pre->next = nd;return head;}}Node* LinkListUtil::sort(Node *head){//if only has one element or none elementsif (head == nullptr || head->next == nullptr)return head;Node *p0, *p1;int temp = 0;p0 = head;//bubble sortwhile (p0 != nullptr && p0->next != nullptr){p1 = p0->next;while (p1 != nullptr){if (p0->data > p1->data){temp = p0->data;p0->data = p1->data;p1->data = temp;}p1 = p1->next;}p0 = p0->next;}return head;}Node* LinkListUtil::reserve(Node *head){//if only has one element or none elementsif (head == nullptr || head->next == nullptr)return head;Node *p0, *p1, *p2;p0 = head;p1 = p0->next;while (p1 != nullptr){p2 = p1->next;p1->next = p0;p0 = p1;p1 = p2;}head->next = nullptr;return p0;}Node* LinkListUtil::removeHead(Node *head){//if only has one element or none elementsif (head == nullptr || head->next == nullptr)return head;Node *p0;p0 = head->next;head->next = p0->next;head = p0;return head;}
The last is the main test:
// Win32Project2.cpp //#include "stdafx.h"#include <iostream> #include <string> #include <assert.h>#include "LinkListUtil.h"using namespace std;int _tmain(int argc, _TCHAR* argv[]){//////////////////////////////////////////////////////////////////////////int arr[5] {3, 1, 9, 4, 6};auto linkList = new LinkListUtil();auto head = linkList->createByArray(arr, 5);//Get linkList size;auto len = linkList->getLenght(head);printf("LinkList's length:%d\n", len);printf("Print LinkList:\n");linkList->println(head);printf("Remove element 1 and print LinkList:\n");head = linkList->remove(head, 1);linkList->println(head);printf("Insert element 5 into the index 1 and print LinkList:\n");head = linkList->insert(head, 5, 1);linkList->println(head);printf("Insert element 2 into the end and print LinkList:\n");head = linkList->insert(head, 2);linkList->println(head);printf("Bubble sort:\n");head = linkList->sort(head);linkList->println(head);printf("reserve:\n");head = linkList->reserve(head);linkList->println(head);printf("remove head:\n");head = linkList->removeHead(head);linkList->println(head);//cleardelete linkList;//////////////////////////////////////////////////////////////////////////system("pause");return 0;}
Write a Linear Linked List in C language, and perform insertion, deletion, sorting, search, and counting. Answer
# Include <stdio. h> # include <stdlib. h> struct linknode // create a linked list node {int data; // you need a more general data type struct linknode * next ;}; struct link {struct linknode * head; struct linknode * tail;}; struct linknode * create () // create a linked list, accepting INT-type values {int datas; struct linknode * head, * temp, * tail; head = tail = NULL; while (scanf ("% d", & datas) = 1) // The input method needs to be improved {temp = (struct linknode *) malloc (sizeof (struct linknode); if (temp = NULL) printf ("Allocate erro! "); Else {temp-> data = datas; temp-> next = NULL; if (head = NULL) head = tail = temp; else {tail-> next = temp; tail = temp; }}return head;} void print (struct linknode * head) // print the linked list {struct linknode * p; p = head; while (p) {printf ("% d \ t", p-> data); p = p-> next ;}} struct linknode * find (struct linknode * head, int datas) // find nodes with specific values {struct linknode * p; p = head; while (p-> data! = Datas & p-> next! = NULL) {p = p-> next;} if (p-> data = datas) return p; else return NULL;} struct linknode * findAhead (struct linknode * head, int datas) // find the previous node {struct linknode * p, * q; q = NULL; p = head; while (p-> data! = Datas & p-> next! = NULL) {q = p; p = p-> next;} if (p-> data = datas) return q; else return NULL ;} struct linknode * enterTohead (struct linknode * head, int datas) // Add a node to the header {// Changes the pointer of the header node, and you need to assign a new value to struct l ...... remaining full text>
C language programming: You can create a linked list and add or delete it at any location in the linked list. It must be completed in the same program.
A linked list made for others in the past, giving you richer functions. Let's take a look:
# Include "stdio. h"
Struct Node
{
Node * pNext;
Int value;
} * PTop;
Struct Node * Insert (struct Node * pNode, int Num );
Void Del (struct Node * pDelNode );
Struct Node * Search (struct Node * pNode, int Num );
Void main ()
{
PTop = NULL;
Int I, k, x, y;
Struct Node * pCurrentNode, * pTempNode;
/* (1) create a single-chain table with a table header node ;*/
For (I = 0; I <30; I ++) Insert (NULL, I);/* Create a linked list with 30 nodes */
/* (2) output the data domain values of all nodes in the single-link table ;*/
PCurrentNode = pTop;
While (pCurrentNode! = NULL)
{
Printf ("% d->", pCurrentNode-> value);/* traverse the linked list and output the data domains of each node */
PCurrentNode = pCurrentNode-> pNext;
}
/* (3) input x and y insert node y after the first data field value is x. If there is no knots x, insert node y at the end of the table ;*/
Printf ("Input x, y ");
Scanf ("% d, % d", & x, & y );
PCurrentNode = Search (NULL, x );
Insert (pCurrentNode, y );
/* (4) Enter k to delete all node k in the single-link table and output the number of deleted nodes. */
Printf ("Input k ");
Scanf ("% d", & k );
PCurrentNode = pTop;
I = 0;
While (1)
{
PTempNode = Search (pCurrentNode, x );
If (pTempNode! = NULL)
{
PCurrentNode = pTempNode-> pNext;
Del (pTempNode );
I ++;
}
Else break;
}
Printf ("% d Nodes was deleted", I );
PTempNode = pTop;
While (pTop! = NULL)
{
PTop = pTempNode-> pNext;
Delete pTempNode;
}
}
Node * Insert (struct Node * pNode, int Num)
{
Struct Node * pNewNode;
PNewNode = new Node;
PNewNode-> value = Num;
If (pNode = NULL)/* put the node at the end of the linked list when there is no definite insertion position */
{
If (pTop! = NULL)/* determine whether the linked list is a blank table */
{
PNode = pTop;
While (pNode-> pNext! = NULL) pNode = pNode-> pNext... the remaining full text>