数据结构----线性表

来源:互联网 发布:mac a1502 编辑:程序博客网 时间:2024/05/02 11:14

参考:http://student.zjzk.cn/course_ware/data_structure/web/main.htm

一、线性表:顺序表、单链表、循环链表、双链表

顺序表:

1.表的初始化
   void InitList(SeqList *L)
     {\\顺序表的初始化即将表的长度置为0
            L->length=0;
    }

2.求表长
    int ListLength(SeqList *L)
       { \\求表长只需返回L->length
          return L->length;
      }

3.取表中第i个结点
  DataType GetNode(L,i)
      {\\取表中第i个结点只需返回和L->data[i-1]即可
          if (i<1||i> L->length-1)
                 Error("position error");
          return L->data[i-1];
       }

4.查找值为x的结点

5. 插入
具体算法描述
   void InsertList(SeqList *L,DataType x,int i)
     {//将新结点 x插入L所指的顺序表的第i个结点ai的位置上
       int j;
       if (i<1||i>L->length+1)
          Error("position error");//非法位置,退出运行
       if (L->length>=ListSize)
          Error("overflow");     //表空间溢出,退出运行
       for(j=L->length-1;j>=i-1;j--)
           L->data[j+1]=L->data[j];//结点后移
      L->data[i-1]=x;      //插入x
       L->Length++;       //表长加1
     }

6.删除

具体算法描述

void DeleteList(SeqList *L,int i)
      {//从L所指的顺序表中删除第i个结点ai
         int j;
        if(i<1||i>L->length)
          Error("position error"); //非法位置
       for(j=i;j<=L->length-1;j++)
          L->data[j-1]=L->data[j]; //结点前移
        L->length--;               //表长减小

       }

单链表

#include <iostream>

#include <stdio.h>

#include <string.h>

#include <conio.h>

using namespace std;

 

typedef struct student

{

         intdata;

         structstudent *next;

}node;

 

node *creat()

{

         node*head,*p,*s;

         intx,cycle = 1;

         head= (node*)malloc(sizeof(node));

         p= head;

         while(cycle)

         {

                   printf("\npleaseinput the data:");

                   scanf("%d",&x);

                   if(x!=0)

                   {

                            s= (node*)malloc(sizeof(node));

                            s->data= x;

                            printf("\n   %d",s->data);

                            p->next= s;

                            p= s;

                   }

                   elsecycle = 0;

         }

         head= head->next;

         p->next= NULL;

         printf("\n  yyy %d",head->data);

         return(head);

}

 

int length(node *head)

{

         intn=0;

         node*p;

         p= head;

         while(p!=NULL)

         {

                   p=p->next;

                   n++;

         }

         return(n);

}

 

void print(node *head)

{

         node*p;int n;

         n= length(head);

         printf("\nNow,These%d records are:\n",n);

         p= head;

         if(head!=NULL)

                   while(p!=NULL)

                   {

                            printf("\nuuu  %d  ",p->data);

                            p= p->next;

                   }

}

 

node *del(node *head,int num)

{

         node*p1,*p2;

         p1=head;

         while(num!=p1->data&&p1->next!=NULL)

         {

                   p2=p1;p1=p1->next;

         }

         if(num==p1->data)

         {

                   if(p1==head)

                   {

                            head= p1->next;

                            free(p1);

                   }

                   else

                            p2->next= p1->next;

         }

         else

                   printf("\n%could not been found",num);

         return(head);

}

 

node *insert(node *head,int num)

{

         node*p0,*p1,*p2;

         p1= head;

         p0= (node *)malloc(sizeof(node));

         p0->data= num;

         while(p0->data>p1->data&&p1->next!=NULL)

         {

                   p2= p1;

                   p1= p1->next;

         }

         if(p0->data<=p1->data)

         {

                   if(head== p1)

                   {

                            p0->next= p1;

                            head= p0;

                   }

                   else

                   {

                            p2->next= p0;

                            p0->next= p1;

                   }

         }

         else

         {

                   p1->next= p0;

                   p0->next= NULL;

         }

         return(head);

}

 

int main()

{

         node*head,stud;

         intn,del_num,insert_num;

         head= creat();

         print(head);

         cout<<"\nInt:  ";

         cin>>del_num;

         head= del(head,del_num);

         print(head);

         cout<<"\npleaseinput the insert data:  ";

         cin>>insert_num;

         head= insert(head,insert_num);

         print(head);

         return0;

}