带头结点的单链表实现

下面是本人总结的关于带头结点的单链表的实现代码。供大家参考学习。

#include<iostream>
using namespace std;

typedef char datatype;

struct node
{
       datatype data;
       node * next;
};
//按序号查找链表 
node * getnode(node * head, int i)
{
     if(head->next == NULL)
     return NULL;
     
     node * p = head;
     int j = 0;
     while((j < i) && (p->next != NULL))
     {
              p = p->next;
              j++;
     }
     if(i > j)
     return NULL;
     else
     return p;         //说明没找到第 i 个结点 
}
//按值查找
node * locatenode(node * head, datatype key)
{
     if(head->next == NULL)
     return NULL;
     
     node * p = head->next;
     while((p->data != key)&& (p->next != NULL))
     {
                   p = p->next;
     }
     if(p->data == key)
     return p;
     else
     return NULL;
}
//删除第 i 个结点的实现
int link_del(node * head, int i)
{
     if(head->next == NULL)
     return -1;
     
     node * p = head;
     node * r;
     int j = 1;
     while((j < i) && (p->next != NULL))
     {
              p = p->next;
              j++;
     }
     if((j < i) || (p->next == NULL))
     return -2;
     
     r = p->next;
     p->next = p->next->next;
     free(r);
     
     return 0;
} 
//按序号插入 
int link_insert(node* head, int i, datatype x)  //i>0,即i=1,2,3... 表示第i个位置 
{
    if(head->next == NULL)     
    return -1;
    
    int j = 1;
    node * p = head;
    while((j<i)&&(p->next != NULL))
    {
        p = p->next;
        j++;     
    }
    if((i<=0)||(j < i)) //插入位置 i 错误 
    return -2;   
    //cout << p->data << endl;
    node * s = (node*)malloc(sizeof(node));
    s->data = x;
    s->next = p->next;
    p->next = s;
    return 0; 
}
//头插法建表 
node * creat_unbrainlink()
{
     node * s;
     node * head, * p;
     p = head = (node *)malloc(sizeof(node));   // 头结点 
     head->next = NULL;                         //这句很重要,说明链表起初为空 
     char ch = getchar();
     while(ch != '\n')
     {
              s = (node *)malloc(sizeof(node));
              s->data = ch;
              s->next = head->next;
              head->next = s;
              ch = getchar();
     }
     return head;
}
//尾插法建表 
node * creat_unbrainlink_end()
{
     node * head, *p, *s;
     head = (node *)malloc(sizeof(node));
     p = head;
     char ch = getchar();
     while(ch != '\n')
     {
              s = (node *)malloc(sizeof(node));
              s->data = ch;
              p->next = s;
              p = s; 
              ch = getchar();                
     } 
     //if(p != NULL)
          p->next = NULL;
          //s->next = NULL;                  //这样写也可以 
     return head;
} 
// 创建大小为n的链表
node * creat_link(int n)
{
     if(n < 1)
          return NULL;
     node * head, * p, *s;
     p = head = (node *)malloc(sizeof(node)); //头结点嘛?应该是 
     for(int i = 1; i <= n; i++)
     {
             s = (node * )malloc(sizeof(node));
             datatype x;
             cin >> x;
             s->data = x;
             p->next = s;
             p = s;          
     }
     p->next = NULL;
     return head;   
}
//链表显示函数
int show(node * head)
{
     if(head == NULL)
     return -1;
     
     node * p;
     p = head->next;         //p为头结点 
     while(p->next != NULL)  //为NULL说明该链表为空 
     {
                   cout << p->data << ends;
                   p = p->next;
     }
     cout << p->data << endl; 
     return 0;
}
//删除所有链表,并释放空间
int DestroyList(node* &head)
{
     node* temp = NULL;
     node* p = NULL;
     if(head == NULL)
     {
           cout << "链表不存在" << endl;;
           return -1;
     }
     temp = head;
     //temp->data = 'H';
     while(temp->next != NULL)
     {
                //cout << temp->data << endl;
                p = temp->next;
                free(temp);
                temp = p;
     }
     //cout << temp->data;
     free(temp);
     head = NULL;
     return 0;
}


int main()
{
    node * head;
    head = creat_link(5);
    //head = creat_unbrainlink();
    //head = creat_unbrainlink_end();
    show(head);
    
    link_del(head,3);
    show(head);
    //node * locte_node = locatenode(head, 's');
    //if(locte_node != NULL)
    //cout << locte_node->data << endl;
    
   // node* get_node = getnode(head, 3); 
    //if(get_node != NULL)
    //cout << get_node->data << endl;
    int result = link_insert(head, 5, 90);
    
    //cout << "result = " << result << endl;
    show(head);
    DestroyList(head);
    
    show(head);
    system("pause");
    return 0;  
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值