『壹』 線性表的鏈式存儲結構的典型演算法(求代碼)
假設是單鏈表存貯,從頭遍歷到尾,遍歷每個元素是將指針反轉,只需要一個額外空間。
演算法描述:
header : 指向鏈表頭
p=header;
pre=NULL;
next=NULL;
while(p!=NULL)
{
next=p->next;
p->next=pre;
pre=p;
p=next;
}
head=p;
這樣就反轉了。用了兩個額外空間,時間復雜度O(n).
『貳』 數據結構線性表(C版)問題 設計演算法
Locate_Sq(Splist *L,int i)
{ if(L->len==0) return 0;
for(j=1;j<=l->len;j++)
if(l->elem[i]==x) retrun i;
else return 0;
}
『叄』 java設計線性表排序演算法
import java.util.Scanner;
import java.util.Arrays;
public class P
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
float[] A=new float[1],B=new float[1];
int alen=0,blen=0,i,j,k;
String line;
System.out.println("請輸入線性表A的各元素,每行一個(輸入#結束):");
while(true)
{
line=sc.nextLine();
if(line.equals("#"))
break;
A=Arrays.Of(A,++alen);
A[alen-1]=Float.parseFloat(line);
}
System.out.println("請輸入線性表B的各元素,每行一個(輸入#結束):");
while(true)
{
line=sc.nextLine();
if(line.equals("#"))
break;
B=Arrays.Of(B,++blen);
B[blen-1]=Float.parseFloat(line);
}
Arrays.sort(A);
Arrays.sort(B);
System.out.println("升序排序後,線性表A的各元素是:");
for(i=0;i<alen;i++)
{
System.out.print(A[i]+" ");
}
System.out.println();
System.out.println();
System.out.println("升序排序後,線性表B的各元素是:");
for(i=0;i<blen;i++)
{
System.out.print(B[i]+" ");
}
System.out.println();
System.out.println();
A=Arrays.Of(A,alen+blen);
for(i=0;i<blen;i++)
{
if(B[i]>=A[alen-1])
A[alen++]=B[i];
else
{
for(j=0;j<alen-1;j++)
{
if(B[i]<=A[j])
break;
}
for(k=alen-1;k>=j;k--)
{
A[k+1]=A[k];
}
A[j]=B[i];
alen++;
}
}
System.out.println("線性表B按順序插入線性表A中後,線性表A的各元素是:");
for(i=0;i<alen;i++)
{
System.out.print(A[i]+" ");
}
sc.close();
}
}
『肆』 線性表順序存儲結構及其有關演算法的實現
將順序表初始化為5個元素,在結構中定義了順序表的長度,int length:所以在主函數中可以直接調用用printf(
『伍』 線性表鏈式存儲結構的基本操作演算法實現
這是我學數據結構時親手碼的,好用的話記得給分。。。。。
#include<iostream>
using namespace std;
//線性表的單鏈表存儲表示
struct LNode
{
int data;
struct LNode *next;
};
//逆序創建鏈表
void CreateList(LNode *L,int a[],int n)
{
LNode *s;
L->next = NULL;
for(int i=n-1;i>=0;--i)
{
s= new LNode;
s->data=a[i];
s->next=L->next;
L->next=s;
}
}
//顯示鏈表
void display(LNode *L)
{
LNode *p;
p=L->next;
while(p)
{
cout<<p->data<<" ";
p=p->next;
}
}
//插入結點操作
void ListInsert(LNode *L,int d, LNode *s)
{
LNode *p;
int i=1;
p=L->next;
while(i<d-1)
{
p=p->next;
i++;
}
s->next=p->next;
p->next=s;
}
//刪除節點操作
void ListDelete(LNode *L,int d,int &e)
{
LNode *p,*q;
int i=0;
p=L->next ;
while(i<d-1)
{
q=p;
p=p->next;
i++;
}
e=p->data;
q->next=p->next;
delete p;
}
//查找元素
LNode* LocalElem(LNode *L,int e)
{
LNode *p;
p=L->next;
while(p&&p->data!=e) p=p->next;
return p;
}
//逆置單鏈表
void InvertLinkedList(LNode *L)
{
LNode *p,*s;
p=L->next;
L->next=NULL;
while(p)
{
s=p;
p=p->next;
s->next=L->next;
L->next=s;
}
}
int main()
{
LNode *H;
int n;
cout<<"please enter the length of the list: ";
cin>>n;
int *A=new int[n];
int i;
H=new LNode;
H->next=NULL;
cout<<"please enter "<<n<<" number of the list:"<<endl;
for(i=0;i<n;i++)
cin>>A[i];
CreateList(H,A,n);
cout<<"show the members of the list: ";
display(H);
cout<<endl;
int d;
cout<<"please enter the address of the add member: ";
cin>>d;
LNode *s;
s= new LNode;//初始化局部變數s
cout<<"please enter the data of the add member: ";
cin>>s->data;
ListInsert(H,d, s);
display(H);
cout<<endl;
int p;
cout<<"please enter the address of the delete member: ";
cin>>p;
int c=0;
int &e=c;//非常量引用的初始值必須為左值!!!
ListDelete(H,p,e);
display(H);
cout<<endl;
int g;
cout<<"please enter the member that you want to find: ";
cin>>g;
cout<<"the local of the member is: "<<LocalElem(H,g);
cout<<endl;
InvertLinkedList(H);
cout<<"the invertlinklist is: ";
display(H);
cout<<endl;
return 0;
}
花時間好好看看,一定要看懂
『陸』 數據結構的線性表演算法
插入元素之後,再次輸出元素,但是插入的元素不在線性表中(我用的while(true)控製程序是否結束),
這可能是你在插入後沒有修改表將L.length++
刪除一個元素之後,再次輸出元素的時候,元素還在。如果每次刪除的是最後一個的話,沒進行插入操作,那刪除的元素當然還在表中,這時只是修改了表長L.length
『柒』 如何設計一個演算法求線性表中最大元素
int sqMax(s,n) //用順序表實現,返回最大值,s是數組名,n是數組長度
{
int t;
for(int i=0,i<n,i++)
for(int j=i+1,j<n,j++)
if(s[i]<s[j])
{
t =s[i];
s[i]=s[j];
s[i]=t ;
}
return s[i];
}
『捌』 寫出線性表操作的演算法
你已經十分明白了,還問什麼?
#include<stdio.h>
void search(int a[];int b)
{
int i=0;
while(i<10&&a[i]!=b)
i++;
if(i<10){
printf("found!");
return;
}
else
{
printf("not found");
return;
}
}
main()
{
int a[]={12,26,39,30,52,43,80,92,101,89};
search(a,b);
}
『玖』 【數據結構】求線性表的長度和線性表上的查找演算法
/* 順序存儲類型*/
typedef struct
{ ElemType data[MAXSIZE]; /*存放線性表的數組*/
int length; /* length是順序表的長度*/
}SqList; SqList L;
/* 求順序表長度*/
int ListLength(SqList L)
{return(L.length);}
/* 給定序號從順序表中查找元素*/
void ListGet(SqList L ,int i)
{ if(L.length==0) printf("順序表空\n");
else if(i<1||i>L.length) printf("查找的位置不正確\n");
else printf("順序表中第%d個元素的值為:%d\n",i,L.data[i-1]);
}
/* 從順序表中查找與給定元素值相同的元素在順序表中的位置*/
int ListLocate(SqList L, ElemType x)
{int i=0;
while(i<L.length && L.data[i]!=x)
i++;
if (i<L.length) return (i+1);
else return 0;
}