It's my code.
I don't know linked list exactly.
I think I dont using linked list.
This is ascending sorting problem using linked lists. First, you should sort the given numbers and then you insert a new number into linked lists properly. You can also delete a number from linked lists.
a. Parameter
1) Given numbers : 31, 1 , 7, 22, 10
2) The program can be accepted new number from user’s input.
b. Data structure
1) struct listNode
a) int data
b) listNodePointer rlink(right link)
c) listNodePointer llink(left link)
c. Menu
1) insert number
2) delete number
3) quit program
My question is
1. How do I use linked list about this code?
2. Is this code correct?
Please help me, I am beginner student of computer science.
#include<stdio.h>
int mNum=0;
int x=5;
typedef struct listnode *listNodePointer;
typedef struct listnode{
int data;
listNodePointer llink;
listNodePointer rlink;
}listnode;
void mySwap(int &x, int &y){
int tmp=0;
tmp=x;
x=y;
y=tmp;
}
void sort_function(struct listnode num[], int &x){
int i=0,j=0;
for(i=0;i<x;i++){
j=i;
while(num[j].data < num[j-1].data){
mySwap(num[j].data,num[j-1].data);
j=j-1;
}
printf("Output is : ");
for(int k=0;k-1<i;k++){
printf(" %d",num[k].data);
}
printf("\n");
}
}
void insertNum(struct listnode num[]){
int inputNum,i;
struct listnode nNum;
printf("Input number you want to input :");
scanf("%d",&inputNum);
nNum.data=inputNum;
printf("Output is :");
for(i=0;i<x;i++){
if(nNum.data>num[i].data){
printf(" %d",num[i].data);
}
else
{
nNum.rlink=num[i-1].llink;
nNum.llink=num[i].rlink;
num[i-1].llink=nNum.rlink;
num[i].rlink=nNum.llink;
printf(" %d",nNum.data);
for(;i<x;i++){
printf(" %d",num[i]);
}
printf("\n");
return;
}
}
x++;
}
void display(struct listnode num[]){
printf("----------------------menu--------------------\n");
printf("- 1.Insert-number\n");
printf("- 2.Delete\n");
printf("- 3.Quit\n");
printf("----------------------------------------------\n");
printf("Input menu number :");
scanf("%d",&mNum);
switch(mNum){
case 1:
printf("You select first menu.\n");
insertNum(num);
display(num);
break;
case 2:
printf("You select second menu.\n");
display(num);
break;
case 3:
printf("Bye Bye~\n");
break;
}
}
int main(){
int i=0;
struct listnode first={0,NULL,NULL},*lp=first.rlink,*q;
struct listnode num[5];
num[0].data=31;
num[1].data=1;
num[2].data=7;
num[3].data=22;
num[4].data=10;
num[0].llink=first.rlink;
for(i=0;i<x;i++){
/
num[i].rlink=num[i+1].llink;
num[i+1].llink=num[i].rlink;
}
sort_function(num,x);
display(num);
return 0;
}