线性表的操作(C语言)

利作链表的插入运算建立线性链表,然后利用链表的查找、删除、计数、输出等运算反复实现链表的这些操作(插入、删除、查找、计数、输出单独写成函数的形式),并能在屏幕上输出操作前后的结果。
//---------------------------------------------------------------------------

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define STY "%d"/*元素类型格式符*/
typedef int eltp;/*元素类型*/

typedef struct node{
eltp data;
struct node *next;
} node;

void init(void)
{
static int fg=1;
if (fg) {
srand(time(NULL));
fg=0;
}
}
node *insert(node *h,eltp d)
{
node *s=(node *)malloc(sizeof(node));
if (!h) {
s->data=d;
s->next=NULL;
h=s;
}
else {
h->next=insert(h->next,d);
}
return h;
}
node *create(int n)
{
node *h=NULL;
int i;
for (i = 0; i<n; i++) {
h=insert(h,rand()%100);
}
if (h) {
printf("线性表生成已完成!\n");
}
else {
fprintf(stderr,"线性表生成未成功\n");
exit(-1);
}
return h;
}
node *del(node *h,eltp d)
{
node *p;
if (h&&h->data==d) {
p=h;
h=h->next;
free(p);
}
else if (h) h->next=del(h->next,d);

return h;
}
int search(node *h,eltp d)
{
int i=1;
while (h&&h->data!=d)
{
h=h->next;
i++;
}
if (!h) i=-1;
return i;
}

int count(node *h)
{
int i=0;
for (i = 0; h; i++) {
h=h->next;
}
return i;
}

void prt(node *h)
{
while (h)
{
printf(STY"\t",h->data);
h=h->next;
}
putchar('\n');
}
void Free(node **h)
{
if (*h) {
Free(&(*h)->next);
free(*h);
*h=NULL;
}
}
int menu(void)
{
int i;
puts("******************");
puts("1.生成物册线性表");
puts("2.输出表元素");
puts("3.删除表元素");
puts("4.查找表元素");
puts("5.统计表元素罩则宏");
puts("6.插入表元素");
puts("7.删除线性表");
puts("0.退盯正出本程序");
puts("******************");
printf("请选择:");
scanf("%d",&i);
return i;
}
void find(node *h)
{
eltp a;
//node *t=NULL;
int index;
printf("请输入要查找的数字:");
scanf(STY,&a);
index=search(h,a);
if (index!=-1) {
printf(STY"是表中的第%d个元素\n",a,index);
}
else printf(STY"不是表中的元素\n",a);
}
node *insert_node(node *h,int index,eltp a)
{
node *hd=h,*in=(node *)malloc(sizeof(node));
int i;
in->data=a;
if (index>1) {
for (i=1; h->next&&i<index-1; i++) {
h=h->next;
}
in->next=h->next;
h->next=in;
}
else {
in->next=hd;
hd=in;
}
return hd;
}
node *remove_node(node *h)
{
eltp a;
printf("请输入要删除的元素:");
scanf(STY,&a);
h=del(h,a);
puts("已完成");
return h;
}
node *ins(node *h)
{
eltp a;
int i;
printf("请输入要插入的元素:");
scanf(STY,&a);
printf("请输入要插入的位置:");
scanf("%d",&i);
return insert_node(h,i,a);
}
int main(void)
{
node *head=NULL;
int ch;
init();
do
{
ch=menu();
switch (ch) {

default:printf("输入有误,重新输入\n");break;
case 0:break;
case 1:if(head) Free(&head);
head=create(10);
break;
case 2:prt(head);break;
case 3:head=remove_node(head);break;
case 4:find(head);break;
case 5:printf("表中共有%d个元素\n",count(head));break;
case 6:head=ins(head);break;
case 7:Free(&head);break;

}
}while (ch);
Free(&head);
return 0;
}
//---------------------------------------------------------------------------
这是我以前学数据结构时写的代码,可以实现你要的功能:
typedef int ElemType;

#include<iostream>
using namespace std;

//定义错误的枚举类型

enum Error_code{ok,error};

//定义节点
struct DuNode{
ElemType data;
DuNode *prior;
DuNode *next;
};

//定义链表类
class DuLinkList{
protected:
DuNode *first;//头指针
int count;//计数器桥橘,计数元素的个数
public:
DuLinkList(int c=0);//初始化链表,默认值为0
bool empty()const;
Error_code CreateList();//创建链表
Error_code GetElem(int i,ElemType &e);//取链表中第i个元素
Error_code Insert(int i,ElemType &e);//在第i个位置插入元素
Error_code ElemDelete(int i,ElemType &e);//删除第i个位置的元素
Error_code Show();
void Clear();/孙消激/清空链表
};

DuLinkList::DuLinkList(int c)
{
count=c;
first=NULL;
}

bool DuLinkList::empty()const
{
if(count==0)
return true;
else return false;
}

Error_code DuLinkList::CreateList()//逆序 双向 循环 链表(不带头节点)
{
ElemType e;
DuNode *p,*front,//指向第一个节点
*rear;//指向最后一个节点
for(int i=0;i<count;i++)
{
p=new DuNode;
if(!p)
{ cout<<"申请失败"; return error; }
else{
cout<<"请输入数据"<<endl;
cin>>e;
p->data =e;
if(first==NULL)
{
first=rear=front=p->next=p->prior=p;
}
else
{
p->next=front;
p->prior=rear;
front->prior=p;
rear->next=p;
front=p;
first=p;
}
}
}
return ok;
}

Error_code DuLinkList::Show()
{
DuNode *p;
p=first;
if(first==NULL)
return error;
else
{
for(int i=0;i<count;i++)
{
cout<<p->data<<'\t'则袜;
p=p->next;
}
}
cout<<endl;
return ok;
}

void main() //测试
{
DuLinkList l1(10);
l1.CreateList();
l1.Show();

}