Linked Lists(Nodes)
Mar 17, 2015 at 1:14am UTC
I want to add a node in the beginning.
Sample Program:
1. Create Lists
2. Add at the beginning
9. Display
Enter choice: 1
How many nodes you want to input? 3
1 2 3
Go back to main menu?[Y/N] y
1. Create Lists
2. Add at the beginning
9. Display
Enter choice: 9
1
2
3
Press any key to go back to main menu...
1. Create Lists
2. Add at the beginning
9. Display
Enter choice: 2
Enter a node on the beginning: 10
Go back to main menu?[Y/N] y
1. Create Lists
2. Add at the beginning
9. Display
Enter choice: 9
10
1
2
3
Press any key to go back to main menu...
This is my problem:
1. Create Lists
2. Add at the beginning
9. Display
Enter choice: 9
10
2
3
How to fix this? Thank you!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
#include<iostream>
using namespace std;
void create();
void addBegin(struct Node **);
void specific();
//void delBegin();
//void delEnd();
//void delSpecific();
//void reverse();
//void sort();
void display();
struct Node{
int info;
struct Node *next;
};
struct Node *initNode(int data){
struct Node *head = (struct Node*)malloc(sizeof (struct Node));
head->info = data;
head->next = NULL;
return head;
}
typedef struct Node *NODE;
int nodes[50], input;
char ch;
int main()
{
int choice;
struct Node *head = initNode(5);
cout << "1. Create List \n\n" ;
cout << "2. Add at beginning \n\n" ;
cout << "3. Add at a specific node \n\n" ;
cout << "4. Delete at the beginning \n\n" ;
cout << "5. Delete at the end \n\n" ;
cout << "6. Delete at a specific node \n\n" ;
cout << "7. Reverese \n\n" ;
cout << "8. Sort \n\n" ;
cout << "9. Display \n\n" ;
cout << endl;
cout << "Enter your choice: " ;
cin >> choice;
switch (choice)
{
case 1:
create();
break ;
case 2:
addBegin(&head);
break ;
case 3:
specific();
/*case 4:
case 5:
case 6:
case 7:
case 8:
case 9:*/
display();
}
system("pause>0" );
return 0;
}
void create()
{
system("cls" );
cout << "How many nodes you want to input? " ;
cin >> input;
cout << "Input " << input << " nodes: " ;
for (int i = 0; i < input; i++)
cin >> nodes[i];
cout << "Go back to main menu? [Y/N]" ;
cin >> ch;
if (ch == 'Y' || ch == 'y' ){
system("cls" );
main();
}
if (ch == 'N' || ch == 'n' )
create();
}
void addBegin(struct Node **head){
system("cls" );
cout << "Insert a node on the beginning: " ;
cin >> nodes[0];
struct Node *NewNode = initNode(nodes[0]);
NewNode->next = *head;
*head = NewNode;
cout << "Go back to main menu? [Y/N]" ;
cin >> ch;
if (ch == 'Y' || ch == 'y' ){
system("cls" );
main();
}
//if (ch == 'N' || ch == 'n')
// addBegin(&head);
}
void display()
{
system("cls" );
for (int i = 0; i < input; i++)
cout << nodes[i] << endl;
cout << "Go back to main menu? [Y/N]" ;
cin >> ch;
if (ch == 'Y' || ch == 'y' ){
system("cls" );
main();
}
if (ch == 'N' || ch == 'n' )
display();
}
void specific()
{
system("cls" );
int node;
cout << "What number of node: " ;
cin >> node;
cout << "What data? " ;
}
Mar 17, 2015 at 5:29am UTC
hi, this is not a linked list
int nodes[50]
this is an array of ints called "nodes", on the first step you might want to have an interface for the whole thing like
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
class List
{
public :
List():head(nullptr ){}
void create();
void addBegin(int i);
void specific();
void delBegin();
void delEnd();
void delSpecific();
void reverse();
void sort();
void display();
private :
Node* head;
}
//addbegin would have something like
void List::addBegin(int i)
{
if (head==nullptr )
{
head=new Node(i,nullptr );
}
else
{
head=new Node(i,head);
}
}
Last edited on Mar 17, 2015 at 5:29am UTC
Mar 17, 2015 at 2:44pm UTC
hey your data is not going to linked list.
it is being stored in array nodes.
your program should be like this.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
#include<iostream.h>
#include<alloc.h>
#include<stdlib.h>
void create();
void addBegin();
void specific();
//void delBegin();
//void delEnd();
//void delSpecific();
//void reverse();
//void sort();
void display();
struct Node
{
int info;
struct Node *next;
};
struct Node *START;
struct Node *lastnode;
int nodes[50], input;
char ch;
int main()
{
int choice;
cout << "1. Create List \n\n" ;
cout << "2. Add at beginning \n\n" ;
cout << "3. Add at a specific node \n\n" ;
cout << "4. Delete at the beginning \n\n" ;
cout << "5. Delete at the end \n\n" ;
cout << "6. Delete at a specific node \n\n" ;
cout << "7. Reverese \n\n" ;
cout << "8. Sort \n\n" ;
cout << "9. Display \n\n" ;
cout << endl;
cout << "Enter your choice: " ;
cin >> choice;
switch (choice)
{
case 1:
create();
break ;
case 2:
addBegin();
break ;
case 3:
specific();
/*case 4:
case 5:
case 6:
case 7:
case 8:*/
case 9:
display();
default :
choice = 0;
}
system("pause>0" );
return 0;
}
void create()
{
struct Node *newnode;
START=0;
system("cls" );
cout << "How many nodes you want to input? " ;
cin >> input;
cout << "Input " << input << " nodes: " ;
for (int i = 0; i < input; i++)
cin >> nodes[i];
if (START==0)
{
newnode=(struct Node*)malloc(sizeof (struct Node));
newnode->next=0;
newnode->info=nodes[0];
START=newnode;
lastnode=newnode;
for (i=1;i<input;i++)
{
newnode=(struct Node*)malloc(sizeof (struct Node));
newnode->next=START;
newnode->info=nodes[i];
START=newnode;
}
}
for (i=0;i<input;i++)
{
newnode=(struct Node*)malloc(sizeof (struct Node));
newnode->next=START;
newnode->info=nodes[i];
START=newnode;
}
cout << "Go back to main menu? [Y/N]" ;
cin >> ch;
if (ch == 'Y' || ch == 'y' )
{
system("cls" );
main();
}
}
void addBegin()
{
struct Node *newnode;
system("cls" );
cout << "Insert a node on the beginning: " ;
cin >> nodes[0];
newnode=(struct Node*)malloc(sizeof (struct Node));
newnode->info=nodes[0];
newnode->next=0;
lastnode->next=newnode;
lastnode=newnode;
cout << "Go back to main menu? [Y/N]" ;
cin >> ch;
if (ch == 'Y' || ch == 'y' )
{
system("cls" );
main();
}
//if (ch == 'N' || ch == 'n')
// addBegin(&head);
}
void display()
{
struct Node *temp=START;
int i=0;
system("cls" );
while (temp!=0)
{
nodes[i]=temp->info;
temp=temp->next;
i++;
}
for (int j=i-1;j>=0;j--)
cout<<nodes[j]<<endl;
cout << "Go back to main menu? [Y/N]" ;
cin >> ch;
if (ch == 'Y' || ch == 'y' )
{
system("cls" );
main();
}
}
void specific()
{
system("cls" );
int node;
cout << "What number of node: " ;
cin >> node;
cout << "What data? " ;
}
Last edited on Mar 17, 2015 at 2:49pm UTC
Topic archived. No new replies allowed.