目前还不完善,慢慢加入新功能
#include<iostream>
#include<stdlib.h>
using namespace std;
struct node
{
int data;
struct node *next;
};
int main() {
int a,n;
struct node *head,*p,*q,*t;
head = NULL;
q = NULL;
cout << "请输入元素个数" << endl;
cin >> n;
for (int i = 0; i < n; i++) {
cout << "请输入元素" << endl;
cin >> a;
p = (struct node *)malloc(sizeof(struct node));
p->data = a;
p->next = NULL;
if (head == NULL)
head = p;
else
q->next = p;
q = p;
}
t = head;
cout << "链表是" << endl;
while (t!=NULL)
{
cout << t->data;
t = t->next;
}
cout << endl << "输入要插入的数" << endl;
cin >> n;
t = head;
while (t!=NULL)
{
if (t->next->data > n) {
p = (struct node *)malloc(sizeof(struct node));
p->data = n;
p->next = t->next;
t->next = p;
break;
}
t = t->next;
}
t = head;
cout << "链表是" << endl;
while (t != NULL)
{
cout << t->data;
t = t->next;
}
return 0;
}