#include <stdio.h>
#include <stdlib.h>
struct node
{
unsigned int elem;
struct node *next;
};
struct node *head = NULL;
struct node *tail = NULL;
void create_list(unsigned int elem);
void insert_node(int pos, unsigned int elem);
void delete_node(int pos);
void print_linklist(struct node *linklist_head);
int search(unsigned int elem);
int main(void)
{
struct node *head1 = NULL;
struct node *head2 = NULL;
struct node *p = NULL; //head1
struct node *q = NULL; //head2
create_list(1);
create_list(9);
create_list(13);
create_list(27);
head1 = head;
print_linklist(head1);
head = NULL;
create_list(3);
create_list(5);
create_list(14);
create_list(81);
create_list(88);
create_list(95);
create_list(99);
head2 = head;
print_linklist(head2);
head = NULL;
p = head1;
q = head2;
while(p && q)
{
if(p->elem <= q->elem)
{
if(head == NULL)
head = p;
else
tail->next = p;
tail = p;
p = p->next;
}
else
{
if(head == NULL)
head = q;
else
tail->next = q;
tail = q;
q = q->next;
}
}
tail->next = p?p:q;
print_linklist(head);
return 0;
}
void create_list(unsigned int elem)
{
struct node *p = (struct node *)malloc(sizeof(struct node));
p->elem = elem;
p->next = NULL;
if(head == NULL)
head = p;
else
tail->next = p;
tail = p;
}
void insert_node(int pos, unsigned int elem)
{
struct node *pre;
pre = head;
int i = 0;
struct node *p = (struct node *)malloc(sizeof(struct node));
if(pos == 0)
{
p->elem = elem;
p->next = head;
head = p;
}
else
{
while(i < pos - 1)
{
pre = pre->next;
i++;
}
p->elem = elem;
p->next = pre->next;
pre->next = p;
if(p->next == NULL)
tail = p;
}
}
void delete_node(int pos)
{
struct node *pre, *p;
pre = head;
int i = 0;
if(pos == 0)
{
head = head->next;
free(pre);
}
else
{
while(i < pos - 1)
{
pre = pre->next;
i++;
}
p = pre->next;
pre->next = p->next;
if(p->next == NULL)
tail = pre;
free(p);
}
}
void print_linklist(struct node *linklist_head)
{
struct node *p;
for(p = linklist_head; p; p = p->next)
printf("%5d", p->elem);
printf("\n");
}
int search(unsigned int elem)
{
struct node *p;
for(p = head; p; p = p->next)
if(p->elem == elem)
return 1;
return 0;
}
一道链表合并比较坑的题目,一直没有发现问题,主要是题目没有明确说要这样,一直没有看到
输入:1-2-4, 1-3-4
输出:1-1-2-3-4-4,它的是这样要求的,2个数字之间需一个空格,也就是开头和最后结尾是没有空格的。最后应该是这样:1空格1空格2空格3空格4空格4
我们只需要把我们上面的代码的print_linklist改成这样就行
p = linklist_head;
while (p->next) {
printf("%d ", p->elem);
p = p->next;
}
if (p->next == NULL)
printf("%d", p->elem);
单链表冒泡排序
#include<stdio.h>
#include<stdlib.h>
typedef struct node
{
int data;
struct node *next;
}node;
node *creat(node *head)
{
node *p = head, *pr = NULL;
pr = (node *)malloc(sizeof(node));
printf("data:");
scanf("%d",&pr->data);
if(head == NULL)
head = pr;
else {
while(p->next != NULL) {
p = p->next;
}
p->next = pr;
}
pr->next=NULL;
return head;
}
void print(node *head)//打印链表
{
node *p = head;
while(p != NULL) {
printf("%2d ",p->data);
p = p->next;
}
printf("\n");
}
void dele(node *head)//释放链表申请的空间
{
node *p = head,*pr = NULL;
while(p != NULL) {
pr = p;
p = p->next;
free(pr);
}
}
/* 思路 */
/*定义2个指针,1个指针指向当前节点,1个指针指向下一个节点,
找到相邻的2个数,前一个数比后一个数大的话,就交换位置*/
void sort(node *head)//对链表进行冒泡排序
{
node *p = NULL,*pr = NULL;//双重循环所需要的两个结构体指针
int temp;
for(p = head; p->next != NULL; p = p->next) {//第一个循环
for(pr = p->next;pr != NULL;pr = pr->next) {//第二个循环
if(p->data > pr->data) { /* 前1个数比后一个数大,就交换位置 */
temp = p->data; /* 保存前一个数到temp*/
p->data = pr->data; /* 后一个数赋值给前一个数 */
pr->data = temp;
}
}
}
}
int main(void)
{
int n;
node *head = NULL;
printf("please input n nmber\n");
scanf("%d",&n);
while(n) {
head = creat(head);
n--;
}
sort(head);
print(head);
dele(head);
return 0;
}
运行结果: