求助大神一道数据结构的编程题,C语言编程,用顺序表实现,蟹蟹蟹蟹!! 顺序表的建立及遍历
求数据结构顺序表c语言实现.....
无函数版本,只需你写算法就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) /*在第i个位置上插入一个元素*/
{ }int LocateElem_Sq(SqList L,ElemType e) /*返回元素e在顺序表中的位置*/
{
}Status ListDelete_Sq(SqList &L, int i, int &e) /*删除第i个位置上的元素*/
{ }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);<br> ListInsert_Sq(La,i,e);}
menuselect(La);
getch();
}
c语言数据结构题,,顺序表
初始化一个头结点head,然后比较list1,和list2链表的第一个节点,选择比较小的连接到head上去,如此往复。
#include "stdafx.h"
#include<iostream>
#include<queue>
using namespace std;
typedef struct node{
int data;
struct node *next;
}Node,*List;
List createList(int N,int multi)
{
List head = (List)malloc(sizeof(Node));
head->data = 0;
head->next=NULL;
int count = 0;
List p = head;
while(count<N)
{count++;
List s = (List)malloc(sizeof(Node));
s->data = count*multi;
s->next = NULL;
p->next = s;
p = s;
}
return head;
}
void traverse(List head)
{
if(head == NULL)
{
return;
}
List p = head->next;
while(p)
{
cout<<p->data<<" ";
p = p->next;
}
cout<<endl;
}
List unionList(List list1,List list2)
{
if(list1 == NULL||list1->next==NULL)
return list2;
if(list2 == NULL||list1->next==NULL)
return list1;
list1 = list1->next;
list2 = list2->next;
List head = (List)malloc(sizeof(Node));
List p = NULL;
if(list1!=NULL && list2!=NULL)//初始化第一个节点
{
if(list1->data<=list2->data)
{
head->next = list1;
list1 = list1->next;
}else{
head->next = list2;
list2 = list2->next;
}
}
p = head->next;
while(list1!=NULL && list2!=NULL)//主体部分
{
if(list1->data<=list2->data)
{
p->next = list1;
list1 = list1->next;
p = p->next;
}else{
p->next = list2;
list2 = list2->next;
p = p->next;
}
}
while(list1 != NULL)//如果list2已经完了,list1还有剩余
{
p->next = list1;
list1 = list1->next;
p = p->next;
}
while(list2 != NULL)//如果list1已经完了,list2还有剩余
{
p->next = list2;
list2 = list2->next;
p = p->next;
}
return head;
}
int main()
{
int N = 10;
List head1 = createList(N,1);
List head2 = createList(N,2);
traverse(head1);
traverse(head2);
List head = unionList(head1,head2);
traverse(head);
getchar();
return 0;
}
数据结构(用C语言描述)编程题求助!!!
这是个二叉排序树例题,希望对你有帮助! #include "stdio.h" # include "stdlib.h" struct Bnode {int data; struct Bnode *lchild,*rchild; }; void insertbst(Bnode *&t, Bnode *s) {if (t== NULL) t=s; else if (s->data< t->data) insertbst(t-> lchild,s); else insertbst(t-> rchild,s); } void inorder(Bnode *t) { if (t!=NULL) { inorder(t->lchild); printf("%d ", t->data); inorder(t->rchild); } } void main() {int i,x; Bnode *t=NULL,*s; for (i=1;i<=4;i++) {scanf("%d",&x); s=( Bnode *)malloc(sizeof(Bnode)); s->data=x; s-> lchild= NULL; s-> rchild= NULL; printf("\n"); insertbst(t,s); } inorder( t); printf("\n"); }
求大神!!!急急急!!!用C语言数据结构算法编写程序!!!!!!!!
#include
#include
#include
#define LEN sizeof(stacknode)
typedef struct stacknode
{
char data;
struct stacknode *next;
}stacknode;
typedef struct
{
stacknode *top;
}LinkStack;
int precedence(char c)
{
switch (c)
{
case '#':return 0;
case '+':return 1;
case '-':return 1;
case '*':return 2;
case '/':return 2;
case '(':return 3;
case ')':return 0;
default :return printf("\nError:preccedence函数内输入错误!输入了:%c\n",c);
}
}
void InitStack(LinkStack *s)
{
s->top=NULL;
}
int PUSH(LinkStack *s,char d)
{
stacknode *t;
t=(stacknode*)malloc(LEN);
//if((t=(stacknode*)malloc(LEN))==NULL)return 0;
t->data=d;
t->next=s->top;
s->top=t;
return 1;
}
int Top(LinkStack *s,char *x)
{
stacknode *p;
if(s->top==NULL)return 0;
p=s->top;
s->top=p->next;
*x=p->data;
free(p);
return 1;
}
void Clear(LinkStack *s)
{
stacknode *p,*q;
p=s->top;
free(s);
while(p)
{
q=p->next;
free(p);
p=q;
}
}
int Topshow(LinkStack *s,char *c)
{
stacknode *p;
if(s->top==NULL)return 0;
p=s->top;
*c=p->data;
return 1;
}
int deep(LinkStack *s)
{
int i=0;
stacknode *p;
p=s->top;
while(p)
{
i++;
p=p->next;
}
return i;
}
int oprate(int a,int b,char c)
{
switch (c)
{
case '+':return a+b;
case '-':return a-b;
case '*':return a*b;
case '/':return a/b;
default: printf("\nError:oprate函数内输入错误!输入了:'%c'\n",c);
}
}
int main(void)
{
char ch[500],op[]="+-*/#()[]{}";
LinkStack *s1,*s2;
s1=(LinkStack*)malloc(sizeof(LinkStack));
s2=(LinkStack*)malloc(sizeof(LinkStack));
InitStack(s1);
InitStack(s2);
while(~scanf("%s",ch))
{
int i,l=strlen(ch)+1;
char c;
ch[l-1]='#';
PUSH(s1,'#');
for(i=0;i { int k,key,j; char c1,c2; for(k=0,key=0;i k=ch[i]-'0'+k*10; if(key) { i--; printf("%d\n",k); PUSH(s2,k); } else { char c; int n=precedence(ch[i]); Topshow(s1,&c); while(n<=precedence(c)&&c!='(') { char a,b; Top(s1,&c); if(c=='#') { PUSH(s1,'#'); break; } Top(s2,&b); Top(s2,&a); printf("%d%c%d=%d\n",a,c,b,oprate(a,b,c)); PUSH(s2,oprate(a,b,c)); Topshow(s1,&c); if(c=='('&&ch[i]==')') { Top(s1,&c); break; } } if(ch[i]!=')')PUSH(s1,ch[i]); } } Topshow(s2,&c); printf("%d\n",c); } return 0; }这个 只能进行char级数值(中间过程及结果不得出现127以上的数)的运算,写的很麻烦,楼主多给点分吧。。。 多给要int级数值、加括号的也可以写。。