链表基本操作

数据结构实验之链表一:顺序建立链表

#include <stdio.h>
#include <stdlib.h>
struct node
{
    int data;
    struct node *next;
};
void creat(struct node *head, int n){
    struct node *tail = head, *p;
    int x;
    for(int i=0;i<n;i++){
        p = (struct node *)malloc(sizeof(struct node));
        scanf("%d", &p->data);
        p->next = NULL;
        tail->next = p;
        tail = p;
    }
}

void show(struct node* head){
    struct node *p = head->next;
    while(p){
        printf("%d ",p->data);
        p = p->next;
    }
}

int main()
{
    int n, x;
    struct node *head = (struct node*)malloc(sizeof(struct node));
    head->next = NULL;
    scanf("%d", &n);
    creat(head, n);
    show(head);
    return 0;
}

数据结构实验之链表二:逆序建立链表

#include <stdio.h>
#include <stdlib.h>
struct node
{
    int data;
    struct node *next;
};
void creat(struct node *head, int n){
    struct node *p;
    int x;
    for(int i=0;i<n;i++){
        p = (struct node *)malloc(sizeof(struct node));
        scanf("%d", &p->data);
        p->next = head->next;
        head->next = p;
    }
}

void show(struct node* head){
    struct node *p = head->next;
    while(p){
        printf("%d ",p->data);
        p = p->next;
    }
}

int main()
{
    int n, x;
    struct node *head = (struct node*)malloc(sizeof(struct node));
    head->next = NULL;
    scanf("%d", &n);
    creat(head, n);
    show(head);
    return 0;
}

数据结构实验之链表七:单链表中重复元素的删除

#include <stdio.h>
#include <stdlib.h>
struct node
{
    int data;
    struct node *next;
};
void creat(struct node *head, int n){
    struct node *p;
    int x;
    for(int i=0;i<n;i++){
        p = (struct node *)malloc(sizeof(struct node));
        scanf("%d", &p->data);
        p->next = head->next;
        head->next = p;
    }
}

void show(struct node* head){
    struct node *p = head->next;
    while(p->next){
        printf("%d ",p->data);
        p = p->next;
    }
    printf("%d\n",p->data);
}

void del(struct node *head, int n){
    struct node *p, *t, *q;
    p=head->next;
    while(p!=NULL)//外循环以p开始
    {
        t=p;
        q=t->next;// t指针总为q指针的前一位,方便释放内存及连接q的前后
        while(q!=NULL)
        {
            if(p->data==q->data)
            {
                n--;
                t->next=q->next;
                free(q);
                q=t->next;//继续后移
            }
            else
            {
                t=t->next;
                q=q->next;//无相等的,同时后移
            }
        }
        p=p->next;    //下次循环
    }
    printf("%d\n", n);
    show(head);
}

int main()
{
    int n, x;
    struct node *head = (struct node*)malloc(sizeof(struct node));
    head->next = NULL;
    scanf("%d", &n);
    creat(head, n);
    printf("%d\n",n);
    show(head);
    del(head, n);
    return 0;
}

数据结构实验之链表九:双向链表

#include <stdio.h>
#include <stdlib.h>
struct node
{
    int data;
    struct node *next, *front;
};
void creat(struct node *head, int n){
    struct node *p, *tail = head;
    int x;
    for(int i=0;i<n;i++){
        p = (struct node *)malloc(sizeof(struct node));
        scanf("%d", &p->data);
        p->next=tail->next;
        tail->next=p;
        p->front=tail;
        tail=p;
    }
}

void show(struct node* head){
    struct node *p = head->next;
    while(p->next){
        printf("%d ",p->data);
        p = p->next;
    }
    printf("%d\n",p->data);
}

int main()
{
    int n, x, m;
    struct node *head = (struct node*)malloc(sizeof(struct node)), *p;
    head->next = NULL;
    head->front = NULL;
    scanf("%d%d", &n, &m);
    creat(head, n);
    while(m--)
    {
        int key;
        scanf("%d",&key);
        p=head->next;
        while(p)
        {
            if(p->data==key)
            {
                if(p->front!=head&&p->next)
                {
                    printf("%d %d\n",p->front->data,p->next->data);
                }
                else if(p->next==NULL&&p)
                {
                    printf("%d\n",p->front->data);
                }
                else if(p->front==head&&p)
                {
                    printf("%d\n",p->next->data);
                }
            }
            p=p->next;
        }
    }
    return 0;
}

数据结构实验之链表三:链表的逆置
//将头结点与之后链表断开,然后逆序建表

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值