===============================================================================
C 数据结构双向链表
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#include<assert.h>
typedef struct PNode
{
int data;
struct PNode *next,*last;
}Node;
void CreateList(Node *&N)
{
N =(Node *)malloc(sizeof(Node));
N->data = 0;
N->next = N->last = 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 = 1;
Node *p = N;
Node *L = (Node *)malloc(sizeof(Node));
L->next = L->last = NULL;
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;
if(p->next != NULL) //注意这里的条件判断
p->next->last = L;
p->next = L;
L->last = p;
return 1;
}
//注:无论是插入还是删除都是找要插入或删除的前一个结点
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;
if(q->next != NULL)
p->next->last = q;
free(q);
return 1;
}
void main()
{
Node *Node = NULL;
int a[]={1, 2, 3, 4, 5};
CreateList(Node);
printf("%d/n",GetLength(Node));
/* if(Node != NULL)
DisplayList(Node);*/
if(InsertList(Node,1,2))
printf("Insert success!!/n");
if(InsertList(Node,1,324))
printf("Insert success!!/n");
if(InsertList(Node,1,435))
printf("Insert success!!/n");
if(InsertList(Node,1,567))
printf("Insert success!!/n");
DisplayList(Node);
if(DeletetList(Node,3))
printf("Delete success!!/n");
DisplayList(Node);
}
===============================================================================
C数据结构双向循环链表
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#include<assert.h>
typedef struct PNode
{
int data;
struct PNode *next,*last;
}Node;
void CreateList(Node *&N)
{
N =(Node *)malloc(sizeof(Node));
N->data = 0;
N->next = N->last = N;
}
int GetLength(Node *N)
{
assert(N);
int i = 1;
Node *p = N->next;
while(p != N)
{
p = p->next;
i++;
}
return i;
}
void DisplayList(Node *N)
{
if(N == NULL)
return;
Node *p = N->next;
while(p != N)
{
printf("%d/n",p->data);
p = p->next;
}
}
int InsertList(Node *N,int n,int x)
{
assert(N);
int i = 1;
Node *p = N;
Node *L = (Node *)malloc(sizeof(Node));
L->next = L->last = NULL;
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;
if(p->next != N) //注意这里的条件判断
p->next->last = L;
p->next = L;
L->last = p;
return 1;
}
//注:无论是插入还是删除都是找要插入或删除的前一个结点
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;
if(q->next != NULL)
p->next->last = q;
free(q);
return 1;
}
void main()
{
Node *Node = NULL;
int a[]={1, 2, 3, 4, 5};
CreateList(Node);
if(Node != NULL)
printf("%d/n",GetLength(Node));
/* if(Node != NULL)
DisplayList(Node);*/
if(InsertList(Node,1,2))
printf("Insert success!!/n");
if(InsertList(Node,1,324))
printf("Insert success!!/n");
if(InsertList(Node,1,435))
printf("Insert success!!/n");
if(InsertList(Node,1,567))
printf("Insert success!!/n");
DisplayList(Node);
if(DeletetList(Node,3))
printf("Delete success!!/n");
DisplayList(Node);
}
1192

被折叠的 条评论
为什么被折叠?



