资源简介
用C语言写的用于实现单链表的基本操作的源代码,都是经过测试可以运行的!定义单链表类型并动态创建单链表
1、实现线性表链式存储结构下元素的插入操作
3、实现线性表链式存储结构下元素的删除操作
4、实现线性表链式存储结构下取元素操作
代码片段和文件信息
#include
#include
typedef struct node
{
int nDate;
struct node *pstnext;
}Node;
//链表建立
Node* creat()
{
Node *head = NULL *p = NULL *s = NULL;
int Date = 0 cycle = 1;
head = (Node*)malloc(sizeof(Node));
if(head == NULL)
{
printf(“分配内存失败\r\n“);
return NULL;
}
head->pstnext = NULL;
p = head;
while(cycle)
{
scanf(“%d“ &Date);
if(Date!=0)
{
s = (Node*)malloc(sizeof(Node));
if(s==NULL)
{
printf(“分配内存失败\r\n“);
return NULL;
}
s->nDate = Date;
p->pstnext = s;
p = s;
}
else
{
cycle = 0;
}
}
p->pstnext = NULL;
return(head);
}
//单链表测长
void length(Node *head)
{
Node *p = head->pstnext;
int j=0;
while(NULL != p)
{
p = p->pstnext;
j++;
}
printf(“%d\r\n“ j);
}
//链表输出
void output(Node *head)
{
Node *p = head->pstnext;
while(p!=NULL)
{
printf(“%d “ p->nDate);
p = p->pstnext;
}
printf(“\r\n“);
}
//按序号查找
void research_Number(Node *head int Num)
{
Node *p=head;
int i = 0;
while(p!=NULL && i < Num)
{
p = p->pstnext;
i++;
}
if(p == NULL)
{
printf(“查找位置不合法\r\n“);
}else if(i == 0)
{
printf(“查找位置为头结点\r\n“);
}else if(i == Num)
{
printf(“第%d个位置数据为%d\r\n“ i p->nDate);
}
}
//在指定元素位置插入新结点
void insert_1(Node *head int i int Newdate)
{
Node *pre = head *New = NULL;
int j = 0;
while(NULL != pre && j < i-1)
{
pre = pre->pstnext;
j++;
}
if(NULL == pre || j > i-1)
{
printf(“插入位置不存在\r\n“);
}else
{
New = (Node*)malloc(sizeof(Node));
if(NULL == New)
{
printf(“分配内存失败\r\n“);
return;
}
New->nDate = Newdate;
New->pstnext = pre->pstnext;
pre->pstnext = New;
}
}
//删除指定结点
void Delete_1(Node *head int i3)
{
Node *p = head *pre = NULL;
int j
评论
共有 条评论