数据结构 C 代码3、4:双向链表和静态链表

一.双向链表

每个节点有两个链接:一个指向前一个节点,当此节点为第一个节点时,指向空值;而另一个指向下一个节点,当此节点为最后一个节点时,指向空值。

请添加图片描述

1.定义结构体

 typedef struct DoubleLinkedNode{
    char data;
    struct DoubleLinkedNode *previous;
    struct DoubleLinkedNode *next;
}DLNode,*DLNodePtr;

2.进行初始化

 DLNodePtr initLinkList(){
    DLNodePtr tempHeader=(DLNodePtr)malloc(sizeof(struct DoubleLinkedNode));
    tempHeader->data='\0';
    tempHeader->previous=NULL;
    tempHeader->next=NULL;
    return tempHeader;
}

3.打印链表

 void printList(DLNodePtr paraHeader){
    DLNodePtr p=paraHeader->next;
    while(p!=NULL){
        printf("%c",p->data);
        p=p->next;
    }
    printf("\r\n");
}

4.指定位置插入元素

 void insertElement(DLNodePtr paraHeader,char paraChar,int paraPosition){
    DLNodePtr p,q,r;
    
    p=paraHeader;
    for(int i=0;i<paraPosition;i++){
        p=p->next;
        if(p==NULL){
            printf("The position %d is beyond the scope of the list.\r\n",paraPosition);
            return;
        }
    }
    
    q=(DLNodePtr)malloc(sizeof(struct DoubleLinkedNode));
    q->data=paraChar;
    
    r=p->next;
    q->next=p->next;
    q->previous=p;
    p->next=q;
    if(r!=NULL){
        r->previous=q;
    }
}

图示步骤即为:
请添加图片描述
请添加图片描述
请添加图片描述
最终实现为:
请添加图片描述

5.删除指定的元素

 void deleteElement(DLNodePtr paraHeader,char paraChar){
    DLNodePtr p,q,r;
    p=paraHeader;
 
    while((p->next!=NULL)&&(p->next->data!=paraChar)){
        p=p->next;
    }
    
    if(p->next==NULL){
        printf("The char '%c' does not exist.\r\n",paraChar);
        return;
    }
 
    q=p->next;
    r=q->next;
    p->next=r;
    if(r!=NULL){
        r->previous=p;
    }
 
    free(q);
}

目标为删除数据元素为ai的结点:
请添加图片描述
最终实现为:
请添加图片描述

6.打印出对应的地址

 void basicAddressTest(){
    DLNode tempNode1,tempNode2;
    
    tempNode1.data=4;
    tempNode1.next=NULL;
 
    tempNode2.data=6;
    tempNode2.next=NULL; 
 
    printf("The first node :%d,%d,%d\r\n",&tempNode1,&tempNode1.data,&tempNode1.next);
    printf("The second node :%d,%d,%d\r\n",&tempNode2,&tempNode2.data,&tempNode2.next);
 
    tempNode1.next=&tempNode2;
}

7.查找元素

int locateElement(DLNodePtr para Header,char paraValue){
    DLNodePtr p = paraHeader;
    for(int i=0;;i++){
    if(p->data==paraValue){
            retuen i;
        }
        p = p->next;
    }
    return 1;
}

8.测试代码

void insertDeleteTest()
{
	DLNodePtr tempList = doubleLinkedListInit();
	insertElementByPosition(tempList, 'H', 0);
	insertElementByPosition(tempList, 'e', 1);
	insertElementByPosition(tempList, 'l', 2);
	insertElementByPosition(tempList, 'l', 3);
	insertElementByPosition(tempList, 'o', 4);
	insertElementByPosition(tempList, '!', 5);
	printList(tempList);
	deleteElement(tempList, 'e');
	deleteElement(tempList, 'a');
	deleteElement(tempList, 'o');
	printList(tempList);

	insertElementByPosition(tempList, 'x', 1);
	printList(tempList);
	deleteElementByPosition(tempList, 0); //边界测试 
	printList(tempList);
	deleteElementByPosition(tempList, 1); //正常测试 
	printList(tempList);
	deleteElementByPosition(tempList, 2); //边界测试 
	printList(tempList);

}

