建立顺序表和单链表时,需要自己书写库函数文件 类似于SeqList.h 等的“.h" 文件。库函数中的数据变量均为不确定的,这便于今后再调用的时候,根据程序需要另行定义。
在写主程序是,要注意main() 函数前的数据类型定义,库函数引入和变量定义的先后顺序。对于指针,*p输出一定是某变量的值,p=&a ,*p为a的值,p=a; *p 输出为错误。
其他见解已在代码中标出
单链表实现输出1~10
并删除5
#include <stdio.h>
#include <stdlib.h>
#define MaxSize 100
typedef int DataType;
#include "LinList.h"
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char *argv[]) {
SLNode *head;
int i ,x;
ListInitiate (&head); // 初始化和撤销是对地址的操作
for (i=0;i<10;i++)
{
ListInsert(head,i,i+1);
}
ListDelete (head,4,&x); //需要将所的值输入到 x 的函数需要加 & 符号,需要输出 x 的则不用加 &
for (i=0;i<ListLength(head);i++)
{
ListGet (head,i,&x);
printf (" %d",x);
}
Destroy (&head);
return 0;
}
顺序表实现输出1~10
并删除5
#include <stdio.h>
#include <stdlib.h>
#define MaxSize 100
typedef int DataType;
#include "SeqList.h"
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char *argv[]) {
SeqList myList;
int i ,x;
ListInitiate(&myList);
for (i=0;i<10;i++)
{
ListInsert(&myList,i,i+1);
}
ListDelete(&myList,4,&x);
for (i=0; i<ListLength(&myList); i++) //只写myList 编译错误
{
ListGet(&myList,i,&x); //只写myList 编译错误
printf(" %d",x);
}
return 0;
}
单链表头文件
#ifndef _LINLIST_H_
#define _LINLIST_H_
#endif
#include <stdio.h>
#include <math.h>
typedef struct Node
{
DataType data;
struct Node *next;
}SLNode;
void ListInitiate (SLNode**head)
{
*head=(SLNode*)malloc (sizeof(SLNode));
(*head)->next=NULL;
}
int ListLength (SLNode*head)
{
SLNode *p=head;
int size=0;
while (p->next!=NULL)
{
p=p->next;
size++;
}
return (size);
}
int ListInsert(SLNode*head,int i,DataType x)
{
SLNode *p,*q;
int j;
p=head;
j=-1;
while (p->next!=NULL&&j<i-1)
{
j++;
p=p->next;
}
if (j!=i-1)
{
printf ("插入位置从参数出错!");
}
q=(SLNode*)malloc(sizeof(SLNode));
q->data=x;
q->next=p->next;
p->next=q;
return 1;
}
int ListDelete(SLNode*head,int i,DataType *x)
{
SLNode *p,*s;
int j;
p=head;
j=-1;
while (p->next!=NULL&&p->next->next!=NULL&&j<i-1)
{
p=p->next;
j++;
}
if (j!=i-1)
{
printf ("位置参数出错!");
}
s=p->next;
*x=s->data;
p->next=p->next->next;
free (s);
return 1;
}
int ListGet (SLNode*head,int i,DataType *x)
{
SLNode *p,*s;
int j;
p=head;
j=-1;
while (p->next!=NULL&&j<i-1)
{
p=p->next;
j++;
}
if (j!=i-1)
{
printf ("位置参数出错!");
}
s=p->next;
*x=s->data;
return 1;
}
void Destroy(SLNode **head)
{
SLNode *p,*p1;
p=*head;
while (p!=NULL)
{
p1=p;
p=p->next;
free (p1);
}
*head=NULL;
}
顺序表头文件
#ifndef _SEQLIST_H_ // if not define
#define _SEQLIST_H_
#endif //end if
#include "stdio.h"
#include "math.h"
//#define MaxSize 10
//typedef int DataType;
typedef struct
{
DataType list[MaxSize];
int size;
} SeqList;
//初始化列表
void ListInitiate(SeqList *L)
{
L->size = 0;
}
//得到列表的大小
int ListLength(SeqList *L)
{
return L->size;
}
int ListInsert( SeqList *L, int i, DataType x)
{
int j;
if ( L->size >= MaxSize )
{
printf("顺序表已经满了无法插入!/n");
return 0;
}
else if ( i<0 || i>L->size)
{
printf("参数不合格!");
return 0;
}
else
{
for (j=L->size;j>i;j--)
{
L->list[j] = L->list[j-1];
}
L->list[i] = x;
L->size++;
return 1;
}
}
int ListDelete( SeqList *L, int i , DataType *x)
{
int j;
if ( L->size <=0 )
{
printf("顺序表已经空!/n");
return 0;
}
else if ( i<0 || i>=L->size)
{
printf("参数不合格!");
return 0;
}
else
{
*x = L->list[i];
for ( j = i;j < L->size-1; j++ )
{
L->list[j] = L->list[j+1];
}
L->size--;
return 1;
}
}
int ListGet (SeqList *L, int i, DataType *x)
{
if ( i<0 || i>L->size-1)
{
printf("参数不合格!");
return 0;
}
else
{
*x=L->list[i];
return 1;
}
}