-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAssignment 3
73 lines (71 loc) · 1.35 KB
/
Assignment 3
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
////Adding Node at the beginning and end of linked list////
#include<iostream>
#include<stdlib.h>
using namespace std;
struct Node {
int data;
struct Node *next;
};
struct Node *create(int);
struct Node *insert_beg(struct Node *head,int );
struct Node *insert_end(struct Node *head,int );
void display(struct Node *head);
int main()
{
struct Node * head=NULL;
head=insert_beg(head,10);
head=insert_beg(head,20);
head=insert_beg(head,30);
display(head);
cout<<"\n";
head=insert_end(head,10);
head=insert_end(head,20);
head=insert_end(head,30);
display(head);
}
struct Node * create(int item)
{
struct Node * npt = new Node;
npt->data=item;
npt->next = NULL;
return npt;
}
struct Node *insert_beg(struct Node *head,int data)
{
struct Node *np=create(data);
if(!head)
{head=np;
return head;}
np->next = head;
head=np;
return head;
}
void display(struct Node *head)
{
if(!head)
{cout<<"list is empty";
}
struct Node *temp = head;
cout<<"list is :";
while(temp != NULL)
{cout<< temp -> data<<" ";
temp=temp -> next;}
}
struct Node *insert_end(struct Node *head,int x)
{
struct Node *np = create(x);
if(!head)
{head=np;
return head;
}
else{
struct Node *temp = head;
while(temp->next)
{temp=temp->next;
}
temp->next=np;
return head;
}
}
Output list is :30 20 10
list is :30 20 10 10 20 30