[c语言和数据结构]没有直接关系。 1、数据结构 数据结构是计算机存储、组织数据的方式。数据结构是指相互之间存在一种或多种特定关系的数据元素的集合。通常情况下,精心选择的数据结构可以带...+阅读
方案一 #include#include#define N 10 typedef 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;ilink=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#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; }//InitList void DestoryList(SqList &L) { if(!L.elem) { delete L.elem; L.elem = NULL; } L.length = 0; L.size = 0; }//DestoryList void ClearList(SqList &L) { L.elem = NULL; L.length = 0; L.size = List_INIT_SIZE; }//ClearList int ListEmpty(SqList L) { if(L.length >0) { return TRUE; } else { return FALSE; } }//ListEmpty int ListLength(SqList L) { return L.length; }//ListLength int ListInsert(SqList &L, int i, ElemType e) { if(iListLength(L)+1) //需满足条件1
数据结构C语言编程
#include#includetypedef struct node { void *data; struct node *left; struct node *right; }NODE; typedef struct { struct node *head; struct node *current; struct node *rear; int count; }linklist; void init_linklist(linklist *list); void add_linklist(linklist *list,void *data); NODE *get_node_by_index(linklist *list,int index); void printlist(linklist *list); void insert_node_by_index(linklist *list,NODE *newNode,int index); void free_node_by_index(linklist *list,int index); void free_list(linklist *list); int main() { int i=0; int a[10]={1,5,3,8,9,7,5,1,56,85}; linklist list; init_linklist(&list); for (;idata=&k; insert_node_by_index(&list,p,4); printlist(&list); free_list(&list); printlist(&list); return 0; } void init_linklist(linklist *list) { list->head=NULL; list->current=NULL; list->rear=NULL; list->count=0; } void add_linklist(linklist *list,void *data) { NODE *newNode; newNode=(NODE *)malloc(sizeof(NODE)); newNode->data=data; if(list->head==NULL) { list->head=newNode; list->current=newNode; list->rear=newNode; newNode->left=NULL; newNode->right=NULL; list->count++; } else { list->rear->right=newNode; newNode->left=list->rear; newNode->right=NULL; list->rear=newNode; list->current=newNode; list->count++; } } NODE *get_node_by_index(linklist *list,int index) { NODE *tmp=list->head; int i=1; if(index>list->count || indexright; ++i; } return tmp; } } void printlist(linklist *list) { NODE *tmp=list->head; while(tmp!=NULL) { printf("%d\n",*((int *)(tmp->data))); tmp=tmp->right; } return; } void insert_node_by_index(linklist *list,NODE *newNode,int index) { NODE *tmp=NULL; if((tmp=get_node_by_index(list,index))==NULL) return; if((list->count++)==index) { tmp->right=newNode; newNode->left=tmp; newNode->right=NULL; list->current=newNode; list->rear=newNode; } else { newNode->right=tmp->right; tmp->right->left=newNode; newNode->left=tmp; tmp->right=newNode; list->current=newNode; } return; } void free_node_by_index(linklist *list,int index) { NODE *tmp=NULL; if((tmp=get_node_by_index(list,index))==NULL) return; if(list->count==1) { free(tmp); init_linklist(list); } else if(index==1) { list->head=tmp->right; tmp->right=NULL; list->current=tmp->left; list->count--; free(tmp); } else if (index==list->count) { tmp->left->right=NULL; list->rear=tmp->left; list->current=tmp->left; list->count--; free(tmp); } else { tmp->left->right=tmp->right; tmp->right->left=tmp->left; list->current=tmp->left; list->count--; free(tmp); } return; } void free_list(linklist *list) { NODE *tmp=list->head; while(tmp!=NULL) { if(tmp->left==NULL) { continue; tmp=tmp->right; } else if(tmp->right==NULL) { free(tmp); break; } else { free(tmp->left); tmp=tmp->right; } } return; }
关于数据结构 C语言版在线等你完美诠释
类c语言简单的说就跟伪代码同种功能,不能用编译器编译的,一般要翻译成c语言来执行的
课本上有出现如Status的关键字,一般要加入重命名的,如
const TRUE=1; const FALSE=0; const OK=1; conse ERROR=0; typedef int Status;
考试,不考定义的,但是会考怎么排序什么的,如生成最小生成树,我上学期才考完的,最后一道大题是用C语言写出中序遍历的算法,跟课本一模一样的,当然应付考试的话,课本的算法的想法搞清楚就行了。
如果基于应用的话,算法的思想搞清楚了,还要到电脑上敲出来才有感觉的。
类c语言也可以说是标准c语言的简写吧,如把输入a[]的第0~6个元素复制到b[]的第6-12个位置上
用类c简写为b[6...12]=a[0...6];
但这样写编译器编译不了的,得翻译成c语言的
for(i=6,j=0;j<=6;j++,i++)
b[i]=a[j];
呵呵,不是复制来的。
数据结构c语言 14
#include
} } for(i=0;i 以下为关联文档: 数据结构c语言这个问题的算法要考虑周全,程序倒不复杂。以四节火车为例, 1,首先考虑四节火车进站的可能顺序有4!=24种可能。 2,出站时机有2^3=8种可能(以大写字母表入站,小写字母表出站): 出站发... c语言数据结构中结构体定义问题编译器逐行解析,在定义next之前,编译器已经知道struct student这个结构类型的存在,所以可以用它来定义next。 next是指向struct student结构的指针类型。一般像这种用法,都是用... c语言版数据结构#include #include struct chain { int value; struct chain *next; }; struct chain *create() { struct chain *head,*tail,*p; int x; head = tail = NULL; while(scanf(... 数据结构实验C语言:顺序表实验Status Initlist_Sq(SqList &L) /*初始化顺序表*/ { L=(SqList *)malloc(sizeof(SqList)); l->length=0; } void Destroylist(SqList &L) /*销毁顺序表*/ { free(L); } void... C语言数据结构与算法分析C语言描述Position不是一个类型,起码C语言中,我写那么多年代码没见过这个类型 。你该把整段代码贴上来。我猜你看的那段代码是伪代码,Position是自定义类型。若Position是类名,那么Positi... c语言数据结构赋值问题你是传值,不是传址。如下修改,能正确得到理想结果: #include<stdio.h> #include<stdlib.h> //void initstack(struct snode*); struct snode { int data[10]; int top; }; main... 数据结构算法c语言迷宫求解注释非常详细,希望对你有所帮助。#include#include#define M 15 #define N 15 struct mark //定义迷宫内点的坐标类型 { int x; int y; }; struct Element //"恋"栈元素,嘿嘿。... 求解二级C语言中数据结构试题完全二叉树实际就是满二叉树减少最下面一层的部分叶子结点。对于满二叉树来说,深度为K就有2^K-1个结点。700个结点就是介于511(=2^9-1)与1023(2^10-1)之间,那么该完全二叉树的... C语言数据结构上机题#include "stdafx.h" #include<iostream> using namespace std; typedef struct LNode { char data; struct LNode * next; }LNode,* LinkList; void CreateList(LinkList &L)...