- 逆置采用头插法逆置
-2022.3.15 更新编码风格,新增单链表合并、单链表链接功能
#include <stdio.h>
#include <stdlib.h>
typedef struct node{
int data;
struct node* next;
} node;
node* linkedlist_init();
void linkedlist_destory(node* head);
int linkedlist_insert(node* head, int pos, int x);
int linkedlist_delete(node* head, int pos, int* x);
void linkedlist_print(node* head);
int linkedlist_size(node* head);
int linkedlist_search(node* head, int x);
void linkedlist_reverse(node* head);
void linkedlist_link(node* head, node* head2);
void linkedlist_merge(node* h1, node* h2);
int main(){
node* head = linkedlist_init();//初始化
int max, i, value;
printf("输入元素个数:");
scanf("%d", &max);
printf("数据:");
for (i = 0; i < max; i++){
scanf("%d", &value);
linkedlist_insert(head, 1, value);
}
linkedlist_reverse(head);
linkedlist_print(head);
int choice;
while (1){
printf("1)插入元素\n");
printf("2)删除元素\n");
printf("3)求表长\n");
printf("4)查找数据\n");
printf("5)单链表逆置\n");
printf("6)打印当前单链表\n");
printf("7)将当前单链表与另一单链表合并\n");
printf("8)将另一单链表与当前单链表链接\n");
printf("9)退出\n");
scanf("%d", &choice);
if (choice == 1){ //插入元素
int pos;
printf("待插入位置及数值:");
scanf("%d%d", &pos, &value);
linkedlist_insert(head, pos, value);
linkedlist_print(head);
}
else if (choice == 2){//删除元素
int x, ret, pos;
printf("待删元素位置:");
scanf("%d", &pos);
ret = linkedlist_delete(head, pos, &x);
if (ret == 0)
printf("删除失败,位置不合法!\n");
else
printf("数据%d已删除!\n", x);
linkedlist_print(head);
}
else if (choice == 3)//求表长
printf("当前表长为:%d\n", linkedlist_size(head));
else if (choice == 4){//查找
int pos;
printf("待查找数据:");
scanf("%d", &value);
pos = linkedlist_search(head, value);
if (pos == 0)
printf("表中不存在该数据!\n");
else
printf("数据%d位于%d位置\n", value, pos);
}
else if (choice == 5){//逆置
linkedlist_reverse(head);
linkedlist_print(head);
}
else if (choice == 6)
linkedlist_print(head);
else if (choice == 7){
node* head2 = linkedlist_init();
printf("输入待插入单链表元素个数:");
scanf("%d", &max);
printf("待插入单链表数据:");
for (i = 0; i < max; i++){
scanf("%d", &value);
linkedlist_insert(head2, 1, value);
}
linkedlist_reverse(head2);
linkedlist_merge(head, head2);
linkedlist_print(head);
}
else if (choice == 8){
node* head2 = linkedlist_init();
int max, value, i;
printf("输入待插入单链表元素个数:");
scanf("%d", &max);
printf("待插入单链表数据:");
for (i = 0; i < max; i++){
scanf("%d", &value);
linkedlist_insert(head2, 1, value);
}
linkedlist_reverse(head2);
linkedlist_link(head, head2);
linkedlist_print(head);
}
else if (choice == 9)
break;
}
//销毁
linkedlist_destory(head);
return 0;
}
node* linkedlist_init(){
node* p = (node*)malloc(sizeof(node));
p->data = 0;
p->next = NULL;
return p;//返回头节点
}
void linkedlist_destory(node* head){
node* p;
while (head != NULL)
{
p = head->next;
free(head);
head = p;
}
}
int linkedlist_insert(node* head, int pos, int x){
if (pos < 0)
return 0;
if (pos == 0)
pos = 1;
node* p = head;
int i;
for (i = 0; i < pos - 1; i++){
if (p == NULL)
return 0;
p = p->next;
}
node* nn = (node*)malloc(sizeof(node));
nn->next = p->next;
nn->data = x;
p->next = nn;
return 1;
}
int linkedlist_delete(node* head, int pos, int* x){
int size;
size = linkedlist_size(head);
if (pos < 0 || pos > size)
return 0;
if (pos == 0)
pos = 1;
node* p = head;
int i;
for (i = 0; i < pos - 1; i++){
if (p == NULL)
return 0;
p = p->next;
}
node* q = p->next;
*x = q->data;
p->next = q->next;
free(q);
return 1;
}
void linkedlist_print(node* head){
printf("当前表中数据为:");
node* p = head->next;
while (p != NULL){
printf("%d ", p->data);
p = p->next;
}
printf("\n");
}
int linkedlist_size(node* head){
node* p = head->next;
int size = 0;
while (p != NULL){
size++;
p = p->next;
}
return size;
}
int linkedlist_search(node* head, int x){
node* p = head->next;
int pos = 1;
while (p != NULL){
if (p->data == x)
return pos;
p = p->next;
pos++;
}
return 0;
}
void linkedlist_reverse(node* head){
node *p, *q;
p = head->next;
head->next = NULL;
while (p != NULL){
q = p;
p = p->next;
q->next = head->next;
head->next = q;
}
}
void linkedlist_merge(node* h1, node* h2){
node *pa, *pb, *pc;
pa = h1->next, pb = h2->next;
pc = h1;
while (pa != NULL && pb != NULL){
if (pa->data <= pb->data)
pc->next = pa, pa = pa->next;
else
pc->next = pb, pb = pb->next;
pc = pc->next;
}
if (pa != NULL)
pc->next = pa;
else
pc->next = pb;
free(h2);
linkedlist_print(h1);
}
void linkedlist_link(node* head, node* head2){
node* p;
for (p = head; p->next != NULL; p = p->next);
p->next = head2->next;
free(head2);
}