单链表的创建和使用(数据结构)

这是单链表的简单形式,我没有尝试双链表,一些简单的功能例如插入删除显示。

#include<stdio.h>
#include<malloc.h>


typedef int Position;
typedef struct LNode * PtrToLNode;


struct LNode
{
	char alp;
	struct LNode *next;
}; 
struct LNode * head;
typedef PtrToLNode List;


void show(  );
List MakeEmpty();
Position Find(char alp);
bool Insert(int X, char alp);
bool Delete(int X);
void DispList();
int Length();




int main()
{
	int i, X, n, cnt, c=0;
	char alp, k;
	show();
	while(1){
      //显示菜单 
		scanf("%d", &n);
		if(n<-1||n>6)
		{
		printf("\n输入无效,请重新输入:");
		continue;
		}
		if(c==0&&(n>0&&n<7)&&n!=1)
		{
		printf("\n还未创建链表,请重新输入:");
		continue; 
		}
		
		switch(n)
		{	
		case 1:
			{
			MakeEmpty();  //建立新的链表
			printf("新的链表为:");
			struct LNode *p1=head;
			cnt = Length()-1;
			for( i=0; i<cnt; i++ )
		    {
			printf("%c", p1->alp);
			p1 = p1->next;
		    }
		    printf("\n");
			c=1;	
			}
        break;
        
		case 2:
			{
			printf("\n请输入你要插入的位置:");
			scanf("%d", &X);
			printf("请输入你要插入的字母:");
			scanf("\n%c", &alp);
			Insert(X, alp);
			}
		break;
		
		case 3:
			{
			printf("输入你要删除的位序号");
			scanf("%d", &X);
			Delete(X); //删除元素
			}
		break;


		case 4: //显示全部元素
			{
		    printf("新的链表为:");
			struct LNode *p2=head;
			cnt = Length()-1;
			for( i=0; i<cnt; i++ )
		    {
				printf("%c", p2->alp);
				p2=p2->next;
		    }
			}
		break;
		
		case 5:
			{
			printf("输入你要查找的字母");
			k = getchar();
			scanf("%c", &alp);
			if( i!=0 && i<=Length() )
			printf("你查找的字母在第%d个位置上!\n", Find(alp)+1);//查找元素函数
			else
			printf("抱歉,找不到该字母\n");
			}
		break;
		
		case 6:
		{
			printf("表长长度为%d\n", Length()-1 ); //求得链表长度 	
		}
		break;	         
		}
		show();
		}
	return 0;
}


int Length() //查找元素长度
{       
    Position cnt = 0;
	struct LNode *p=head;
	while(p)
	{
		//printf("*cnt = %d*%c*\n",cnt, p);
		p = p->next;
		cnt++;
	}
	return cnt;
}
List MakeEmpty()
{
		head = (List)malloc(sizeof(struct LNode));   //在这里申请大小为struct LNode 的结构空间 
		struct LNode *p=(struct LNode *)malloc(sizeof(struct LNode));
		struct LNode *t=(struct LNode *)malloc(sizeof(struct LNode));
		p = head;
		char alp, k;
		printf("请输入字母,输入0为结束按回车键:");//输入连续的字母串,以0为结束 
        k = getchar();
		while(true)
		{
			scanf("%c", &alp);
			if(alp=='0')
		    {
	    	p->next = NULL;
	    	break;
		    }
		p->alp = alp;
	    p->next = t;
	    p = t;
	    t = (struct LNode *)malloc(sizeof(struct LNode));
	    //printf("%c", p->alp);
		}	
}            


Position Find(char alp)//元素查找 
{
	struct LNode *t=head;
	Position i=0, cnt = Length();
	while(t->alp!=alp )
	{
		if( i>cnt )
		break;
	    t = t->next;
	    i++;    	
    }
    printf("%d", i);
	return i;
}


bool Insert(int X, char alp) //插入操作
{
	Position j;
	struct LNode *p=(struct LNode *)malloc(sizeof(struct LNode));
	p = head;
	struct LNode *m=(struct LNode *)malloc(sizeof(struct LNode));
/*  for( j=L->Last; j>=X-1; j-- )
	     p->alp[j+1] = p->alp[j];
	p->alp[i-1] = X;
	p->Last++;*/
	for( j = 0; j<=X-2; j++ )
	p = p->next;
	m->next = p->next;
	p->next = m;
	m->alp = alp;
}


bool Delete(int X) //删除操作
{
	Position j, k;
	struct LNode *u=(struct LNode *)malloc(sizeof(struct LNode));
	struct LNode *o=(struct LNode *)malloc(sizeof(struct LNode));
	u = head;
	/*for( j=i; j<=L->Last; j++ )
	     L->Date[j-1] = L->Date[j];
	L->Last--;*/
	if( X>=2 )
	{
	for( j=0; j<X-2; j++ )
	u = u->next;
	o = u->next;
	u->next = o->next;
	free(o);
	k=1;
	}
	else
	head = u->next;
	if( k=1 )
	printf("删除成功!");
	else
	printf("删除失败!请重试。");
}   


void show()
{
	printf("\n\n");
	printf("                    数字查询系统          \n");
	printf("            ******************************\n");
	printf("            *       1------建   表       *\n");
	printf("            *       2------插   入       *\n");
	printf("            *       3------删   除       *\n");
	printf("            *       4------显   示       *\n");
	printf("            *       5------查   找       *\n");
	printf("            *       6------表   长       *\n");
	printf("            *       0------退   出       *\n");
	printf("            ******************************\n\n");
	printf("请选择你需要的操作(0-6): ");
}

编译器:DEV C++





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值