conio.h不是C标准库中的头文件,是console input/output(控制台输入输出)的简写,其中定义了通过控制台进行数据录入和输出的函数,主要是一些用户通过键盘产生的
对应操作,例如getch()函数等。
<pre name="code" class="cpp">// 13_1_linklist.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <stdio.h> #include <conio.h> #include <string.h> #include <iostream> using namespace std; typedef struct student { int data; struct student *next; }node; //建立 node *create() { int x,cycle = 1; node *p,*s,*head; head = (node *)malloc(sizeof(node)); p = head; while (cycle) { cout << "Please input an integer:" << endl; cin >> x; if (x != 0) { s = (node *)malloc(sizeof(node)); s->data = x; cout << "\n" << s->data << endl; p->next = s; p = s; } else cycle = 0; } head = head->next; //head的next是我们需要的链表的头结点, 因为head(p)的next指向了第一次输入的s p->next = NULL; cout << head->data << endl; return head; } //测长 int length(node *head) { int n = 0; node *p; p = head; while (p != NULL) { p = p->next; n++; } return n; } //打印 void print(node *head) { node *p; if (head != NULL) { p = head; while (p != NULL) { cout << p->data << endl; p = p->next; } } } //删除 node *del(node *head,int num) { node *p1,*p2; p2 = p1 = head; //防止p2未被初始化就使用 while (num != p1->data && p1->next != NULL) { p2 = p1; p1 = p1->next; } if (num == p1->data) { if (p1 == head) //删除的是头节点,将head指针指向头结点的下一节点 并free p1 { head = p1->next; free(p1); } else //删除的是非头节点,将p2的next指向p1的next,并free p1 { //p2 = p1; p2->next = p1->next; free(p1); } } return(head); } //插入,分为头节点, 尾节点, 中间节点三种情况插入 node *insert(node *head,int num) { node *p0,*p1,*p2; p0 = (node*)malloc(sizeof(node)); p0->data = num; p2 = p1 = head; while (p1->data < num && p1->next != NULL) { p2 = p1; p1 = p1->next; } if (p1->data >= num) { if (p1 == head)//插入在头节点前,使p0的next指向头结点,再将p0赋给head { p0->next = p1; head = p0; } else//中间节点 { p2->next = p0; p0->next = p1; } } else//num大于等于链表里所有数值,插入到尾节点之后 { p1->next = p0; p0->next = NULL; } return head; } //排序 node *sort(node *head) { node *p; int temp; int len = length(head); p = head; if (NULL == head || NULL == head->next) { return head; } for (int i = 0; i < len - 1; i++) { p = head;// 每一次嵌套for循环结束后,p都指向了尾节点,需要重新将p指向头节点才能进行下一次循环 for (int j = i + 1; j < len; j++) { if (p->data > p->next->data) { temp = p->data; p->data = p->next->data; p->next->data = temp; } p = p->next; } } return head; } //逆置 node *reverse(node *head) { node *p1,*p2,*p3; p1 = head; p2 = p1->next; if (NULL == head || NULL == head->next) { return head; } while (p2) { p3 = p2->next; p2->next = p1; p1 = p2; p2 = p3;//将p2赋给p1,p3赋给p2,进入下一次循环 } head->next = NULL; head = p1; return head; } int _tmain() { int val = 10; node *link_list = create(); int len = length(link_list); cout << "The length of link_list is: " << len << "\n" << endl; printf("The original link list is:\n"); print(link_list); node *del_link_list = del(link_list,val); printf("The deleted link list is:\n"); print(del_link_list); node *sorted_link_list = sort(del_link_list); printf("THe sorted link list is: \n"); print(sorted_link_list); node *inserted_link_list = insert(sorted_link_list,val); printf("The inserted link list is:\n"); print(inserted_link_list); node *reversed_link_list = reverse(inserted_link_list); printf("The reversed link list is: \n"); print(reversed_link_list); return 0; }The length of link_list is: 5The original link list is:257111The deleted link list is:257111THe sorted link list is:125711The inserted link list is:12571011The reversed link list is:11107521 原文链接:https://www.f2er.com/datastructure/383041.html