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

1.写一个最基本的链表

时间:2016-03-05 13:00:48      阅读:110      评论:0      收藏:0      [点我收藏+]

标签:

#include<stdio.h>
#include<stdlib.h>
typedef int ElemType; 
struct Node{
    Node *next;
    ElemType data;
};
void CreateList(Node*&L)
{
    L = (Node*)malloc(sizeof(Node));
    if(!L) exit(-2);
    L->next = NULL;
    L->data = 0;
}
void InsertList(Node*&L,ElemType e)
{
    Node *p = L;
    for(int i=0;i<L->data;++i)
    {
        p=p->next;
    }
    Node *s = NULL;
    s=(Node*)malloc(sizeof(Node));
    s->data = e;
    s->next = p->next;
    p->next = s;
    ++(L->data);
}
void DeleteList(Node*&L,int n)
{
    Node*p = L;
    if(n<1||n>L->data) exit(-2);
    for(int i = 0;i<n-1;i++)
    {
        p=p->next;
    }
    Node*temp = p->next;
    p->next = p->next->next;
    free(temp);
}
void ShowList(Node*&L)
{
    Node *p = L;
    while(p){
        printf(">%d\t",(p)->data);
        p=p->next;
    }
        
    printf("\n");
}
int main()
{
    Node *N1;
    CreateList(N1);
    InsertList(N1,1);
    ShowList(N1);
    InsertList(N1,2);
    ShowList(N1);
    InsertList(N1,3);
    ShowList(N1);
    InsertList(N1,4);
    ShowList(N1);
    InsertList(N1,5);
    ShowList(N1);
    DeleteList(N1,4);
    ShowList(N1);
    DeleteList(N1,2);
    ShowList(N1);
}
    

 

1.写一个最基本的链表

标签:

原文地址:http://www.cnblogs.com/minemine/p/5244421.html

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