Data structure Sequential table Operation example _c language

Source: Internet
Author: User

Copy Code code as follows:

#include <stdio.h>
#include <malloc.h>
#define MAXSIZE 1024
typedef CHAR datatype;
typedef struct
{
DataType data[maxsize];
int last;
}sequenlist;


/* Inserts data x before the first element, counting the elements starting at 0 */
int Insert (sequenlist *l,datatype x,int i)
{
 int J;
 if (l->last==maxsize-1)
 {
  printf ("Overflow");
  return 0;
 }
 else if (i<0) | | (I>l->last))
 {
  printf ("Error,please input" right ' I ');
  return 0;
 }
 else
 {
  for (j=l->last;j>=i;j--)
  {
    l->data[j+1]=l->data[j];
   L->data[i]=x;
   l->last=l->last +1;
  } 
 
  return (1);
}


/* Delete the first element, the element starts counting from 0 * *
int dellist (sequenlist *l,int i)
{
if ((i<0) | | (I>l->last))
{printf ("Error,please input" right ' I ');
return 0;}
Else
{
for (; i<l->last; i++)
l->data[i]=l->data[i+1];
L->last =l->last-1;
return (1);
}
}


/* Create a sequential table with an element of a single character * *
void CreateList (Sequenlist *l)
{
int n,i;
char tmp;
printf ("Please enter the number of elements: \ n");
scanf ("%d", &n);
for (i=0;i<n;i++)
{
printf ("data[%d]=", I);
Fflush (stdin);
scanf ("%c", &tmp);
L->data[i] =tmp;
}
l->last=n-1;
printf ("n");
}


/* Print ORDER form * *
void Printflist (Sequenlist *l)
{
int i;
for (i=0;i<l->last; i++)
{
printf ("data[%d]=", I);
printf ("%c\n", L->data [i]);
}
}


Main ()
{
Sequenlist *l;
Char cmd,x;
int i;
L= (Sequenlist *) malloc (sizeof (sequenlist)); /* pointers are initialized before use.
CreateList (L);
Printflist (L);
Todo
{
printf ("I,i ...") Insert \ n ");
printf ("D,d ...") Delete \ n ");
printf ("Q,q ...") Exit \ n ");

Todo
{
Fflush (stdin);
scanf ("%c", &cmd);
}while (cmd!= ' d ') && (cmd!= ' d ') && (cmd!= ' Q ') && (cmd!= ' Q ') && (cmd!= ' i ') && ( cmd!= ' I '));
Switch (CMD)
{
Case ' I ':
Case ' I ':
printf ("Please enter the data you want to insert:");
Fflush (stdin);
scanf ("%c", &x);
printf ("Please enter the location you want to insert:");
scanf ("%d", &i);
Insert (l,x,i);
Printflist (L);
Break

Case ' d ':
Case ' D ':
printf ("Please enter the location of the element you want to delete:");
Fflush (stdin);
scanf ("%d", &i);
Dellist (L,i);
Printflist (L);
Break
}
}while (cmd!= ' Q ') && (cmd!= ' Q '));
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.