单链表的基本操作 元素为数字 要C不要c++

来源:百度知道 编辑:UC知道 时间:2024/05/17 21:02:54
1.输入一组整型元素序列,建立顺序表
2.实现该顺序表的遍历
3.在该顺序表中进行顺序查找某一元素,按位置查找和按元素查找
4.在该顺序表中间位置插入某一元素,并输出
5.在该顺序表中间删除某一元素
6.编写一个主函数,调试上述算法

#include<stdio.h>
#include<malloc.h>
struct node
{
int key;
node *next;
};

node * Creat()
{
node *head = NULL;
node *temp = NULL;
int i;
printf("please input the data:\n");
scanf("%d",&i);
while(i!=0)
{
temp = (node *)malloc(sizeof(struct node));
temp->key = i;
temp->next = head;
head = temp;
printf("please input the data:\n");
scanf("%d",&i);
}
return head;
}
void Printlist(node * head)
{
node * p = head;
while(p!=NULL)
{
printf("%d ",p->key);
p = p->next;
}
printf("\n");
}
int Searchindex(node *head,int i)
{
node * p = head;
int count = 0;
while(p!=NULL&&++count!=i)
{
p = p->next;
}
if(p==NULL)
return 0;//表示没有找到位置i的