C课程设计---单链表基本操作实现

Points:

  1. 循环菜单程序,main中利用while(1)设置死循环
  2. 初始化一个空链表
  3. 创建链表
  4. 首插法增加新结点
  5. 删除结点
  6. 输出链表长度
  7. 输出链表元素
  8. 按结点数据域排序
  9. 查找结点
  10. 销毁链表
//SingleLinkedList.c
#include <stdio.h>
#include <stdlib.h>  //调用malloc()、free()、exit()等函数需要包含此头文件
typedef struct node{  //定义新的数据类型
	char data;  //结点的数据域
	struct node *next;  //结点的指针域
}LNode;  //结点数据类型的名称

//1
void InitList(LNode **head){  //初始化一个空链表,不包含头结点
	(*head)=NULL;
}

void InitList2(LNode **head){  //初始化一个空链表,包含头结点
	(*head)=(LNode*)malloc(sizeof(LNode));
	if((*head)==NULL)   //申请空间的失败判断及处理
		;
	(*head)->next=NULL;
}

//2
void CreateList(LNode**h){  //创建链表,不含头结点
	LNode *p;
	char e;
	printf("Input node data(end with '#'):");
	e=getchar();  //链表各结点的数据来源于键盘输入的多个字符
	while(e!='#'){
		p=(LNode*)malloc(sizeof(LNode));  //申请一个结点的存储空间
		if(p==NULL)  //申请存储空间失败的判断和处理
			;
		p->data=e;  // 为新结点的数据域赋值
		p->next=(*h);  //以首插法将新结点插入链表
		(*h)=p;
		e=getchar();
	}
}

void CreateList2(LNode *h){  //创建链表,包含头结点

}

//3
void InsertNode(LNode* *h,int i,char e){   // 在链表h 中的i位置插入新结点,数据域为e
	LNode *p,*q;
	p=(*h);
	q=(LNode*)malloc(sizeof(LNode));   //申请新结点的存储空间
	if(q==NULL)  //申请空间的失败判断及处理
		;
	q->data=e;   //为新结点的数据域赋值
	if(i==1){  //如果当前链表为空,或者要在1位置插入新结点
		q->next=(*h);
		(*h)=q;
		return ;  //函数完成功能并返回,防止进行后续操作
	}
	for(i=i-2;i;i--)  //链表非空且插入位置不为1时,寻找要插入位置的前趋结点
		p=p->next;
	q->next=p->next;   //新结点的插入操作
	p->next=q;
}

//4
void DeleteNode(LNode* *h,int i){   // 在链表h 的i位置删除该结点
	LNode *p=(*h),*q;
	if(i==1){  //如果要删除的节点位置为1
		(*h)=p->next;  //保持后续链的完整
		free(p);   //删除1节点
		return ;  //函数完成功能并返回,防止进行后续操作
	}
	for(i=i-2;i;i--)   //要删除的结点位置不为1,寻找要删除结点的前趋结点
		p=p->next;
	q=p->next;
	p->next=q->next;   //保持后续链的完整
	free(q);    // 删除该节点
}

//5
int ListLength(LNode *h){   //求链表中的结点数量,并返回该整数值
	int count=0;
	LNode *p=h;
	while(p!=NULL){
		count++;   //计数器自加一
		p=p->next;
	}
	return count;  //返回链表中的结点数量
} 

//6
void DispList(LNode *h){  //依次输出链表所有结点的数据域
	LNode *p=h;
	while(p!=NULL){
		printf("%c ",p->data);
		p=p->next;
	}
	printf("\n");
}

//7  排序
void InsertionSortList(LNode* *h){
	LNode *s,*tmp,*pre,*p;
	s=(LNode*)malloc(sizeof(LNode));
	if(s==NULL)
		;
	s->next=(*h);
	p=(*h)->next;
	s->next->next=NULL;
	while(p!=NULL){
		pre=s;
		tmp=s->next;
		while((tmp!=NULL)&&(tmp->data)<(p->data)){
			tmp=tmp->next;
			pre=pre->next;
		}
		(*h)=p->next;
		p->next=tmp;
		pre->next=p;
		p=(*h);
	}
	(*h)=s->next;
	free(s);
	printf("over\n");
}
//8
void Search(LNode *h,int n){
    LNode *s=h;
    int count=1;
    while(count!=n){
        count++;
        s=s->next;
    }
    printf("%c\n",s->data);//输出结点数据
}
//0
void DestroyList(LNode* *h){  //销毁链表
	LNode *p=(*h);
	while(p!=NULL){
		p=p->next;  //p指向下一个待销毁的结点
		free(*h);  //销毁当前结点
		(*h)=p;   //
	}
	//(*h)=NULL; 此句可省
}

void Menu(){  //主程序菜单函数
	printf("   --- Single Linked List ---\n");
	printf("1.Init list    2.Creat list\n");
	printf("3.Insert node  4.Delete node\n");
	printf("5.Length       6.Display list\n");
	printf("7.Sort list    8.Search\n");
	printf("0.Destroy list & exit\n");
	printf("Input your choice:");
}

int main(){
	LNode *head=NULL;
	int i,choice;
	char e,t,n;
	while(1){  //程序主体循环
		Menu();		
		scanf("%d",&choice);  //获取用户的选择
		getchar();    //-->吃掉回车
		switch(choice){  //根据用户的选择,提供服务
			case 1:
				InitList(&head);
				break;
			case 2:
				CreateList(&head);
				break;
			case 3://InsertNode
				printf("Input Location(1~%d):",ListLength(head)+1);
				scanf("%d",&i);
				if(i>(ListLength(head)+1))
					;
				printf("Input data:");
				t=getchar();
				scanf("%c",&e);
				InsertNode(&head,i,e);
				break;
			case 4://DeleteNode
				printf("Input Location(1~%d):",ListLength(head));
				scanf("%d",&i);
				if(i>(ListLength(head)+1))
					;
				DeleteNode(&head,i);
				break;
			case 5://ListLength
				printf("the length of list is:%d\n",ListLength(head));
				break;
			case 6://DispList
				DispList(head);
				break;
			case 7://InsertionSortList
				InsertionSortList (&head);
				break;
			case 8://Search
		        printf("Input Location(1~%d):",ListLength(head));
		        scanf("%d",&n);
		        if(n<=0)
		        	printf("error\n");
		        else
		            Search(head,n);
		        break;
			case 0:
				DestroyList(&head);
				exit(0);
				break;
			default:
				printf("Input error,retry\n");
				break;
		}//end of switch(choice)
	}//end of while(1)
	return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值