代码还是写出来更容易记住
下面的尾插还是有点瑕疵的,每次都重新找了尾结点,导致插入变成o(n),可以用一个变量记录一下尾指针,这样就是o(1)插入,但基础练习效果到了,不太想优化了~
#include<stdio.h>
#include<stdlib.h>
/*
1. CreateEmptyList(Node** head):创建一个空链表
2. CreateList(DataType *addr, unsigned int n, Node** head):根据给定的数组初始化链表 (此处数组事先设定为[0,1,2,3,4,5,6,7,8,9])
3. ListInsert(Node *head, unsigned int index, DataType e):在链表给定的位置(0---N-1)插入元素
4. ListDelete(Node *head, unsigned int index, DataType* e):删除链表给定位置的元素,将删除节点的值返回给e
5. DestroyList(Node *head):销毁链表,释放内存
*/
typedef struct Node {
int data;
struct Node* next;
} Node;
typedef struct Node* LinkList;
int op[10001], da[10001];
int n, now = 1;
Node* CreateEmptyList() {
Node *head;
head = (Node*)malloc(sizeof(Node));
head->next = NULL;
return head;
}
int CreateEmptyList(Node** head)
{
*head = (Node*)malloc(sizeof(Node));
(*head)->next = NULL;
return 1;
}
Node* CreateList(int *addr, unsigned int n) {
Node *head = CreateEmptyList();
Node *p = head;
for(int i = 0; i <= n; i++) {
p = p->next = (Node*)malloc(sizeof (Node)); //带头结点
p->data = addr[i];
}
p->next = NULL;
return head;
}
void ListInsert(Node *head, unsigned int index, int e) {
Node *p1 = head, *p2;
while(index--)p1 = p1->next;
p2 = (Node*)malloc(sizeof(Node));
p2->data = e;
p2->next = p1->next;
p1->next = p2;
}
void ListDelete(Node *head, unsigned int index, int &e) {
Node *p = head;
while(index--)p = p->next;
e = p->next->data;
p->next = p->next->next;
}
void DestroyList(Node *head) {
Node *p1, *p2 = head;
if(p2 == NULL)return;
while(p2 != NULL) {
p1 = p2;
p2 = p2->next;
free(p1);
}
}
void print(Node* head) {
Node *p = head->next;
for(; p != NULL; p = p->next)
printf("%d ", p->data);
puts("");
}
int main() {
scanf("%d", &n);
int cnt = 0;
for(int i = 0; i < n; i++) {
scanf("%d", &op[i]);
if(op[i] == 3 || op[i] == 4)cnt++;
}
for(int i = 1; i <= 2 * cnt; i++)scanf("%d", &da[i]);
int addr[11] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
Node * head;
int e;
for(int i = 0; i < n; i++) {
if(op[i] == 1)head = CreateEmptyList();
if(op[i] == 2)head = CreateList(addr, 9);
if(op[i] == 3)ListInsert(head, da[now], da[now + 1]), now += 2;
if(op[i] == 4)ListDelete(head, da[now], e), now += 2;
if(op[i] == 5)DestroyList(head), printf("%d\n", e);
else print(head);
}
return 0;
}
#include<bits/stdc++.h>
using namespace std;
typedef struct Node {
int data;
struct Node *next;
} node, *linklist;
node* create() {
node* head;
head = (node*)malloc(sizeof (node));
head->next = NULL;
return head;
}
void head_insert(node* head, int x) {
node* q = create();
q->data = x;
q->next = head->next;
head->next = q;
}
void tail_insert(node* head, int x) {
node *r = head; //尾指针
while(r->next != NULL)r = r->next;
node*q = create();
q->data = x;
r->next = q;
r = q;
}
void print(node* head) {
node *p = head->next;
for(; p; p = p->next)cout << p->data << ' ';
cout << "\n";
}
void delete_by_key(node*head, int x) {
node* pre = head, *p = head->next;
while(p != NULL) {
if(p->data == x) {
node*q = p;
p = p->next;
pre->next = p;
free(q);
} else {
pre = pre->next;
p = p->next;
}
}
}
void insert(node*p,int x){
node*q=create();
q->data=x;
q->next=p->next;
p->next=q;
}
node* find(node*head,int x){
node*p=head->next;
while(p->data<=x&&p->next!=NULL)p=p->next;
return p;
}
int main() {
node* head = create();
int n;
cin >> n;
for(int i = 0; i < n; i++) {
int x;
cin >> x;
tail_insert(head, x);
}
// cout << "输入要删除的值:\n";
// int k;
// cin >> k;
// delete_by_key(head, k);
//
// print(head);
//
// cout<<"输入插入的数字:\n";
// cin>>k;
// node* t = find(head,k);
// insert(t,k);
// print(head);
node *A=create(),*B=create(),*p=head->next;
int cnt=1;
for(;p;p=p->next){
if(cnt&1){
tail_insert(A,p->data);
}else{
head_insert(B,p->data);
}
cnt++;
}
print(A);
print(B);
return 0;
}