diff --git "a/\351\223\276\350\241\250.txt" "b/\351\223\276\350\241\250.txt" new file mode 100644 index 0000000000000000000000000000000000000000..b3c565389cd4e299d0abf59d00c53aefc8a06f64 --- /dev/null +++ "b/\351\223\276\350\241\250.txt" @@ -0,0 +1,335 @@ +#include +#include +typedef struct node +{ + int data; + node* pNext; +}Node; +//链表的操作,以有头节点为例,无头节点类似 +Node* head = NULL; +//创建链表,头结点data=0,pNext=NULL; +bool createNodeList() +{ + head = (Node*) malloc(sizeof(Node)); + if(NULL == head) + { + return false; + } + else + { + head->data = 0; + head->pNext = NULL; + return true; + } +bool addNode(Node* node) +{ + if(NULL == head) + { + return false; + } + Node* p = head->pNext; + Node* q = head; + while(NULL != p) + { + q = p; + p = p->pNext; + } + q->pNext = node; + node->pNext = NULL; + return true; +} +bool deleteNode(int index) +{ + if(NULL == head) + { + return false; + } + Node* p = head->pNext; + int length = 0; + while(NULL != p) + { + length ++; + p = p->pNext; + } + if(length < index) + { + return false; + } + else + { + Node* q = head; + p = head; + for(int i=0;ipNext; + } + Node* t = p->pNext; + q->pNext = t; + free(p); + return true; + } +} + +//逆序 +void reverseNodeList() +{ + if(NULL == head) + { + return; + } + //如果链表长度为1 + if(head->pNext == NULL) + { + return; + } + Node* p = head->pNext; + Node* q = p->pNext; + Node* t = NULL; + while(NULL != q) + { + t = q->pNext; + q->pNext = p; + p = q; + q = t; + } + head->pNext->pNext = NULL; + head->pNext = p; +} +//排序(降序) +void sort() +{ + //冒泡排序 + Node* pHead = head; + if(head == NULL) { + return; + } + if(pHead->pNext == NULL) + { + return; + } + Node* pi = pHead->pNext; + Node* pj = pi->pNext; + for(;pi != NULL;pi=pi->pNext) + { + for(pj = pi->pNext;pj != NULL;pj=pj->pNext) + { + if(pj->data>pi->data) + { + int tmp = pj->data; + pj->data = pi->data; + pi->data = tmp; + } + } + } +} +//销毁 +void destroyNodeList() +{ +. +. + if(NULL == head) + { + return; + } + if(NULL == head->pNext) + { + free(head); + head = NULL; + return; + } + Node* p = head->pNext; + while(NULL != p) + { + Node* tmp = p; + p = p->pNext; + free(tmp); + } + free(head); + head = NULL; +} +void main() +{ + createNodeList(); + Node* node1 = (Node*)malloc(sizeof(Node)); + node1->data = 1; + node1->pNext = NULL; + Node* node2 = (Node*)malloc(sizeof(Node)); + node2->data = 2; + node2->pNext = NULL; + addNode(node1); + addNode(node2); + reverseNodeList(); + Node* node3 = (Node*)malloc(sizeof(Node)); + node3->data = 3; + node3->pNext = NULL; + addNode(node3); + sort(); + deleteNode(2); + destroyNodeList(); +} +2.class Node { +Value: string; +Next:Node; +} +Class LinkedList{ +Private:head:Node; +Private:tail:Node; +}ate tail: Node; +addToRear(v:string){ +if(head +== +null) +this.addToFront(v); +else { +let p += +head; +while(p +.next ! += +null) +p += +p +.next; +let n += +new Node(); +n.value += +v; +tail.next += +n; +n.next += +null; +tail += +n +} +} +3. +#include +#include +#include +typedef struct Node pNode; +struct Node +{ + int data; + pNode *prev, *next; +}; +/* 初始化链表,尾插法 */ +pNode *InitList(pNode **head, int n) +{ + pNode *p, *s; + (*head) = (pNode *)malloc(sizeof(pNode)); + if ((*head) == NULL) + exit(0); + (*head)->next = NULL;//head的prev和next均指向NULL + (*head)->prev = NULL; + p = (*head);//p指向head + int i; + for (i = 0; i < n; ++i) + { + s = (pNode *)malloc(sizeof(pNode)); + if (s == NULL) + exit(0); + printf("Input the value of the %dth node:", i + 1); + scanf("%d", &s->data); + s->next = NULL; + p->next = s; + s->prev = p; + p = s;//p指向尾节点 + } + return p; +} +/* 遍历打印 */ +void PrintList(pNode *head) +{ + pNode *p; + p = head->next; + if (head->next == NULL) + printf("the list is empty\n"); + while(p != NULL) + { + printf("%d ", p->data); + p = p->next; + } + printf("\n"); +} +/* 清空链表 */ +void DeleteList(pNode **head) +{ + pNode *p; + while((*head)->next != NULL) + { + p = (*head); + p->next->prev = NULL; + (*head) = p->next; + free(p); + } +} +/* 查找链表内的某个值 */ +int SearchList(pNode *head) +{ + int number; + printf("Values are about to be deleted:"); + scanf("%d", &number); + pNode *p; + p = head->next; + while(p != NULL) + { + if (p->data == number) + { + return number; + } + p = p->next; + } + return 0; +} +/* 删除链表中某个元素,令p的前驱节点和后驱节点相互指向即可,如果p是尾节点则直接将前驱节点指向NULL*/ +void DelNumqList(pNode **head, int n) +{ + int i; + pNode *p; + p = (*head)->next; + for (i = 1; i < n; ++i) + p = p->next; + if(p->next == NULL) + { + p->prev->next = NULL; + free(p); + } + else + { + p->next->prev = p->prev; + p->prev->next = p->next; + free(p); + } +} +int main(int argc, char const *argv[]) +{ + int n, element, flag; + pNode *head, *last; + /***************************************************************/ + printf("Please input the size of the list:"); + scanf("%d", &n); + last = InitList(&head, n);//初始化链表并赋值,返回尾节点last + printf("%d %d \n", head->next->data, last->data); //打印为第一个元素和最后一个元素 + PrintList(head); + /***************************************************************/ + flag = SearchList(head); //搜索某个值并删除节点 + if (flag > 0 && flag <= n) + { + DelNumqList(&head, flag); + PrintList(head); + } + else + printf("Element does not exist, cannot be deleted\n"); + /***************************************************************/ + DeleteList(&head);//清空列表 + PrintList(head); + return 0; +} \ No newline at end of file