求数据结构顺序表c语言实现.....
- 提问者网友:骨子里的高雅
- 2021-07-29 01:02
- 五星知识达人网友:逐風
- 2021-07-29 02:25
无函数版本,只需你写算法就OK,算是比较基础的··
*顺序表的基本操作*/
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#define LIST_INIT_SIZE 10
#define LISTINCREMENT 3
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR -1
#define OVERFLOW -2
typedef int Status;
typedef int ElemType;
typedef struct {
int *elem;
int length;
int listsize;
}SqList;
Status Initlist_Sq(SqList &L)
{
}
int Destroylist(SqList &L)
{
}
int Clearlist_Sq(SqList &L)
{
}
Status Listempty_Sq(SqList L)
{
}
Status ListInsert_Sq(SqList &L, int i,ElemType e)
{
}
int LocateElem_Sq(SqList L,ElemType e)
{
}
Status ListDelete_Sq(SqList &L, int i, int &e)
{
}
void Print_Sq(SqList L)
{ int i;
if (Listempty_Sq(L))
printf("\nSequential List's length is %d. It's empty!",L.length);
else
printf("\nSequential List's length is %d. These element are : ",L.length);
for(i=0;i<L.length;i++)
printf(" %d",L.elem[i]);
}
void menu()
{ printf("\n");
printf("\n* * * * * * * * * * * * * * * * * * * * * * * * * *\n");
printf(" 1 ------- Print the Sequential List(输出顺序表).\n");
printf(" 2 ------- Insert a data in the Sequential List(在第i个位置上插入一个元素).\n");
printf(" 3 ------- Delete a data in the Sequential List(删除第i个位置上的元素).\n");
printf(" 4 ------- Get a element's locationt in the SqList(返回元素e在顺序表中的位置).\n");
printf(" 5 ------- Clear the Sequential List(清空顺序表).\n");
printf(" 6 ------- Exit.\n");
printf("* * * * * * * * * * * * * * * * * * * * * * * * * *\n");
printf("\n");
}
void menuselect(SqList L)
{ int k,i,done=1;
ElemType e;
while (done)
{ menu();
printf("Please choose: ");
scanf("%d",&k);
getchar();
switch(k)
{ case 1: Print_Sq(L); break;
case 2:{
printf("\nInput the location and data you want to Insert: ");
scanf("%d,%d",&i,&e);
if (ListInsert_Sq(L,i,e)==ERROR) printf("Insert location is not correct!\n");
break;
}
case 3:{ printf("\nInput the location you want to delete data: ");
scanf("%d",&i);
if (ListDelete_Sq(L,i,e)==ERROR)
printf("Delete location is not exit!\n");
else
printf("\nDelete data is %d.",e);
break; }
case 4:{ printf("\nInput the data you want to find: ");
scanf("%d",&e);
if (LocateElem_Sq(L,e)!=FALSE)
printf("\nData %d location in the Sequential List is %d.", e,LocateElem_Sq(L,e));
else printf("\nData %d is not in the Sequential List.\n", e);
break; }
case 5: Clearlist_Sq(L);; break;
case 6: done=0;
}
}
}
void main()
{ ElemType e;
SqList La;
int n,i;
Initlist_Sq(La);
printf("Input a number of the element in the Sequential List (n<=%d):",LIST_INIT_SIZE);
scanf("%d",&n);
printf("Enter these elements:");
for(i=1;i<=n;i++)
{scanf("%d",&e);
ListInsert_Sq(La,i,e);}
menuselect(La);
getch();
}
- 1楼网友:怀裏藏嬌
- 2021-07-29 04:39
其实楼上的那不怎么算是结构体,没有用到结构体和指针,不过也说名了什么顺序表。下面我给的这个程序是链表的操作,不过有点小问题。很简单就出在字符串和指针的关系上。尝试修改一下。不行再找我也行。
#include"stdio.h"
#include"string.h"
#include"stdlib.h"
#include"ctype.h"
typedef struct node //定义结点
{
char data[10]; //结点的数据域为字符串
struct node *next; //结点的指针域
}ListNode;
typedef ListNode * LinkList; // 自定义LinkList单链表类型
LinkList CreatListR1(); //函数,用尾插入法建立带头结点的单链表
ListNode *LocateNode(); //函数,按值查找结点
void DeleteList(); //函数,删除指定值的结点
void printlist(); //函数,打印链表中的所有值
void DeleteAll(); //函数,删除所有结点,释放内存
//==========主函数==============
void main()
{
char *ch,*num;
LinkList head;
head=CreatListR1(); //用尾插入法建立单链表,返回头指针
printlist(head); //遍历链表输出其值
printf(" Delete node (y/n):"); //输入“y”或“n”去选择是否删除结点
scanf("%s",num);
if(strcmp(num,"y")==0 || strcmp(num,"Y")==0){
printf("Please input Delete_data:");
scanf("%s",ch); //输入要删除的字符串
DeleteList(head,ch);
printlist(head);
- 2楼网友:怀裏藏嬌
- 2021-07-29 03:21
将表中元素一个接一个的存入一组连续的存储单元中,这种存储结构是顺序结构。 #include <stdio.h> void main(void) { int shu,shuzu[10]; for(int i=0;i<10;i++) { printf("请输入第%d个数:",i+1); scanf("%d",&shu); shuzu[i]=shu; } for(int i=0;i<10;i++) printf(" %d",shuzu[i]); }
希望对你有所帮助!