-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbasic+trvisal
159 lines (135 loc) · 3.7 KB
/
basic+trvisal
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
link list may be all concept
c
#include<stdio.h>
#include<stdlib.h>
struct node{
int meta;
struct node *link;
};
struct node* creatNode(int k){
struct node *temp =(struct node*) malloc(sizeof(struct node));
temp->meta=k;
temp->link=NULL;
return temp;
}
void printlist(struct node* head){
while(head != NULL){
printf("%d",head->meta);
if(head->link!=NULL){
printf("->");
}
head = head->link;
}
printf("->NULL\n");
}
int serchElement(struct node* ld,int key){
struct node* curent = ld;
int postion =1;
while(curent != NULL){
if(curent->meta == key){
return postion;
}
curent = curent->link;
postion++;
}
return -1;
}
void addnode(struct node* head,int add,int key){
struct node* current = head;
while(current != NULL){
if(current->meta == key){
struct node* newNode=creatNode(add);
newNode->link=current->link;
current->link= newNode;
return;
}
current = current->link;
}
printf("Element %d not found in the list.\n",key);
}
void addElementbyPostion(struct node** ld,int add,int postion){
struct node *newNode = creatNode(add);
if(postion ==1){
newNode->link =*ld;
*ld = newNode;
return;
}
struct node *current = *ld;
for(int i = 1;i < postion -1 && current !=NULL;i++){
current = current ->link;
}
if(current == NULL){
printf("Postion out of rang\n");
free(newNode);
return;
}
newNode->link =current->link;
current->link =newNode;
}
void deletingElementbyPostion(struct node** ld,int postion){
if(*ld ==NULL){
printf("List is Empty\n");
return;
}
struct node* temp = *ld;
if(postion == 1){
*ld = temp->link;
free(temp);
return;
}
for(int i=0;i<postion-1 && temp!=NULL;i++){
temp = temp->link;
}
if(temp== NULL||temp->link == NULL){
printf("postion out of range \n");
}
struct node *link =temp->link->link;
free(temp->link);
temp->link=link;
}
void apending(struct node** ld,int t){
struct node *newNode =creatNode(t);
if(*ld ==NULL){
*ld =newNode;
return;
}
struct node *current = *ld;
while (current->link != NULL)
{
current =current ->link;
}
current -> link =newNode;
}
int main(){
int n=4;
struct node *ld =NULL,*temp=NULL;
for(int i=1;i<=n;i++){
struct node *newnode =creatNode(i);
if(ld==NULL){
ld=newnode;
}
else{
temp->link=newnode;
}
temp = newnode;
}
printlist(ld);
int key = 2;
int add =5;
int postion =serchElement( ld, key);
addnode( ld,add,key);
printlist(ld);
addElementbyPostion(&ld,add,postion-1);
printlist(ld);
deletingElementbyPostion(&ld,postion);
if(postion==-1){
printf("element is not found \n");
}
else{
printf("element postion is %d \n",postion);
}
printlist(ld);
apending(&ld,6);
printlist(ld);
return 0;
}