9.运行结果

Hello!
The char 'a' does not exist.
Hll!
Holl!
The first node: 1703632, 1703632, 1703640
The second node: 1703620, 1703620, 1703628

**

二.静态链表

**
用数组代替指针或引用来描述单链表,即用数组描述的链表叫做静态链表,这种描述方法叫做游标实现法。

结构:
请添加图片描述
静态链表需要对数组的第一个和最后一个元素作为特殊元素处理,不存数据。通常把未被使用的数组元素称为备用链表。而数组第一个元素,即下标为0的元素的cur就存放备用链表的第一个结点的下标;而数组的最后一个元素的cur则存放第一个有数值的元素的下标,相当于单链表中的头结点作用,当整个链表为空时,则为0。

1.静态链表存储结构及定义

typedef struct StaticLinkedNode {
	char data;
	int next;
}*NodePtr;
typedef struct StaticLinkedList {
	NodePtr nodes;
	int* used;
} *ListPtr;

2.初始化

ListPtr initLinkedList()
{
	//分配空间
	ListPtr tempPtr = (ListPtr)malloc(sizeof(StaticLinkedList));
	tempPtr->nodes = (NodePtr)malloc(sizeof(struct StaticLinkedNode) * DEFAULT_SIZE);
	tempPtr->used = (int*)malloc(sizeof(int) * DEFAULT_SIZE);
	//创建头节点
	tempPtr->nodes[0].data = '\0';
	tempPtr->nodes[0].next = -1;
	//记录空间使用情况
	tempPtr->used[0] = 1;
	for (int i = 1; i < DEFAULT_SIZE; i++) {
		tempPtr->used[i] = 0;
	}
	return tempPtr;
}

3.插入元素

void insertElement(ListPtr paraListPtr, char paraChar, int paraPosition)
{
	int p, q, i;
	//1.查找位置
	p = 0;
	for (i = 0; i < paraPosition; i++)
	{
		p = paraListPtr->nodes[p].next;
		if (p == -1)
		{
			printf("所给位置超出范围\r\n");
			return;
		}
		
	}
	//创建创建新结点
	for (i = 1; i < DEFAULT_SIZE; i++) {
		if (paraListPtr->used[i] == 0) {
			// This is identical to malloc.
			printf("在第 %d 个位置空余.\r\n", i);
			paraListPtr->used[i] = 1;
			q = i;
			break;
		}
	}
	if (i == DEFAULT_SIZE)
	{
		printf("没有空间了\n\r");
		return;
	}
	paraListPtr->nodes[q].data = paraChar;
	//连接结点
	printf("连接中\r\n");
	paraListPtr->nodes[q].next = paraListPtr->nodes[p].next;
	paraListPtr->nodes[p].next = q;
}

请添加图片描述

4.删除指定的元素

void deleteElement(ListPtr paraListPtr, char paraChar)
{
	int p, q;
	p = 0;
	while ((paraListPtr->nodes[p].next != -1) && (paraListPtr->nodes[paraListPtr->nodes[p].next].data != paraChar)) {
		p = paraListPtr->nodes[p].next;
	}
	if (paraListPtr->nodes[p].next == -1) {
		printf("没有要删除的元素\r\n", paraChar);
		return;
	}
	q = paraListPtr->nodes[p].next;
	paraListPtr->nodes[p].next = paraListPtr->nodes[paraListPtr->nodes[p].next].next;
	paraListPtr->used[q] = 0;
}

请添加图片描述

5.打印链表

void printList(ListPtr paraListPtr)
{
	int p = 0;
	while (p != -1)
	{
		printf("%c", paraListPtr->nodes[p].data);
		p = paraListPtr->nodes[p].next;
	}
	printf("\r\n");
}

6.代码测试

