数据结构作业2——利用链表实现增删改查

发布于 29 天前  58 次阅读


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

struct String{
	char name[10];
};
typedef struct Node{
	struct String str;
	struct Node *pNext;
}node,*pnode;

void Show_Menu(void);

pnode  inputstring(void);
void outputstring(pnode phead);
void deletestring(pnode phead);
void searchstring(pnode phead);
void changestring(pnode phead);

void Show_Menu(void){
	printf("================================================================================\n\n");
	printf("================================================================================\n\n");
	printf("*************************欢迎使用字符串数据管理系统*******************************\n\n");
	printf("-----------------------------------------------------------------制作人:吕洪震\n\n");
	printf("********************************************************************************\n\n");
	printf("================================================================================\n\n");
 
 
	printf("请按任意将进入学生管理系统:\n");
	getchar();
	system("cls");
 
	printf("================================================================================\n\n");
	printf("------------------------ 请选择要操作的命令:-----------------------------------\n\n");
	printf("-------------------------- 1  输入字符串信息--------------------------------------\n\n");
	printf("-------------------------- 2  输出字符串信息--------------------------------------\n\n");
	printf("-------------------------- 3  删除字符串信息--------------------------------------\n\n");
	printf("-------------------------- 4  更改字符串信息--------------------------------------\n\n");
	printf("-------------------------- 5  查找字符串信息--------------------------------------\n\n");
	printf("-------------------------- 6  退出------------------------------------------------\n\n");
}

int main(){
	pnode p=NULL;
	int Item;//保存操作命令
	Show_Menu();
	while(1){
		printf("请选择操作命令:");
		scanf("%d",&Item);
	    switch(Item){
			case 1://输入信息
			{
				p=inputstring();
		    }break;
			case 2:
			{
				outputstring(p);
			}break;
			case 3:
			{
				deletestring(p); 
			}break;
			case 4:
			{
				changestring(p); 
			}break;
			case 5:
			{
				searchstring(p);
			}break;
		 	case 6:return 0;
		 	default:printf("输入有误!\n"); 
        }
	}
	system("pause");
	return 0;
} 

pnode  inputstring(void){
	int len;
	node str;
	
	pnode phead=(pnode)malloc(sizeof(node));
	
	if(NULL==phead){
		printf("内存分配失败\n");
		exit(-1);
	}
	
	pnode ptail = phead;
	
	ptail->pNext =  phead->pNext;
	
	printf("请输入输入的个数:");
	scanf("%d",&len);
	
	for(int i=0;i<len;i++){
		system("cls");//清屏
		printf("请输入第%d个数据:",i+1);
		scanf("%s",str.str.name);
		pnode pnew = (pnode)malloc(sizeof(node));
		
		if(NULL==pnew){
			printf("分配失败\n");
			exit(-1);
		}
		
		pnew->str = str.str;
		
		ptail->pNext = pnew;
		
		pnew->pNext = NULL;
		
		ptail = pnew; 
	}
	return phead;
}

void outputstring(pnode phead){
	pnode p = phead->pNext;
	
	printf("保存的字符串:\n");
	while(NULL!=p){
		printf("%s\n",p->str);
		p=p->pNext;
	}
}

void deletestring(pnode phead){
	pnode p=phead;
	
	int i=0;
	int pos;
	
	printf("请输入需要删除的字符串的位置:");
	scanf("%d",&pos);
	
	while(NULL!=p->pNext&&i<pos-1){
		p = p->pNext;
		i++;
	}
	if(NULL == p->pNext || i>pos-1)
	{
		printf("没找到需要删除的位置!\n");
		return;
	}
	pnode q = p->pNext;

	p->pNext = q->pNext;

	free(q);

	q == NULL;
	printf("你已经成功删除了第%d个字符串!\n",pos);
}
//参数为头指针 
void searchstring(pnode phead){
	//输入一个位置
	int i=0,pos;
	pnode p=phead;

	printf("输入要查询的位置:");
	scanf("%d",&pos);
	for(i=1;i<pos+1;i++){
		p=p->pNext;
	} 
	printf("%s",p->str);
}

void changestring(pnode phead){
	char name[10];
	printf("请输入你需要修改的字符串:");
	scanf("%s",&name);
	pnode p = phead->pNext;
	
	while(NULL!=p){
		if(0==strcmp(name,p->str.name)){
			printf("修改前的字符串信息!\n");
			printf("%s\n",p->str.name);
			
			system("pause");
			system("cls");//清屏
			printf("请输入新的字符串内容:");
			scanf("%s",p->str.name); 
			break;
		}
		p=p->pNext;
	}
}


本当の声を響かせてよ