数据结构(一)---链表:
①线性表的本质:
线性表的定义:
线性表(List)是零个或多个数据元素的集合、线性表中的数据元素之间是有顺序的、线性表中的数据元素个数是有限的、线性表中的数据元素的类型必须相同
②线性表的相关操作:
线性表的一些常用操作:创建线性表、销毁线性表、清空线性表、将元素插入线性表、将元素从线性表中删除、获取线性表中某个位置的元素、获取线性表的长度
下面给出一个实例,创建链表,并遍历链表:
#include <stdlib.h>
#include <stdio.h>
struct node
{
int num;
struct node *next;
};
int Number;
struct node*creat(struct node *head, struct node*pNew, struct node*pEnd)
{
pNew->next = NULL;
int i = 1;
while (i<=Number)
{
if (head == NULL)
{
printf("NO%d\n",i);
head = pNew;
printf("pHead=%d\n", head);
pEnd = pNew;
printf("pEnd=%d\n", pEnd);
pEnd->next = pNew->next;
printf("pEnd->next=%d\n", pEnd->next);
printf("/**********************/\n");
}
else
{
printf("NO%d\n", i);
pNew = (struct node*)malloc(sizeof(struct node));
pNew->next = NULL;
pEnd->next = pNew;
printf("pNew=%d\n",pNew);
pEnd = pNew;
printf("pEnd=%d\n", pEnd);
pEnd->next = pNew->next;
printf("pEnd->next=%d\n", pEnd->next);
printf("/**********************/\n");
}
++i;
}
return head;
}
void print(struct node*head)
{
int i = 1;
struct node *temp;
temp = head;
while (temp != NULL)
{
printf("NO%d\n", i);
++i;
printf("temp=%d\n", temp);
printf("temp->next