void appendInsertDeleteTest() {
	// Step 1. 初始化
	ListPtr tempList = initLinkedList();
	printList(tempList);
 
	// Step 2. 添加元素
	insertElement(tempList, 'H', 0);
	insertElement(tempList, 'e', 1);
	insertElement(tempList, 'l', 2);
	insertElement(tempList, 'l', 3);
	insertElement(tempList, 'o', 4);
	printList(tempList);
 
	// Step 3. 删除元素
	printf("Deleting 'e'.\r\n");
	deleteElement(tempList, 'e');
	printf("Deleting 'a'.\r\n");
	deleteElement(tempList, 'a');
	printf("Deleting 'o'.\r\n");
	deleteElement(tempList, 'o');
	printList(tempList);
 
	insertElement(tempList, 'x', 1);
	printList(tempList);
}

7.运行结果

Space at 1 allocated.
linking
Space at 2 allocated.
linking
Space at 3 allocated.
linking
Space at 4 allocated.
linking
No space.
 Hell
Deleting 'e'.
Deleting 'a'.
Cannot delete a
Deleting 'o'.
Cannot delete o
 Hll
Space at 2 allocated.
linking
 Hxll
Press any key to continue

8.自己的一点理解

/*待处理的数据类型*/
typedef int Elemtype;

/*定义处理的静态链表最大值*/
#define MAX_SIZE 20

/*函数返回状态*/
typedef enum{
    success,
    fail,
}Status;

/*静态链表结构*/
typedef struct Stack{
    Elemtype data;
    int cur;
}StackLink,*PtrStackLink;


Status LinkInit(PtrStackLink space)
{
    int i;
    Status outcome = fail;
    if(space)
    {
        for(i = 0; i < MAX_SIZE-1; i++)
        {
            space[i].cur = i+1;
        }
        space[MAX_SIZE-1].cur = 1;/*最后一个元素不存数据*/
        outcome = success;
    }
    return outcome;
}

int LengthList(PtrStackLink space)
{
    int i,j = 0;
    i = space[MAX_SIZE -1].cur;
    while (i)
    {
        i = space[i].cur;
        j++;
    }
    return j;
}

int Malloc_Sll(PtrStackLink space)
{
    int i = space[0].cur;
    if(space[0].cur)
    {
        space[0].cur = space[i].cur;/*第一个没有数据的游标改为新的最后一个没有数据的下标*/
    }
    return i;/*待插入位置的下标*/
}

Status ListInsert(PtrStackLink space, int i, Elemtype e)
{
    Status _outcome_ = fail;
    int j,k,l;
    k = MAX_SIZE-1;/*最后一个元素*/
    j = Malloc_Sll(space);/*待插入位置下标*/
    if(i < 1 || i > LengthList(L))/*插入位置是否合法以及是否超出范围*/
    {
        space[j].data = e;
        for (l = 1; l <= i-1; l++)
        {
            k = space[k].cur;/*最后一个的游标即第一个元素的下标1*/
        }
        space[j].cur = space[k].cur;
        space[k].cur = j;
        space[0].cur = space[j].cur;/*第一个位置的元素的游标改为插入元素的下标*/
        _outcome_ = success;
    }
    return _outcome_;
}

Status ListDelete(PtrStackLink space, int i)
{
    Status _outcome_ = success;
    int j,k,l;
    k = MAX_SIZE - 1;/*最后一个元素的下标*/
    if(i < 1 || i > LengthList(space))
    {
        success = fail;
    }
    for(j = 1; j <= i-1;j++)
    {
        k = space[k].cur;/*待删除的元素的下标*/
    }
    j = space[k].cur;
    space[k].cur = space[j].cur;/*删除第i个元素*/

    space[k].cur = space[0].cur;/*回收为备用链表*/
    space[0].cur = k;
}

9.总结

进行插入和删除操作时无需移动后续元素,优化了顺序表的插入及删除操作。
但没有解决连续存储分配带来的表长难以确定的问题。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值