本文主要是介绍带头节点的单链表练习,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
#include <stdio.h>
#include <stdlib.h>
typedef int ElemType;
typedef struct
{ElemType data;struct LNode *next;
}LNode, *LinkList;void initLink(LinkList L);//初始化头节点
LinkList getLinkHead();//返回一个头节点
void createLink(LinkList L, ElemType *arr, int n);//用长度为n的数组创建链表
void showList(LinkList L, int n);//输出指定长度链表
void showList2(LinkList L);//输出指定节点之后的所有节点//在elem后插入enew,若elem不存在,则在末尾插入
void insertByData(LinkList L, ElemType elem, ElemType enew);
void insertByData2(LinkList L, ElemType elem, ElemType enew);
void insertByData3(LinkList L, ElemType elem, ElemType enew);//倒数第n个元素之前插入enew,若L长度小于n,则插入开头
LNode *insertByIndexEnd(LinkList L, int n, ElemType enew);//将值为负数的结点从L表中删除,并按原次序链接成一个新的单链表NeL
void SeparationNegative(LinkList L, LinkList NeL);//将值为负数的结点删除
void deleteNegative(LinkList L);int main()
{ElemType data[] = { -1, -2, 3, 4, -5, 6, 7, -8, 10 };int length = sizeof(data) / sizeof(ElemType);LinkList L = (LinkList)malloc(sizeof(LinkList)),NeL;initLink(L);createLink(L, data, length);printf("原链表:\t\t\t");showList(L, length);printf("\n");/*printf("\n将值为负数的结点删除:\n");deleteNegative(L);printf("删除后链表:\t\t\t"); showList2(L);*/printf("\n分离链表中的负数:\n");NeL = getLinkHead();SeparationNegative(L, NeL);printf("原链表:\t\t\t"); showList2(L);printf("新链表:\t\t\t"); showList2(NeL);printf("\n");printf("4后插入5:\t\t\t");insertByData3(L, 4, 5);showList2(L);printf("8后插入11:\t\t\t");insertByData3(L, 8, 11);showList2(L);printf("倒数第2个元素之前插入了8:\t");LNode *node4e = insertByIndexEnd(L, 2, 8);showList2(L);printf("\n");system("PAUSE");return 0;
}void deleteNegative(LinkList L)
{LNode *r = L, *p = L->next;while (p){if (p->data < 0){r->next = p->next;free(p);p = r->next;}else{p = p->next;r = r->next;}}
}void SeparationNegative(LinkList L, LinkList NeL)
{LNode *p = L->next, *r = L, *q = NeL;while (p){if (p->data < 0){r->next = p->next;p->next = q->next;q->next = p;p = r->next;q = q->next;}else{r = r->next;p = p->next;}}
}LinkList getLinkHead()
{LinkList L = (LinkList)malloc(sizeof(LinkList));L->data = 0;L->next = NULL;return L;
}void initLink(LinkList L)
{L->data = 0;L->next = NULL;
}void createLink(LinkList L, ElemType *arr, int n)
{LNode *node = NULL;LNode *r = L;int i;for (i = 0; i < n; i++){node = (LNode *)malloc(sizeof(LNode));node->data = arr[i];node->next = r->next;r->next = node;r = r->next;}
}void showList(LinkList L, int n)
{int i;LNode *r = L->next;for (i = 0; i < n; i++){printf("%d ", r->data);r = r->next;}
}void showList2(LinkList L)
{int i;LNode *r = L->next;while (r->next != NULL){printf("%d ", r->data);r = r->next;}printf("%d ", r->data);printf("\n");
}//在elem后插入enew
void insertByData(LinkList L, ElemType elem, ElemType enew)
{int flag = 0;LNode *r = L->next;LNode *p = r;while (r){if (r->data == elem){LNode *node = (LinkList)malloc(sizeof(LNode));node->data = enew;node->next = r->next;r->next = node;flag = 1;break;}else{p = r;//保存最后一个节点的地址r = r->next;}}if (flag == 0){LNode *node = (LinkList)malloc(sizeof(LNode));node->data = enew;node->next = p->next;p->next = node;}
}//在elem后插入enew
void insertByData2(LinkList L, ElemType elem, ElemType enew)
{LNode *r;for (r = L->next; r->next != NULL; r = r->next){if (r->data == elem){LNode *node = (LinkList)malloc(sizeof(LNode));node->data = enew;node->next = r->next;r->next = node;return;}}LNode *node = (LinkList)malloc(sizeof(LNode));node->data = enew;node->next = r->next;r->next = node;
}//在elem后插入enew
void insertByData3(LinkList L, ElemType elem, ElemType enew)
{LNode *r = L->next;while (r){if (r->data == elem || r->next == NULL){LNode *node = (LinkList)malloc(sizeof(LNode));node->data = enew;node->next = r->next;r->next = node;break;}else{r = r->next;}}
}LNode *insertByIndexEnd(LinkList L, int n, ElemType enew)
{LNode *p = L;LNode *s = L;n++;while (p && n--){p = p->next;}while (p){s = s->next;p = p->next;}LNode *node = (LinkList)malloc(sizeof(LNode));node->data = enew;node->next = s->next;s->next = node;return node;
}
这篇关于带头节点的单链表练习的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!