本文主要是介绍无头节点的单循环链表中删除指针s所指结点的前驱结点,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
#include <stdio.h>
typedef int ElemType;
typedef struct
{ElemType data;struct LNode *next;
}LNode, *CLinkList;void initLink(CLinkList L);//初始化头节点
void createLink(CLinkList L, ElemType *arr, int n);//用长度为n的数组创建链表
void showList(CLinkList L);//输出指定节点之后的所有节点//返回L后第n个节点(n可超过L的总节点个数)
LNode *creLNodePoint(CLinkList L, int n);
//删除s的前驱节点
void delPrior(LNode *s);int main()
{ElemType data[6] = {1, 2, 3, 8, 4, 5 };int length = sizeof(data) / sizeof(ElemType);CLinkList L = (CLinkList)malloc(sizeof(CLinkList));LNode *s;initLink(L);createLink(L, data, length);printf("原链表:\t\t");showList(L);s = creLNodePoint(L,4);//制作指向循环链表某节点的指针delPrior(s);//删除s的前驱节点printf("s指向的数据为%d\n",*s);printf("删除其前驱节点:\t");showList(L);getchar();return 0;
}void initLink(CLinkList L)
{L->data = 0;L->next = L;
}void createLink(CLinkList L, ElemType *arr, int n)
{LNode *node = NULL;LNode *r = L;int i;r->data = arr[0];for (i = 1; i < n; i++){node = (LNode *)malloc(sizeof(LNode));node->data = arr[i];node->next = r->next;r->next = node;r = r->next;}
}void showList(CLinkList L)
{int i;LNode *r = L;while (r->next != L){printf("%d ", r->data);r = r->next;}printf("%d ", r->data);printf("\n");
}//返回L后第n个节点(n可超过L的总节点个数)
LNode *creLNodePoint(CLinkList L, int n)
{LNode *s = L;while (n--){s = s->next;}return s;
}//删除s的前驱节点
void delPrior(LNode *s)
{LNode *p = s;LNode *r = p;while (p->next != s){r = p;p = p->next;}r->next = s;free(p);
}
这篇关于无头节点的单循环链表中删除指针s所指结点的前驱结点的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!