/**
* 1、链表是一个结构体
* 2、链表包含数据和地址两部分
* 3、单链表每个结点由该结点真实数据和指向下一个结点的指针组成,对于头结点,没有真实数据,对于为节点,地址指针为NULL
*/
#include <stdio.h>
#include <stdlib.h>
struct LinkList
{
int score;
struct LinkList *next;
};
typedef struct LinkList List;
/**
* 新加的结点在头部
*/
List *createListInHead(void)
{
int j = 50;
List *head;
List *tamp;
// 先把链表置空
head = NULL;
while (j < 53)
{
tamp = (List*) malloc(sizeof(List));
tamp->score = j;
tamp->next = head;
head = tamp;
j++;
}
return head;
}
/**
* 新加的结点在尾部
*/
List *createListInEnd(void)
{
int i = 90;
int listSize = 0;
List *head;// 链表
//临时变量,代表下一个结点的指针域
List *pNextNode;
List *newNode;// 新的结点
newNode = pNextNode = (List*)malloc(sizeof(List));
newNode->score = i;
head = NULL;// 此时链表是为空的
while (i < 93)
{
if(listSize == 0)
{
head = newNode;
}else
{
pNextNode->next = newNode;
}
pNextNode = newNode;
newNode = (List*)malloc(sizeof(List));
newNode->score = ++i;
listSize++;
}
// 尾结点的指针域要记得赋值为NULL
pNextNode->next = NULL;
return head;
}
/**
* 删除结点
*/
List *delListNode(int score,List *list)
{
List *newList;
List *p;
newList = NULL;
p = list;
if(p->score == score)
{// 要删的结点为头部结点
printf("----------------删除了头结点-------------------\n");
newList = p->next;
return newList;
}
while(p != NULL)
{
if(p->next != NULL && p->next->score == score)
{
printf("----------------删除了结点-------------------\n");
p->next = p->next->next;
return list;
}
p = p->next;
}
printf("----------------没有找到要删除的结点-------------------\n");
return list;
}
List *insertListNode(List *list,List *insertNode)
{
List *p;
List *tamp ;
tamp = list;
if(tamp == NULL)
{
tamp = insertNode;
tamp->next = NULL;
return list;
}
if(tamp->score == insertNode->score)
{
p = tamp->next;
tamp->next = insertNode;
insertNode->next = p;
return list;
}
while(tamp != NULL)
{
if(tamp->next != NULL && tamp->next->score == insertNode->score)
{
//printf("0000-------------------%d\n",tamp->score);
p = tamp->next;
tamp->next = insertNode;
insertNode->next = p;
break;
}else{
if(tamp->next == NULL)
{
//printf("3333-------------------%d\n",tamp->score);
tamp->next = insertNode;
insertNode->next = NULL;
break;
}
}
tamp = tamp->next;
}
return list;
}
void printList(List *list)
{
if(list == NULL)
{
printf("This is empty List!!! \n");
return ;
}
List *tamp;
tamp = list; // 这里tamp指向的是list的第一个数据
do
{
printf("printList score = %d \n", tamp->score);
tamp = tamp->next;
} while (tamp != NULL);
}
int main(int argc, char **argv)
{
printf("****************新加的元素加在链表头部******************\n");
List *listInHead;
listInHead = NULL;
listInHead = createListInHead();
printList(listInHead);
printf("****************新加的元素加在链表尾部******************\n");
List *listInEnd;
listInEnd = NULL;
listInEnd = createListInEnd();
printList(listInEnd);
// printf("****************删除链表中的结点******************\n");
// List *newList;
// newList = NULL;
// newList = delListNode(99,listInEnd);
// printList(newList);
printf("****************insert node into list******************\n");
List *insertList,*newNode;
insertList = NULL;
newNode = (List *)malloc(sizeof(List));
newNode->score = 95;
insertList = insertListNode(listInEnd,newNode);
printList(insertList);
return 0;
}
单链表之C实现
最新推荐文章于 2024-08-11 09:14:53 发布