C语言、数据结构。
发布网友
发布时间:2022-04-30 08:32
我来回答
共2个回答
热心网友
时间:2022-06-19 20:31
方案一#include <stdio.h>
#include <conio.h>
#define N 10typedef struct node
{
char name[20];
struct node *link;
}stud;stud * creat()
{
stud *p,*h,*s;
int i,n;
puts("\nPlease input the number of linklist:");
scanf("%d",&n);
if((h=(stud *)malloc(sizeof(stud)))==NULL)
{
printf("cannot find space!");
exit(0);
}
h->name[0]='\0';
h->link=NULL;
p=h;
for(i=0;i<n;i++)
{
if((s= (stud *) malloc(sizeof(stud)))==NULL)
{
printf("cannot find space!");
exit(0);
}
p->link=s;
printf("please input %d student's name: ",i+1);
scanf("%s",s->name);
s->link=NULL;
p=s;
} return(h);
}stud * search(stud *h,char *x)
{
stud *p;
char *y;
p=h->link;
while(p!=NULL)
{
y=p->name;
if(strcmp(y,x)==0)
return(p);
else p=p->link;
}
if(p==NULL)
printf("data not find!");
return 0;
}stud * search2(stud *h,char *x)
{
stud *p,*s;
char *y;
p=h->link;
s=h;
while(p!=NULL)
{
y=p->name;
if(strcmp(y,x)==0)
return(s);
else
{
p=p->link;
s=s->link;
}
}
if(p==NULL)
printf("data not find!");
return 0;
}void insert(stud *p)
{
char stuname[20];
stud *s;
if((s= (stud *) malloc(sizeof(stud)))==NULL)
{
printf("cannot find space!");
exit(0);
}
printf("\nplease input the student's name: ");
scanf("%s",stuname);
strcpy(s->name,stuname);
s->link=p->link;
p->link=s;
}void del(stud *x,stud *y)
{
stud *s;
s=y;
x->link=y->link;
free(s);
}void print(stud *h)
{
stud *p;
p=h->link;
printf("Now the link list is:\n");
while(p!=NULL)
{
printf("%s ",&*(p->name));
p=p->link;
}
printf("\n");
}void quit()
{
clrscr();
puts("\n Thank you for your using!\n Press any key to quit...");
getch();
exit(0);
}void menu(void)
{
clrscr();
printf(" simple linklise realization of c\n");
printf(" ||=====================================||\n");
printf(" || ||\n");
printf(" || [1] create linklist ||\n");
printf(" || [2] seach ||\n");
printf(" || [3] insert ||\n");
printf(" || [4] delete ||\n");
printf(" || [5] print ||\n");
printf(" || [6] exit ||\n");
printf(" || ||\n");
printf(" || if no list exist,create first ||\n");
printf(" || ||\n");
printf(" ||=====================================||\n");
printf(" Please input your choose(1-6): ");
}main()
{
int choose;
stud *head,*searchpoint,*forepoint;
char fullname[20];
while(1)
{
menu();
scanf("%d",&choose);
switch(choose)
{
case 1:
clrscr();
head=creat();
puts("Linklist created successfully! \nPress any key to return...");
getch();
break;
case 2:
clrscr();
printf("Input the student's name which you want to find:\n");
scanf("%s",fullname);
searchpoint=search(head,fullname);
printf("The stud name you want to find is:%s",*&searchpoint->name);
printf("\nPress any key to returen...");
getchar();
getchar();
break;
case 3:
clrscr();
insert(head);
print(head);
printf("\nPress any key to returen...");
getchar();getchar();
break;
case 4:
clrscr();
print(head);
printf("\nInput the student's name which you want to delete:\n");
scanf("%s",fullname);
searchpoint=search(head,fullname);
forepoint=search2(head,fullname);
del(forepoint,searchpoint);
print(head);
puts("\nDelete successfully! Press any key to return...");
getchar();
getchar();
break;
case 5:print(head);
printf("\nPress any key to return...");
getchar();getchar();
break;
case 6:quit();
break;
default:
clrscr();
printf("Illegal letter! Press any key to return...");
menu();
getchar();
}
}
}方案二#include "stdafx.h"
#include "stdio.h"
#ifndef SQLIST_H
#define SQLIST_H
#include <stdlib.h>
#define List_INIT_SIZE 100 //线性表的存储空间初始大小
#define LIST_INCREMENT 10 //分配增量
#define OVERFLOW -2
#define OK 1
#define ERROR -1
#define TRUE 1
#define FALSE 0
typedef int ElemType;
typedef struct{
ElemType *elem; //存储空间基址
int length; //当前长度
int size; //当前存储容量(sizeof(ElemType)为单位)
}SqList;
int InitList(SqList &L)
{
//构建一个线性表L
L.elem = (ElemType*)malloc(List_INIT_SIZE*sizeof(ElemType));
if(!L.elem)
{
exit(OVERFLOW);
}
L.length = 0;
L.size = List_INIT_SIZE;
return OK;
}//InitListvoid DestoryList(SqList &L)
{
if(!L.elem)
{
delete L.elem;
L.elem = NULL;
}
L.length = 0;
L.size = 0;
}//DestoryListvoid ClearList(SqList &L)
{
L.elem = NULL;
L.length = 0;
L.size = List_INIT_SIZE;
}//ClearListint ListEmpty(SqList L)
{
if(L.length > 0)
{
return TRUE;
}
else
{
return FALSE;
}
}//ListEmptyint ListLength(SqList L)
{
return L.length;
}//ListLengthint ListInsert(SqList &L, int i, ElemType e)
{
if(i<1 || i>ListLength(L)+1) //需满足条件1<i<ListLength(L)+1
{
return ERROR;
}
ElemType * NewBase; //新的基址
if(L.length > L.size) //空间已满
{
NewBase = (ElemType*)malloc((L.size+LIST_INCREMENT)*sizeof(ElemType)); //申请新的空间
if(!NewBase)
{
exit(OVERFLOW);
}
L.elem = NewBase;
L.size +=LIST_INCREMENT;
delete NewBase;
NewBase = NULL;
}
ElemType *p;
ElemType *temp;
p = &(L.elem[i-1]); //取得i的位置,即插入位置
for(temp = &(L.elem[L.length-1]);temp>p;--temp) //将插入点后的所有元素向后移动一位
{
*(temp+1) = *temp;
}
*p = e;
++L.length;
return OK;
}//ListInsertint ListDelete(SqList &L, int i, ElemType &e)
{
if (i<0 || i>ListLength(L)) {
return ERROR;
}
ElemType *p;
p = &(L.elem[i-1]);
e = *p;
ElemType *pos;
pos =&(L.elem[L.length-1]);
for(++p; p<=pos; ++p){
*(p-1) = *p;
}
--L.length;
return OK;
}
#endif //SQLIST_H
int main(){
SqList list;
ElemType e;
InitList(list);
ListInsert(list, 1, 1);
ListInsert(list, 2, 2);
ListInsert(list, 3, 3);
ListInsert(list, 4, 4);
ListInsert(list, 5, 5);
for(int i =0; i<list.length; i++){
printf("%d\n",list.elem[i]);
}
ListDelete(list, 2, e);
for(i =0; i<list.length; i++){
printf("%d\n",list.elem[i]);
} printf("%d\n", list.length);
return 1;
}
热心网友
时间:2022-06-19 20:32
代码还是有些多。我想直接把代码打包发给你。
留个邮箱?