|
|
#2
诸葛修勤2011-03-09 08:11
程序代码:#include<iostream.h> #include<stdlib.h> #include <string.h> #define ERROR -1 #define OK 1 struct Student { char name[6]; //姓名 int id; //学号 int math; //高数 int datastructure; //数据结构 }; typedef struct LNode { struct Student data; struct LNode *next; }LNode,*LinkList; int InitList_L(LinkList L) { LinkList p; Student student[6]={"张三",1,12,43,"李四",2,45,67,"小王",3,45,67,"大军",4,45,67,"钱芳",4,45,67,"大刘",6,45,67}; for(int i=0;i<6;i++) { p = (LinkList) malloc (sizeof(LNode)); if( !p ) { exit(ERROR); } p->data=student[i]; //p->data.datastructure = student[i].datastructure; //p->data.id = student[i].id; //p->data.math = student[i].math; //strcpy(p->data.name, student[i].name); p->next = L->next; L->next = p; } return OK; } /* int ListInsert_L(LinkList L,int i,int e) { LinkList p,q; int j=0; q=L; while(p&&j<i-1) //寻找第一个结点 { p=p->next; ++j; } if(!p||j>i-1) return ERROR; p=(LinkList)malloc(sizeof(LNode)); p->data=e; p->next = q->next; p->next = q; return OK; } */ int Display_L(LinkList L) { LinkList temp = L->next; while( temp ) { cout << "name: " << temp->data.name << " id: " << temp->data.id << "math: " << temp->data.math << "dast: " << temp->data.datastructure << endl; temp = temp->next; } return OK; } int main() { LinkList L = NULL; // int i; // struct Student student; L=(LinkList)malloc(sizeof(LNode)); L->next=NULL; InitList_L(L); Display_L(L); return 0; } |
#include<iostream.h>
#include<stdlib.h>
#define ERROR -1
#define OK 1
struct Student
{
char name[6]; //姓名
int id; //学号
int math; //高数
int datastructure; //数据结构
};
typedef struct LNode
{
struct Student data;
struct LNode *next;
}LNode,*LinkList;
int InitList_L(LinkList L)
{
LinkList p,q;
q=L;
p=(LinkList)malloc(sizeof(LNode));
if(!p)
exit(ERROR);
Student student[6]={("张三",1,12,43),("李四",2,45,67),("小王",3,45,67),("大军",4,45,67),("钱芳",4,45,67),("大刘",6,45,67)};
for(int i=0;i<6;i++)
{
p->data=student[i];
}
p->next = L->next;
L->next = p;
return OK;
}
int ListInsert_L(LinkList L,int i,int e)
{
LinkList p,q;
int j=0;
q=L;
while(p&&j<i-1) //寻找第一个结点
{
p=p->next;
++j;
}
if(!p||j>i-1)
return ERROR;
p=(LinkList)malloc(sizeof(LNode));
p->data=e;
p->next = q->next;
p->next = q;
return OK;
}
int Display_L(LinkList L)
{
for(int i=0;i<6;i++)
{
}
return OK;
}
int main()
{
LinkList L;
int i;
struct Student student;
L=(LinkList)malloc(sizeof(LNode));
L->next=NULL;
InitList_L(L);
Display_L(L);
return 0;
}
程序代码: