链表源码之单向链表

C数据结构单向链表

#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#include<assert.h>
typedef struct PNode
{
 int data;
 struct PNode *next;
}Node;
void CreateListFirstInsert(Node *&N,int a[],int n)
{
          Node *T;
          N =(Node *)malloc(sizeof(Node));
          N->data = 0;
          N->next = NULL;
          for(int i = 0;i<= n;i++)
          {
                    T = (Node *)malloc(sizeof(Node));
                    T->data = a[i];
                    T->next = N->next;
                    N->next = T;
          }
}
void CreateListLastInsert(Node *&N,int a[],int n)
{
          Node *T,*R;
          N =(Node *)malloc(sizeof(Node));
          N->data = 0;
          R = N;
          for(int i = 0;i< n;i++)
          {
                    T = (Node *)malloc(sizeof(Node));
                    T->data = a[i];
                    R->next = T;
                    R = R->next;
          }
          T->next = NULL;
}
int GetLength(Node *N)
{
          assert(N);
          int i = 0;
          Node *p = N;
          while(p != NULL)
          {
                    p = p->next;
                    i++;
          }
          return i;
}
void DisplayList(Node *N)
{
          if(N == NULL)
                    return;
         Node *p = N->next;
          while(p != NULL)
          {
                    printf("%d/n",p->data);
                    p = p->next;
          }
}
int InsertList(Node *N,int n,int x)
{
          assert(N);
          int i = 0;
          Node *p = N;
          Node *L = (Node *)malloc(sizeof(Node));
          L->data = x;
          if(n <=0 || n >= GetLength(N))
          {
                    printf("please enter a right number!!");
                    return 0;
          }
          while(i < n)
          {
                    p = p->next;
                    i++;
          }
          L->next = p->next;
          p->next = L;
 
          return 1;
}
//首先要保证变量n的正确性,用p指向它,q指向要删除的结点。
int DeletetList(Node *N,int n)
{
          assert(N);
          int i = 1;
          Node *p = N,*q;
          if(n <=0 || n >= GetLength(N))
          {
                    printf("please enter a right number!!");
                    return 0;
          }
          while(i < n)
          {
                    p = p->next;
                    i++;
          }
          q = p->next;
          p->next = q->next;
          return 1;
}
void main()
{
          Node *Node = NULL;
          int a[]={1, 2, 3, 4, 5};
          CreateListLastInsert(Node,a,5);


          if(Node != NULL)
                    DisplayList(Node);


          if(InsertList(Node,3,6))
                    printf("Insert success!!/n");

          if(DeletetList(Node,3))
                    printf("Delete success!!/n");
}

===================================================================

C数据结构单向循环链表

#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#include<assert.h>
typedef struct PNode
{
          int data;
          struct PNode *next;
}Node;
void CreateListFirstInsert(Node *&N,int a[],int n)
{
          Node *T;
          N =(Node *)malloc(sizeof(Node));
          N->data = 0;
          N->next = N;  //注意:此处和单向链表的差异
          for(int i = 0;i<= n;i++)
          {
                    T = (Node *)malloc(sizeof(Node));
                    T->data = a[i];
                    T->next = N->next;
                    N->next = T;
          }
}
void CreateListLastInsert(Node *&N,int a[],int n)
{
          Node *T,*R;
          N =(Node *)malloc(sizeof(Node));
          N->data = 0;
          R = N;


          for(int i = 0;i< n;i++)
          {
  
                    T = (Node *)malloc(sizeof(Node));
                    T->data = a[i];
                    R->next = T;
                    R = R->next;
          }


          T->next = NULL;
}
//设置i为计数器,当p指针等于头结点时结束查找
int GetLength(Node *N)
{
          assert(N);
          int i = 0;
          Node *p = N->next;
          while(p != N)
          {
                    i++;
                    p = p->next;
          }
          return i;
}
void DisplayList(Node *N)
{
          if(N == NULL)
                    return;
          Node *p = N->next;
          while(p != NULL)
          {
                    printf("%d/n",p->data);
                    p = p->next;
          }
}
int InsertList(Node *N,int n,int x)
{
          assert(N);
          int i = 0;
          Node *p = N;
          Node *L = (Node *)malloc(sizeof(Node));
          L->data = x;


          if(n <=0 || n >= GetLength(N))
          {
                    printf("please enter a right number!!");
                    return 0;
          }


          while(i < n)
          {
                    p = p->next;
                    i++;
          }


          L->next = p->next;
          p->next = L;
 
          return 1;
}
//首先要保证变量n的正确性,用p指向它,q指向要删除的结点。
int DeletetList(Node *N,int n)
{
          assert(N);
          int i = 1;
          Node *p = N,*q;
          if(n <=0 || n >= GetLength(N))
          {
                    printf("please enter a right number!!");
                    return 0;
          }
          while(i < n)
          {
                    p = p->next;
                    i++;
          }
          q = p->next;
          p->next = q->next;


          return 1;
}
void main()
{
          Node *Node = NULL;
          int a[]={1, 2, 3, 4, 5};
           CreateListFirstInsert(Node,a,5);


          if(Node != NULL)
                    printf("create success length is %d!!/n",GetLength(Node));


          if(InsertList(Node,3,6))
                    printf("Insert success!!/n");


          if(DeletetList(Node,3))
                    printf("Delete success!!/n");/**/
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值