-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsingle_advance.c
118 lines (111 loc) · 2.09 KB
/
single_advance.c
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
/*
* Simple single linked list oprations program
* ECE532 Data Structure and Algorithms
* FKE UiTM
* wrote by:
* Muhammad Saufy Rohmad msaufy@yahoo.co.uk
*
* functions:
* add_end(int) - add node to the end of linked list
* read() - read all nodes
* delete_end() - delete end nodes (tail node)
*/
#include<stdio.h>
#include<stdlib.h>
void add_end(int);
void read();
void delete_end();
void add_after(int seq,int val);
struct node
{
int data;
struct node *next;
}*head;
void add_end(int n)
{
if(head==0)
{
struct node *new = (struct node*)malloc(sizeof(struct node));
new->data=n;
new->next=NULL;
head=new;
}
else
{
struct node *new = (struct node*)malloc(sizeof(struct node));
new->data=n;
struct node *temp=head;
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=new;
new->next=NULL;
}
}
void read()
{
struct node *temp=head;
while(temp)
{
printf("node at %u, store %d, next is %u\n",temp,temp->data,temp->next);
temp=temp->next;
}
}
void delete_end()
{
struct node *temp=head;
while(temp->next->next!=NULL)
{
temp=temp->next;
}
printf("node at %u with data %d deleted! \n",temp->next,temp->next->data);
temp->next=NULL;
}
void add_after(int seq,int val)
{
struct node *temp=head;
struct node *nw = (struct node*)malloc(sizeof(struct node));
nw->data=val;
int count=0;
while(temp->next!=NULL)
{
count++;
if(count==seq)
break;
temp=temp->next;
}
struct node *temp2;
temp2=temp->next;
temp->next=nw;
nw->next=temp2;
}
void delete_node(int seq)
{
struct node *temp=head;
int count=0;
while(temp->next!=NULL)
{
count++;
if(count==seq-1)
break;
temp=temp->next;
}
printf("baca %d \n",temp->next->data);
struct node *temp2;
temp2=temp->next->next;
printf("baca %d \n",temp2->data);
temp->next=temp2;
}
int main()
{
add_end(10);
add_end(20);
add_end(30);
add_end(40);
add_end(50);
delete_end();
add_after(3,77);
delete_node(3);
read();
}