前面我们已经说了线性表的顺序实现,
http://blog.itpub.net/7728585/viewspace-2124937/
下面我们将讨论一下线性表的链表实现。
链表结构使用得非常多,不管是操作系统还是数据库都是使用非常频繁的一种数据结构,
由于其相对灵活的内存使用,并且快速的插入和删除,都是非常有优势的。
这里通过C语言实现5个功能:
1、使用线性表的顺序结构初始化链表结构
2、初始化链表结构为指定的大小
3、获取链表中的指定位置的元素
4、在指定位置后面插入一个元素
5、删除指定位置的元素
先来看一个大概的图,说明是是加NODENEW加入到节点NODE1后面的情况,实际上删除也是类似,具体实现看代码

首先我们需要定义个头结点指向第一个NODE,NODE中有NEXT指针指向下一个结点,在这种结构中,
查看固定元素的时间复杂度最坏也是O(n),而插入一元素时间复杂度为O(1),删除一个元素复杂度也是O(1)
但是我们应该清楚如果要插入指定位置或者删除指定位置的元素首先需要的是查询,那么需要的时间则是他们
相加。来看C语言实现,在整个代码文件中没有使用头文件导致每个文件都需要定义一些需要的定义!同时我
使用了函数重载来实现
1、使用线性表的顺序结构初始化链表结构
2、初始化链表结构为指定的大小
不可避免的引入了一点点C++的知识
-
链表实现:
-
#include<iostream>
-
#include<stdio.h>
-
#include <stdlib.h>
-
typedef int datatype;
-
typedef int Etype;
-
typedef unsigned int uint;
-
-
typedef struct node
-
{
-
datatype data;
-
struct node *next;
-
} Node,*Nodep;
-
-
typedef struct hnode
-
{
-
int clenth;
-
Nodep headp;
-
} Hnode,*Hnodep;
-
-
-
typedef struct sqlist
-
{
-
Etype* elem; //pointer of sqlist base address
-
uint length; //current length of elem
-
uint m_size; //
-
} SQLIST;
-
-
void initchain(Hnodep hnode,int n)
-
{
-
Nodep p;
-
int i;
-
hnode->clenth = 0;
-
hnode->headp = (Nodep)malloc(sizeof(Node));
-
hnode->headp->next = NULL;
-
hnode->headp->data = 0;
-
(hnode->clenth)++;
-
for(i=(n-1);i>0;--i)
-
{
-
p = (Nodep)malloc(sizeof(Node));
-
p->next = hnode->headp->next;
-
hnode->headp->next = p;
-
p->data = 0;
-
(hnode->clenth)++;
-
}
-
}
-
-
-
void initchain(Hnodep hnode,const SQLIST* sqdata)
-
{
-
Nodep p;
-
int i;
-
int t=1;
-
int sqlen = sqdata->length;
-
hnode->clenth = 0;
-
hnode->headp = (Nodep)malloc(sizeof(Node));
-
hnode->headp->next = NULL;
-
hnode->headp->data = *(sqdata->elem);
-
(hnode->clenth)++;
-
sqlen--;
-
for(i=sqlen;i>0;--i)//every time insert elem after first elem and before seconed elem
-
{
-
p = (Nodep)malloc(sizeof(Node));
-
p->next = hnode->headp->next;
-
hnode->headp->next = p;
-
p->data = *(sqdata->elem+t);
-
(hnode->clenth)++;
-
t++;
-
}
-
}
-
-
-
void viewchain(Hnodep hnode)
-
{
-
int i=1;
-
Nodep p;
-
int max_len;
-
p = hnode->headp;
-
max_len = hnode->clenth;
-
-
printf("Max chain length is:%d\n",max_len);
-
do
-
{
-
printf("node:%d values is: %d\n",i,p->data);
-
i++;
-
-
}while(p=p->next);
-
}
-
-
void getelem(Hnodep hnode,int postion)
-
{
-
int i=0;
-
Nodep p;
-
if(postion > hnode->clenth || postion ==0 )
-
{
-
printf("postion large than lenth or poastion = 0\n");
-
exit(4);
-
}
-
p = hnode->headp;
-
-
while(i<postion-1)
-
{
-
i++;
-
p = p->next;
-
}
-
-
printf("node %d values is %d\n",i+1,p->data);
-
-
}
-
-
-
Nodep getelemp(const Hnodep hnode,int postion) //insert one elem after postion
-
{
-
int i=0;
-
Nodep p;
-
if(postion > hnode->clenth || postion ==0 )
-
{
-
printf("postion large than lenth or poastion = 0\n");
-
exit(5);
-
}
-
p = hnode->headp;
-
-
while(i<postion-1)
-
{
-
i++;
-
p = p->next;
-
}
-
-
return p;
-
-
}
-
-
void addnode(Nodep inode,int postion,Hnodep hnode)//insert one elem after postion
-
{
-
Nodep p;
-
p = getelemp(hnode,postion);
-
if(!p->next) //end elem?
-
{
-
p->next = inode;
-
inode->next = NULL;
-
}
-
else
-
{
-
inode->next = p->next;
-
p->next = inode;
-
}
-
hnode->clenth++;
-
}
-
-
void delnode(int postion,Hnodep hnode) //delete elem at give postion
-
{
-
Nodep p1; //delete postion-1 p
-
Nodep p2; //delete postion p
-
-
if(postion == 1)
-
{
-
p2 = hnode->headp->next;
-
hnode->headp->next = hnode->headp->next->next;
-
free(p2);
-
}
-
else
-
{
-
p1=getelemp(hnode,postion-1);//find before delete node postion
-
p2 = p1->next;
-
p1->next = p1->next->next;
-
free(p2);
-
}
-
hnode->clenth--;
-
}
-
main 函数:
-
#include<iostream>
-
#include<stdio.h>
-
#include<stdlib.h>
-
-
-
typedef unsigned int uint;
-
typedef int Etype;
-
typedef int datatype;
-
-
typedef struct node
-
{
-
-
datatype data;
-
struct node *next;
-
} Node,*Nodep;
-
-
typedef struct hnode
-
{
-
-
int clenth;
-
Nodep headp;
-
} Hnode,*Hnodep;
-
-
-
typedef struct sqlist
-
{
-
-
Etype* elem; //pointer of sqlist base address
-
uint length; //current length of elem
-
uint m_size; //
-
} SQLIST;
-
-
void initchain(Hnodep hnode,int n);
-
void initchain(Hnodep hnode,const SQLIST* sqdata);
-
void viewchain(Hnodep hnode);
-
void view(SQLIST* inlist);
-
void delsqldel(SQLIST* inlist,uint postion);
-
void initsqlist(SQLIST* inlist);
-
void initsqlinsert(SQLIST* inlist,Etype ielem,uint postion);
-
void getelem(Hnodep hnode,int postion);
-
void addnode(Nodep inode,int postion,const Hnodep hnode);
-
void delnode(int postion,Hnodep hnode);
-
-
-
int main(void)
-
{
-
SQLIST a;
-
Hnode chd1;
-
Nodep newnode = (Nodep)malloc(sizeof(Node));
-
newnode->data=50;
-
newnode->next=NULL;
-
initsqlist(&a);
-
printf("insert 5 values use seq mode\n");
-
initsqlinsert(&a,5,1);
-
initsqlinsert(&a,10,1);
-
initsqlinsert(&a,15,1);
-
initsqlinsert(&a,20,1);
-
initsqlinsert(&a,25,1);
-
view(&a);
-
printf("\n\ninit chain use seq arrary\n");
-
initchain(&chd1,&a);
-
viewchain(&chd1);
-
printf("\n\nview one node at postion 3\n");
-
getelem(&chd1,3);
-
printf("\n\nadd one node after node 2\n");
-
addnode(newnode,2,&chd1);
-
viewchain(&chd1);
-
printf("\n\nadd one node at postion 2\n");
-
delnode(2,&chd1);
-
viewchain(&chd1);
-
return 0;
-
}
编译:
g++ main.cpp chain.cpp sqlist.cpp -W
运行:
gaopeng@bogon:~/datas/part2/chain$ ./a.out
insert 5 values use seq mode
node:0 values is:25 data length:5 max_size:10
node:1 values is:20 data length:5 max_size:10
node:2 values is:15 data length:5 max_size:10
node:3 values is:10 data length:5 max_size:10
node:4 values is:5 data length:5 max_size:10
init chain use seq arrary
Max chain length is:5
node:1 values is: 25
node:2 values is: 5
node:3 values is: 10
node:4 values is: 15
node:5 values is: 20
view one node at postion 3
node 3 values is 10
add one node after node 2
Max chain length is:6
node:1 values is: 25
node:2 values is: 5
node:3 values is: 50
node:4 values is: 10
node:5 values is: 15
node:6 values is: 20
add one node at postion 2
Max chain length is:5
node:1 values is: 25
node:2 values is: 50
node:3 values is: 10
node:4 values is: 15
node:5 values is: 20
可以看到我初始化的链表为 25 5 10 15 20 查看第三个元素为 10 在位置2后面插入一个元素50 变为
了
25 5 50 10 15 20 删除位置2元素变为了 25 50 10 15 20
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/7728585/viewspace-2125007/
,如需转载,请注明出处,否则将追究法律责任。