链表源码之单向链表

    技术2024-06-23  69

    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");/**/}

     

    最新回复(0)