📄 split.c
字号:
/*splitting into even n odd list*/
#include<conio.h>
#include<stdio.h>
#include<alloc.h>
#define MAX 30
typedef struct node1
{
int ele;
struct node1 *next;
}node;
typedef struct
{
node *arr[MAX];
int top;
}stk;
//creation of stack
void crts(stk *s)
{
s->top=-1;
}
//creation of list
void crt(node **st)
{
*st=NULL;
}
//push operation
void push(stk *s,node **st)
{
if(s->top==MAX-1)
{
printf("stack is full\n");
return;
}
s->arr[++(s->top)]=*st;
}
//pop operation
int pop(stk *s)
{
node *temp;
int data;
if(s->top==-1)
{
printf("stack is empty\n");
return NULL;
}
temp = (s->arr[(s->top)--]);
data = temp->ele;
return data;
}
//insertion
void add(node **st)
{
int data;
node *temp,*temp1;
temp=(node *)malloc(sizeof(node));
if(temp==NULL)
{
printf("no space \n");
return;
}
printf("enter any element \n");
scanf("%d",&data);
temp->ele=data;
temp->next=NULL;
if(*st==NULL)
*st=temp;
else
{
temp1=*st;
while(temp1->next!=NULL)
temp1=temp1->next;
temp1->next=temp;
}
}
//display
void display(node *st)
{
node *temp;
temp=st;
if(temp==NULL)
{
printf("list is empty\n");
return;
}
printf("the elements are\n");
while(temp!=NULL)
{
printf("%d \t",temp->ele);
temp=temp->next;
}
printf("\n");
}
//splitting
void split(node **st)
{
node *temp,*temps,*temp1,*st1,*st2;
if(*st==NULL)
{
printf("list is empty\n");
return;
}
crt(&st1);
crt(&st2);
temp=*st;
//splitting
while(temp!=NULL)
{
temps=(node*)malloc(sizeof(node));
if(temps==NULL)
{
printf("no space\n");
return;
}
temps->ele=temp->ele;
temps->next=NULL;
// even list
if(temp->ele % 2==0)
{
if(temps==NULL)
{
printf("no space\n");
break;
}
if(st1==NULL)
st1=temps;
else
{
temp1=st1;
while(temp1->next!=NULL)
temp1=temp1->next;
temp1->next=temps;
}
}
//odd list
else
{
if(st2==NULL)
st2=temps;
else
{
temp1=st2;
while(temp1->next!=NULL)
temp1=temp1->next;
temp1->next=temps;
}
}
temp=temp->next;
}
temp=*st;
printf("original list \n");
display(temp);
printf("even list\n");
display(st1);
printf("odd list\n");
display(st2);
}
//reverse the list
void rev(node **st)
{
node *next,*pre,*cur;
pre=NULL;
cur=*st;
next=(*st)->next;
(*st)->next=NULL;
while(next!=NULL)
{
pre=cur;
cur=next;
next=cur->next;
cur->next=pre;
}
*st=cur;
printf("elements in reverse are\n");
display(*st);
}
//reverse using stack
void revs(node **st)
{
node *temp;
stk *s;
if(*st==NULL)
{
printf("list is empty\n");
return;
}
temp=*st;
crts(s);
while(temp!=NULL)
{
push( s,&temp);
temp=temp->next;
}
printf("in reverse order\n");
while(s->top!=-1)
printf("%d \t",pop(s));
printf("\n");
}
//for count
int count (node *st)
{
node *temp;
int c=0;
temp=st;
if(temp==NULL)
{
printf("list is empty\n");
return NULL;
}
while(temp!=NULL)
{
c++;
temp=temp->next;
}
return c;
}
//for sorting
void sort(node **st)
{
node *temp,*temp1;
int i,n;
if(count(*st))
{
for(i=0;i<count(*st);i++)
{
temp=*st;
while(temp->next!=NULL)
{
temp1=temp;
if(temp1->ele>temp1->next->ele)
{
n=temp1->ele;
temp1->ele=temp1->next->ele;
temp1->next->ele=n;
}
temp=temp->next;
}
}
printf("sorted list is\n");
display(*st);
}
else
printf("list is empty\n");
}
void main()
{
node *st;
int n;
char ch1;
clrscr();
crt(&st);
do
{
printf(" 1 for insertion \n 2 for splitting evev/odd \n 3 for reverse \n 4 for sorting \n 5 for display\n 6 for exit\n");
scanf("%d",&n);
switch(n)
{
case 1: do
{
add(&st);
printf("wanna more\n");
fflush(stdin);
scanf("%c",&ch1);
}
while(ch1=='y' || ch1=='Y');
break;
case 2: split(&st);
break;
case 3: rev(&st);
revs(&st);
break;
case 4: sort(&st);
break;
case 5: display(st);
break;
case 6: exit();
break;
default : printf("wrong choice\n");
break;
}
}
while(n!=6);
getch();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -