码迷,mamicode.com
首页 > 其他好文 > 详细

单链表的基本操作(建立.遍历.添加.删除)

时间:2015-09-27 17:29:16      阅读:258      评论:0      收藏:0      [点我收藏+]

标签:

 

有n个元素,我们把他们用链表表示,执行一些操作在某个位置添加一个数,或者删除某个位置的数;

输入合法的代码:

技术分享
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
using namespace std;
struct node
{
    int date;
    struct node *next;
};

node *CreatList(node *head, int n);
void TravList(node *head);
node *AddList(node *head, int n, int pos, int num);
node *DelList(node *head, int n, int pos);

int main()
{
    int n, pos, num;
    node *head;
    while(scanf("%d", &n)!=EOF)
    {
        head = (node*)malloc(sizeof(node));

        head = CreatList(head, n);
        TravList(head);

        scanf("%d %d",&pos, &num);///在第pos个位置插入num;
        head = AddList(head, n+1, pos, num);
        TravList(head);

        scanf("%d", &pos);///删除第pos个位置的数;
        head = DelList(head, n+1, pos);
        TravList(head);

    }
    return 0;
}
node *CreatList(node *head, int n)
{
    head->next = NULL;
    for(int i=1; i<=n; i++)
    {
        node *p;
        p = (node *)malloc(sizeof(node));
        scanf("%d", &p->date);
        p->next = head->next;
        head->next = p;
    }
    return head;
}
void TravList(node *head)
{
    node *real;
    real = head->next;
    while(real != NULL)
    {
        printf("%d ", real->date);
        real = real->next;
    }
    printf("\n");
}
node *AddList(node *head, int n, int pos, int num)
{
    node *pre, *p, *q;
    pre = head;
    p = pre->next;
    int i=1;
    while(i<pos)
    {
        pre = p;
        p = p->next;
        i++;
    }
    q = (node*)malloc(sizeof(node));
    q->date = num;
    q->next = pre->next;
    pre->next = q;
    return head;
}
node *DelList(node *head, int n, int pos)
{
    node *pre, *p;
    pre = head;
    p = pre->next;
    int i=1;
    while(i<pos)
    {
        pre = p;
        p = p->next;
        i++;
    }
    pre->next = p->next;
    free(p);
    return head;
}
/*
5
1 2 3 4 5
3 10
1

5 4 3 2 1
5 4 10 3 2 1
4 10 3 2 1
*/
View Code

对所有输入都可以的代码:

技术分享
#include<stdio.h>
#include<stdlib.h>
#define OK 1
#define ERROR 0
typedef int Status;

typedef struct Lnode
{
    int data;
    struct Lnode *next;
} Lnode,*Linklist;

int m=sizeof(Lnode);

Linklist Creatlist(int n);
Linklist CreatlistRear(int n);
void Trealist(Linklist L);
Status Listdel(Linklist L,int k);
Status ListAdd(Linklist L,int num,int x);

Linklist Creatlist(int n)///从表的前面插入;
{
    Linklist head,p;
    int i;
    head=(Linklist)malloc(m);
    head->next=NULL;
    for(i=1;i<=n;i++)
    {
        p=(Linklist)malloc(m);
        scanf("%d",&p->data);
        p->next=head->next;
        head->next=p;
    }
    return head;
}

Linklist CreatlistRear(int n)///从表的后面插入;
{
    Linklist rear,p,head;
    int i;
    head=(Linklist)malloc(m);
    head->next=NULL;
    rear=head;
    for(i=0;i<n;i++)
    {
        p=(Linklist)malloc(m);
        scanf("%d",&p->data);
        rear->next=p;
        rear=p;
    }
    rear->next=NULL;
    return head;
}

void Trealist(Linklist L)///遍历单链表;
{
    Linklist p;
    p=L->next;
    while(p)
    {
        printf("%d ",p->data);
        p=p->next;
    }
    printf("\n");
}
Status Listdel(Linklist L,int k)///删除单链表的元素;
{
    int j=1;
    Linklist p=L,q=L->next;
    while(q&&j<k)
    {
        p=q;
        q=q->next;
        j++;
    }
    if((q==0 && j<=k) || k<1 )
        return ERROR;
    p->next=q->next;
    free(q);
    return OK;
}
Status ListAdd(Linklist L,int num,int x)///添加单链表的元素;
{
    Linklist p,q,s;
    p=L;
    q=p->next;
    int j=1;
    while(q&&j<x)
    {
        p=q;
        q=q->next;
        j++;
    }
    if((q==0&&j<x)||x<1)
        return ERROR;
    s=(Linklist)malloc(m);
    s->data=num;
    s->next=p->next;
    p->next=s;
    return OK;
}

int main()
{
    int n,k,num,x;
    while(scanf("%d",&n)!=EOF)
    {
        Linklist L;
        //L=Creatlist(n);
        L=CreatlistRear(n);//两种建立链表的方法;
        Trealist(L);
        printf("请输入要删除的位置\n");
        scanf("%d",&k);
        int m=Listdel(L,k);
        if(m==1)
            printf("删除成功\n");
        else
            printf("删除失败\n");
        Trealist(L);
        printf("请输入要插入的数和位置\n");
        scanf("%d %d",&num,&x);
        m=ListAdd(L,num,x);
        if(m==1)
            printf("插入成功\n");
        else
            printf("插入失败\n");
        Trealist(L);
    }
    return 0;
}
View Code

 

单链表的基本操作(建立.遍历.添加.删除)

标签:

原文地址:http://www.cnblogs.com/zhengguiping--9876/p/4842389.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!