赞
踩
1、掌握线性表中元素的前驱、后续的概念。
2、掌握顺序表与链表的建立、插入元素、删除表中某元素的算法。
3、对线性表相应算法的时间复杂度进行分析。
4、理解顺序表、链表数据结构的特点(优缺点)。
#include<stdio.h> #include<malloc.h> #define ERROR 0 #define OK 1 #define INIT_SIZE 5 /*初始分配的顺序表长度*/ #define INCREM 5 /*溢出时,顺序表长度的增量*/ typedef int ElemType; /*定义表元素的类型*/ typedef struct Sqlist{ ElemType *slist; /*存储空间的基地址*/ int length; /*顺序表的当前长度*/ int listsize; /*当前分配的存储空间*/ }Sqlist; int InitList_sq(Sqlist *L); /*构造一个空的顺序表L*/ int CreateList_sq(Sqlist *L,int n); /*向顺序表首位置插入n个元素*/ int ListInsert_sq(Sqlist *L,int i,ElemType e);/*在顺序表第i个位置插入元素e*/ int PrintList_sq(Sqlist *L); /*输出顺序表的元素*/ int ListDelete_sq(Sqlist *L,int i); /*删除第i个元素*/ int ListLocate(Sqlist *L,ElemType e); /*查找值为e的元素*/ int InitList_sq(Sqlist *L){ L->slist=(ElemType*)malloc(INIT_SIZE*sizeof(ElemType)); if(!L->slist) return ERROR; L->length=0; L->listsize=INIT_SIZE; return OK; }/*InitList*/ int CreateList_sq(Sqlist *L,int n){ ElemType e; int i; for(i=0;i<n;i++){ printf("input data %d",i+1); scanf("%d",&e); if(!ListInsert_sq(L,i+1,e)) return ERROR; } return OK; }/*CreateList*/ /*输出顺序表中的元素*/ int PrintList_sq(Sqlist *L){ int i; for(i=1;i<=L->length;i++) printf("%5d",L->slist[i-1]); return OK; }/*PrintList*/ int ListInsert_sq(Sqlist *L,int i,ElemType e){ int k; if(i<1||i>L->length+1) return ERROR; if(L->length>=L->listsize){ L->slist=(ElemType*)realloc(L->slist, (INIT_SIZE+INCREM)*sizeof(ElemType)); if(!L->slist) return ERROR; L->listsize+=INCREM; } for(k=L->length-1;k>=i-1;k--){ L->slist[k+1]= L->slist[k]; } L->slist[i-1]=e; L->length++; return OK; }/*ListInsert*/ /*在顺序表中删除第i个元素*/ int ListDelete_sq(Sqlist *L,int i){ int j; if(i<1||i>L->length) return ERROR; for(j=i;j<L->length;j++) L->slist[j-1]=L->slist[j]; L->length--; return OK; } /*在顺序表中查找指定值元素,返回其序号*/ int ListLocate(Sqlist *L,ElemType e){ ElemType *p; int i=1; p=L->slist; while(i<=L->length) { if((*p++)!=e) ; else printf("%d ",i); ++i; } printf("\n"); return OK; } int main(){ Sqlist sl; int n,m,k,r,l; printf("please input n:"); /*输入顺序表的元素个数*/ scanf("%d",&n); if(n>0){ printf("\n1-Create Sqlist:\n"); InitList_sq(&sl); CreateList_sq(&sl,n); printf("\n2-Print Sqlist:\n"); PrintList_sq(&sl); printf("\nplease input insert location and data:(location,data)\n"); scanf("%d,%d",&m,&k); ListInsert_sq(&sl,m,k); printf("\n3-Print Sqlist:\n"); PrintList_sq(&sl); printf("\nplease input delete location:\n"); scanf("%d",&r); ListDelete_sq(&sl,r); printf("\n4-Print Sqlist:\n"); PrintList_sq(&sl); printf("\nplease input locate data:\n"); scanf("%d",&l); ListLocate(&sl,l); printf("\n"); } else printf("ERROR"); return 0; }
插入一次的时间复杂度为O(n)
int ListDelete_sq(Sqlist *L,int i){
int j;
if(i<1||i>L->length)
return ERROR;
for(j=i;j<L->length;j++)
L->slist[j-1]=L->slist[j];
L->length--;
return OK;
}
删除操作一次时间复杂度为O(n)
int ListLocate(Sqlist *L,ElemType e){
ElemType *p;
int i=1;
p=L->slist;
while(i<=L->length)
{
if((*p++)!=e)
;
else
printf("%d ",i);
++i;
}
printf("\n");
return OK;
}
查找操作一次时间复杂度为O(n)
#include<stdio.h> #include<malloc.h> #define ERROR 0 #define OK 1 typedef int ElemType; /*定义表元素的类型*/ typedef struct LNode{ /*线性表的单链表存储*/ ElemType data; struct LNode *next; }LNode,*LinkList; LinkList CreateList(int n); /*建立带头结点的空链表并存入n个元素*/ void PrintList(LinkList L); /*输出带头结点单链表的所有元素*/ int GetElem(LinkList L,int i,ElemType *e); /*当带头结点的空链表第i个元素存在时赋值给e并返回OK,否则返回ERROR*/ LinkList CreateList(int n){ LNode *p,*q,*head; int i; head=(LinkList)malloc(sizeof(LNode)); head->next=NULL; p=head; for(i=0;i<n;i++){ q=(LinkList)malloc(sizeof(LNode)); printf("input data %i:",i+1); scanf("%d",&q->data); /*输入元素值*/ q->next=NULL; /*结点指针域置空*/ p->next=q; /*新结点连在表末尾*/ p=q; } return head; }/*CreateList*/ void PrintList(LinkList L){ LNode *p; p=L->next; /*p指向单链表的第1个元素*/ while(p!=NULL){ printf("%5d",p->data); p=p->next; } }/*PrintList*/ int GetElem(LinkList L,int i,ElemType *e){ LNode *p;int j=1; p=L->next; while(p&&j<i){ p=p->next;j++; } if(!p||j>i) return ERROR; *e=p->data; return OK; }/*GetElem*/ int InsertElem(LinkList L,int i,ElemType e){ int j=0; LinkList p = L,s; while(p&&j<i-1){ p=p->next; j++; } if(!p||j>i-1){ printf("the node isn't exist\n"); return ERROR; } s = (LinkList)malloc(sizeof(LNode)); s->data = e; s->next = p->next; p->next = s; return OK; } int DeleteElem(LinkList L,int i){ int j=0; LinkList p = L,q; while(p->next&&j<i-1){ p=p->next; j++; } if(!(p->next)||j>i-1){ printf("the node is not exist\n"); return ERROR; } q= p->next; p->next=q->next; free(q); return OK; } int main(){ int n,i,l,d;ElemType e; LinkList L=NULL; /*定义指向单链表的指针*/ printf("please input n:"); /*输入单链表的元素个数*/ scanf("%d",&n); if(n>0){ printf("\n1-Create LinkList:\n"); L=CreateList(n); printf("\n2-Print LinkList:\n"); PrintList(L); printf("\n3-GetElem from LinkList:\n"); printf("input i="); scanf("%d",&i); if(GetElem(L,i,&e)) printf("No%i is %d",i,e); else printf("not exists"); printf("\n4-InsertElen to LinkList:\n"); printf("input location and data="); scanf("%d%d",&l,&d); InsertElem(L,l,d); printf("\n5-Print LinkList:\n"); PrintList(L); printf("\n6-DeleteElen from LinkList:\n"); printf("input location="); scanf("%d",&l); DeleteElem(L,l); printf("\n7-Print LinkList:\n"); PrintList(L); }else printf("ERROR"); return 0; }
int InsertElem(LinkList L,int i,ElemType e){ int j=0; LinkList p = L,s; while(p&&j<i-1){ p=p->next; j++; } if(!p||j>i-1){ printf("the node isn't exist\n"); return ERROR; } s = (LinkList)malloc(sizeof(LNode)); s->data = e; s->next = p->next; p->next = s; return OK; }
插入的时间复杂度为O(n)其中查找的时间复杂度为O(n),插入的时间复杂度为O(1)
int DeleteElem(LinkList L,int i){ int j=0; LinkList p = L,q; while(p->next&&j<i-1){ p=p->next; j++; } if(!(p->next)||j>i-1){ printf("the node is not exist\n"); return ERROR; } q= p->next; p->next=q->next; free(q); return OK; }
操作一次的时间复杂度是O(n)其中查找消耗的时间为O(n)删除的时间复杂度为O(1)
n个数据元素构成一个环,从环中任意位置开始计数,计到m将该元素从表中取出,重复上述过程,直至表中只剩下一个元素。
提示:用一个无头结点的循环单链表来实现n个元素的存储。
#include <stdio.h> #include <iostream> #include <stdlib.h> #include <malloc.h> #define ERROR 0 #define OK 1 typedef int ElemType; /*定义表元素的类型*/ typedef struct LNode /*线性表的单链表存储*/ { ElemType data; struct LNode *next; } LNode,*LinkList; LinkList CreateList(int n) { LNode *p,*q,*head; int i; printf("输入初始的n个数据:\n"); head=(LinkList)malloc(sizeof(LNode)); p = head; printf("输入第1个数据:"); scanf("%d",&head->data); for(i = 1; i < n; i++){ q = (LinkList)malloc(sizeof(LNode)); printf("输入第%d个数据:",i+1); scanf("%d",&q->data); p->next = q; p = q; } p->next = head; return head; }/*CreateList*/ int DeleteEem(LinkList L) { LNode *temp = L,*fnode; while(temp->next != L){ temp = temp->next; } fnode = temp; fnode->next = L->next; free(L); return OK; } void go(LinkList L,int n,int m) { int i,j; LNode *p = L; for(i = 1; i <= n - 1; i++){ for(j = 0; j < m - 1; j++){ p = p->next; } printf("第%d个取出的数据:%d\n",i,p->data); LNode *fnode = p; p = p->next; DeleteEem(fnode); } return ; } int main() { int n,m; LinkList L=NULL; printf("请输入总数据量n,以及一次循环的数量m:"); scanf("%d%d",&n,&m); printf("\n"); L = CreateList(n); printf("\n"); go(L,n,m); printf("\n此时表中便只剩一个元素\n"); return 0; }
提示:指针p从链表的第一个元素开始,利用指针q从指针p位置开始向后搜索整个链表,删除与之值相同的元素;指针p继续指向下一个元素,开始下一轮的删除,直至p==null为至,既完成了对整个链表元素的删除相同值。
#include<stdio.h> #include<malloc.h> #include <iostream> #define ERROR 0 #define OK 1 typedef int ElemType; /*定义表元素的类型*/ typedef struct LNode /*线性表的单链表存储*/ { ElemType data; struct LNode *next; } LNode,*LinkList; LinkList CreateList(int n); /* 创建一个长度为n的单链表 */ void PrintList(LinkList L); /*输出带头结点单链表的所有元素*/ LinkList CreateList(int n) { LNode *p,*q,*head; int i; head=(LinkList)malloc(sizeof(LNode)); head->next=NULL; p=head; for(i=0; i<n; i++) { q=(LinkList)malloc(sizeof(LNode)); printf("输入第%i个元素:",i+1); scanf("%d",&q->data); /*输入元素值*/ q->next=NULL; /*结点指针域置空*/ p->next=q; /*新结点连在表末尾*/ p=q; } return head; }/*CreateList*/ void PrintList(LinkList L) { LNode *p; p=L->next; /*p指向单链表的第1个元素*/ while(p!=NULL) { printf("%5d",p->data); p=p->next; } }/*PrintList*/ int go(LinkList L) { int t; LNode *p = L; p = p->next; for(;p != NULL;) { t = p->data; LNode *now = p->next; LNode *fnode = p; for(;now != NULL;) { LNode *tnode = now; if(now->data == t) { tnode = fnode; fnode->next = now->next; free(now); } fnode = tnode; now = tnode->next; } p = p->next; } return OK; } int main() { int n; LinkList L=NULL; /*定义指向单链表的指针*/ printf("请输入总数据量n:"); /*输入单链表的元素个数*/ scanf("%d",&n); printf("\n"); L = CreateList(n); printf("\n"); printf("删除重复元素之后:\n"); go(L); PrintList(L); return 0; }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。