sdut 链表4

简介: sdut 链表4

输出格式:

第一行输出初始链表元素个数;

第二行输出按照逆位序所建立的初始链表;

第三行输出删除重复元素后的单链表元素个数;

第四行输出删除重复元素后的单链表。

输入样例:

10
21 30 14 55 32 63 11 30 55 30

输出样例:

10
30 55 30 11 63 32 55 14 30 21
7
30 55 11 63 32 14 21
 
 

代码长度限制

16 KB

时间限制

400 ms

内存限制

64 MB

栈限制

8192 KB

#include <stdio.h>
#include <stdlib.h>
struct node
{
    int a;
    struct node *next;
};
 
struct node *creat(int n);
//链表建立;
void putout(struct node *head);
//链表输出;
void delete_(struct node *head,int n);
//重复节点删除;
 
int main()
{
    int n;
    struct node *head;
    head = (struct node *)malloc(sizeof(struct node));
    head->next = NULL;//头结点的建立;
    scanf("%d",&n);
 
    head = creat(n);
    //建立链表;
    printf("%d\n",n);
    putout(head);
    //输出链表;
    delete_(head,n);
    //删除节点;
    putout(head);
    //输出链表;
 
 
    return 0;
}
 
 
struct node *creat(int n)
//链表建立;
{
    struct node *head,*p;
    head = (struct node *)malloc(sizeof(struct node));
    head->next = NULL;
    while (n--)
    {
        p = (struct node *)malloc(sizeof(struct node));
        p->next = head->next;
        scanf("%d",&p->a);
        head->next = p;//逆序建立;
    }
    return head;
};
 
 
 
void putout(struct node *head)
//链表输出函数;
{
    struct node *p = head->next;
    while(p)
    {
        if (p->next)
        {
            printf("%d ",p->a);
        }
        else
        {
            printf("%d\n",p->a);
        }
        p = p->next;//注意不要遗落;
    }
}
 
 
 
void delete_(struct node *head,int n)
//重复节点删除;
{
    struct node *p,*pi,*q;
    q = head->next;
 
 
    while(q)
    {
        p = q;
        pi = q->next;
        while(pi)
        {
            if (pi->a == q->a)
            
            {
                struct node *r = pi;
                p->next = pi->next;
                pi = pi->next;
                free(r);//释放删除的节点;
                n--;
            }
            else
            {
                pi = pi->next;
                p = p->next;
            }
        }
        q = q->next;
    }
    printf("%d\n",n);
}


目录
相关文章
|
1月前
sdut链表lab2
sdut链表lab2
27 1
|
1月前
|
存储
sdut 链表lab1
sdut 链表lab1
23 1
|
1月前
|
存储
sdut 链表5
sdut 链表5
31 1
|
1月前
【移除链表元素】LeetCode第203题讲解
【移除链表元素】LeetCode第203题讲解
|
17天前
|
存储 SQL 算法
LeetCode力扣第114题:多种算法实现 将二叉树展开为链表
LeetCode力扣第114题:多种算法实现 将二叉树展开为链表
|
17天前
|
存储 SQL 算法
LeetCode 题目 86:分隔链表
LeetCode 题目 86:分隔链表
|
22天前
|
存储 算法 Java
【经典算法】Leetcode 141. 环形链表(Java/C/Python3实现含注释说明,Easy)
【经典算法】Leetcode 141. 环形链表(Java/C/Python3实现含注释说明,Easy)
10 2
|
1月前
<数据结构>五道LeetCode链表题分析.环形链表,反转链表,合并链表,找中间节点.
<数据结构>五道LeetCode链表题分析.环形链表,反转链表,合并链表,找中间节点
24 1
|
14天前
|
算法
【经典LeetCode算法题目专栏分类】【第7期】快慢指针与链表
【经典LeetCode算法题目专栏分类】【第7期】快慢指针与链表
|
18天前
|
算法 数据挖掘 Python
LeetCode题目25 hard:K个一组翻转链表 【分治策略 Python】
LeetCode题目25 hard:K个一组翻转链表 【分治策略 Python】